Nginx
介绍
Nginx HTTP服务器的特色及优点
a. 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
b. 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB
c. 可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能
d. 具备Squid等专业缓存软件等的缓存功能
e. 支持异步网络I/O事件模型epoll
Nginx的主要企业功能
a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)
b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)
c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)
Nginx作为web服务器的主要应用场景包括:
a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)
b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)
c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)
一般情况下普通php引擎支持的并发连接参考为300-1000,Java引擎和数据库的并发连接参考值为300-1500.当然架构不同可能会有浮动
安装
编译安装
# 下载,这里是1.12版本 wget http://nginx.org/download/nginx-1.12.2.tar.gz # 解压并进入目录 tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 # 三部曲 ./configure --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module make make install
可能会遇到的故障
故障一:没有安装pcre或pcre-devel
会提示如下错误:
./configure: error: the HTTP rewrite module requires the PCRE library. 2 You can either disable the module by using --without-http_rewrite_module 3 option, or install the PCRE library into the system, or build the PCRE library 4 statically from the source with nginx by using --with-pcre=<path> option.
故障二:没有安装openssl和openssl-devel
./configure: error: SSL modules require the OpenSSL library. 2 You can either do not enable the modules, or install the OpenSSL library 3 into the system, or build the OpenSSL library statically from the source 4 with nginx by using --with-openssl=<path> option.
如果是学习,需要关闭防火墙和selinux
启动
/application/nginx/sbin/nginx –t # 检查 /application/nginx/sbin/nginx # 启动 /application/nginx/sbin/nginx -s reload# 默默的重启
启动时出现 nginx: [emerg] getpwnam("nginx") failed 异常, 是因为缺少nginx用户
useradd -s /sbin/nologin -M nginx
目录结构
安装目录下,并非安装包的解压文件
[root@v-1 nginx-1.12.2]# tree . ├── client_body_temp ├── conf # 这是Nginx所有配置文件的目录 │ ├── fastcgi.conf # fastcgi相关参数的配置文件 │ ├── fastcgi.conf.default │ ├── fastcgi_params # #fastcgi的参数文件 │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types # 媒体类型 │ ├── mime.types.default │ ├── nginx.conf # nginx默认的主配置文件 │ ├── nginx.conf.default │ ├── scgi_params # scgi相关参数 │ ├── scgi_params.default │ ├── uwsgi_params # uwsgi相关参数 │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp # fastcgi临时数据目录 ├── html # 编译安装Nginx的默认站点目录 │ ├── 50x.html # 错误页面优雅替代显示文件 │ └── index.html # 默认的首页文件 ├── logs # 默认的日志路径包括错误日志和访问日志 │ ├── access.log │ ├── error.log │ └── nginx.pid ├── proxy_temp # 临时目录 ├── sbin # Nginx命令目录 │ └── nginx # 启动命令 ├── scgi_temp # 临时目录 └── uwsgi_temp
配置文件
nginx.conf和nginx.conf.default默认是相同的,可以通过diff命令来比较两个文件
# 通过命令将nginx配置文件精简化显示(去掉#注释和空行的内容): egrep -v "#|^$" nginx.conf.default >nginx.conf # egrep是grep的升级版,增加了正则|的使用 worker_processes 1; #worker进程的数量 events { #事件区块的开始 worker_connections 1024; #每个worker进程支持的最大连接数 } #事件区块的结束 http { #http区块的开始 include mime.types; #nginx支持的媒体类型库文件 default_type application/octet-stream; #默认的媒体类型 sendfile on; #开启高效传输模式 keepalive_timeout 65; #连接超时 server { #第一个server区块开始,表示一个独虚拟主机站点 listen 80; #服务端口,默认80 server_name localhost; #提供服务的域名主机名 location / { #第一个location区块开始 root html; #站点的根目录,相当于Nginx的安装目录 index index.html index.htm; #默认的首页文件,如果多个用空格分开 } #第一个location区块结束 error_page 500 502 503 504 /50x.html; #出现对象http状态码时使用50x.html回应用户 location = /50x.html { root html; } } } #http区块结束
代理TCP
代理tcp在编译时要指定选项
./configure --with-http_stub_status_module --with-stream
配置文件
stream { upstream rtmp { server 127.0.0.1:1936; # 这里配置成要访问的地址 server 127.0.0.1:1937; } server { listen 1935; # 需要监听的端口 proxy_timeout 20s; proxy_pass rtmp; } }