Nginx
Nginx
1、什么是Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,是一款轻量级的Web服务器反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、网易、腾讯、淘宝等。
2、可提供服务
- 动静分离.
- 负载均衡 (反向代理)
- 高可用模式
3、nginx 的优点
- 高并发。静态小文件
- 占用资源少。2万并发、10个线程,内存消耗几百M。
- 功能种类比较多。web,cache,proxy。每一个功能都不是特别强。
- 支持epoll模型,使得nginx可以支持高并发。
- nginx 配合动态服务和Apache有区别。(FASTCGI 接口)
- 利用nginx可以对IP限速,可以限制连接数。
- 配置简单,更灵活。
4、nginx应用场合
- 静态服务器。(图片,视频服务)另一个lighttpd。并发几万,html,js,css,flv,jpg,gif等。
- 动态服务,nginx——fastcgi 的方式运行PHP,jsp。(PHP并发在500-1500,MySQL 并发在300-1500)。
- 反向代理,负载均衡。日pv2000W以下,都可以直接用nginx做代理。
- 缓存服务。类似 SQUID,VARNISH。
5、nginx实战过程
5.1 安装依赖包
- nginx安装依赖GCC、openssl-devel、pcre-devel和zlib-devel软件库。
- Pcre全称(Perl Compatible Regular Expressions),中文perl兼容正则表达式,pcre官方站点。
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y
12
5.2 开始编译
使用./configure --help查看各个模块的使用情况,使用--without-http_ssi_module的方式关闭不需要的模块。可以使用--with-http_perl_modules方式安装需要的模块。
5.2.1 编译命令
tar -zxf nginx-1.10.1.tar.gz
cd nginx-1.10.1/
./configure --prefix=/data/nginx-1.10.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
useradd nginx -M -s /sbin/nologin
make && make install
ln -s /data/nginx-1.10.1 /data/nginx
1234567
5.2.2 测试nginx配置文件是否正常
/data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx-1.10.1/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx-1.10.1/conf/nginx.conf test is successful
123
5.2.3 启动nginx服务器
/data/nginx/sbin/nginx -t ##检查配置文件
/data/nginx/sbin/nginx ##确定nginx服务
netstat -lntup |grep nginx ## 检查进程是否正常
curl http://localhost ## 确认结果
1234
5.2.4 nginx其他命令
nginx -s signal
signal:
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files
用来打开日志文件,这样nginx会把新日志信息写入这个新的文件中
1234567
/data/nginx/sbin/nginx -V 查看已经编译的参数。
使用kill命令操作nginx。格式:kill -信号 PID
信号名称
- TERM,INT 快速关闭
- QUIT 优雅的关闭,保持吸纳有的客户端连接
- HUP 重启应用新的配置文件
- USR1 重新打开日志文件
- USR2 升级程序
- WINCH 优雅的关闭工作进程
例子
kill -QUIT `cat /data/nginx/nginx.pid`
kill -HUP `cat /data/nginx/nginx.pid`
12
6、nginx配置文件
配置基础配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
### 测试配置文件是否正常
shell> /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx-1.10.3/conf/nginx.conf test is successful
shell> curl -I http://localhost
HTTP/1.1 200 OK
12345678910111213141516171819202122232425262728
7、扩展一:nginx全局变量
$args:这个变量等于请求行中的参数,同$query_string。$is_args: 如果已经设置$args,则该变量的值为"?",否则为""。$content_length: 请求头中的Content-length字段。$content_type: 请求头中的Content-Type字段。$document_uri: 与$uri相同。$document_root: 当前请求在root指令中指定的值。$host: 请求主机头字段,否则为服务器名称。$http_user_agent: 客户端agent信息。$http_cookie: 客户端cookie信息。$limit_rate: 这个变量可以限制连接速率。$request_method: 客户端请求的动作,通常为GET或POST。$remote_addr: 客户端的IP地址。$remote_port: 客户端的端口。$remote_user: 已经经过Auth Basic Module验证的用户名。- $request_body_file`: 客户端请求主体的临时文件名。
$request_uri: 请求的URI,带参数$request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。$scheme: 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;。$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。$server_name: 服务器名称。$server_port: 请求到达服务器的端口号。$request_uri: 包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz,它无法修改。$uri: 不带请求参数的当前URI,$uri不包含主机名,如/foo/bar.html可能和最初的值有不同,比如经过重定向之类的。它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
例子:
访问链接是:http://localhost:88/test1/test.php
网站路径是:/var/www/html
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test.php
$document_uri:/test1/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test.php
12345678910


浙公网安备 33010602011771号