目录:
Nginx简介.
Nginx的特性.
Nginx的功能.
Nginx的模块类型.
源码编译安装Nginx.
nginx相关命令.
Nginx的配置文件介绍.
Nginx的配置指令详解.
正文:
一、Nginx简介:(摘自nginx官网wiki文档)
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability(稳定性), rich feature set, simple configuration, and low resource consumption(消耗).
二、Nginx的特性.
1)模块化设计;
2)高可靠性:
master --> worker
3)低内存消耗:
10000个keep-alive模式下的connection,仅需要2.5MB的内存;
4)支持热部署:
不停机而更新配置文件,日志文件滚动,升级程序版本;
三、Nginx的功能.
1.基本功能:
1)静态资源的web服务器,能缓存打开的文件描述符;
2)http,smpt,pop3协议的反向代理服务器;
3)缓存加速,负载均衡;
4)支持FastCGI(fpm,LNMP),uWSGI(python)等;
5)模块化(非DSO机制),过滤器zip,SSI及图像的大小写调整;
6)支持SSL:
2.扩展功能:
1)基于名称和IP的虚拟主机;
2)支持keepalive;
3)支持平滑升级;
4)定制访问日志,支持使用日志缓冲区提供日志存储性能;
5)支持url rewrite;
6)支持路径别名;
7)支持基于IP及用户的访问限制;
8)支持速率限制,支持并发数限制;
四、Nginx的模块类型.
1. 核心模块
2. Standard HTTP modules
3. Optional HTTP modules
4. Mail modules
5. 3rd party modules
五、源码编译安装Nginx.
系统环境:CentOS7.3
nginx软件包版本:1.10.2 Stable version
Nginx的安装方式有两种,即RPM安装和源码编译安装。此处采用编译安装的方式,Linux系统为CentOS7.3.
首先到官网下载相应Stable version安装包:
<code>[root@nginx tools]</code><code># cat /etc/redhat-release </code>
<code>CentOS Linux release 7.3.1611 (Core) </code>
<code>[root@nginx tools]</code><code># wget http://nginx.org/download/nginx-1.10.2.tar.gz</code>
然后开始安装:
<code>[root@nginx ~]</code><code># useradd -s /sbin/nologin -M nginx</code>
<code>//</code><code>创建nginx用户及nginx组来运行nginx服务进程.</code>
<code>[root@nginx ~]</code><code># mkdir /data/nginx/logs/ -p</code>
<code>[root@nginx ~]</code><code># touch /data/nginx/logs/error.log </code>
<code>[root@nginx ~]</code><code># mkdir /data/nginx -p</code>
<code>[root@nginx ~]</code><code># touch /data/nginx/{nginx.pid,nginx.lock} </code>
<code>//</code><code>创建相关文件的存储位置.</code>
<code>[root@nginx ~]</code><code># cd /mnt/tools/</code>
<code>[root@nginx tools]</code><code># yum -y gd gd-devel pcre pcre-devel</code>
<code>[root@nginx tools]</code><code># tar zxf nginx-1.10.2.tar.gz</code>
<code>[root@nginx tools]</code><code># cd nginx-1.10.2</code>
<code>[root@nginx nginx-1.10.2]</code><code># ls</code>
<code>auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile </code><code>man</code> <code>objs README src</code>
<code>[root@nginx nginx-1.10.2]</code><code>#</code>
注:
1)gd库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。
2)PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
查看编译安装的选项:
<code>[root@nginx nginx-1.10.2]</code><code># ./configure --help</code>
<code> </code><code>--help print this message</code>
<code> </code><code>--prefix=PATH </code><code>set</code> <code>installation prefix</code>
<code> </code><code>--sbin-path=PATH </code><code>set</code> <code>nginx binary pathname</code>
<code> </code><code>--modules-path=PATH </code><code>set</code> <code>modules path</code>
<code> </code><code>--conf-path=PATH </code><code>set</code> <code>nginx.conf pathname</code>
<code> </code><code>--error-log-path=PATH </code><code>set</code> <code>error log pathname</code>
<code> </code><code>--pid-path=PATH </code><code>set</code> <code>nginx.pid pathname</code>
<code> </code><code>--lock-path=PATH </code><code>set</code> <code>nginx.lock pathname</code>
<code> </code><code>--user=USER </code><code>set</code> <code>non-privileged user </code><code>for</code>
<code> </code><code>worker processes</code>
<code> </code><code>--group=GROUP </code><code>set</code> <code>non-privileged group </code><code>for</code>
<code> </code><code>--build=NAME </code><code>set</code> <code>build name</code>
<code> </code><code>--builddir=DIR </code><code>set</code> <code>build directory</code>
<code> </code><code>--with-select_module </code><code>enable</code> <code>select</code> <code>module</code>
<code> </code><code>--without-select_module disable </code><code>select</code> <code>module</code>
<code> </code><code>--with-poll_module </code><code>enable</code> <code>poll module</code>
<code> </code><code>--without-poll_module disable poll module</code>
<code> </code><code>--with-threads </code><code>enable</code> <code>thread pool support</code>
<code> </code><code>--with-</code><code>file</code><code>-aio </code><code>enable</code> <code>file</code> <code>AIO support</code>
<code> </code><code>--with-ipv6 </code><code>enable</code> <code>IPv6 support</code>
<code> </code><code>--with-http_ssl_module </code><code>enable</code> <code>ngx_http_ssl_module</code>
<code> </code><code>--with-http_v2_module </code><code>enable</code> <code>ngx_http_v2_module</code>
<code> </code><code>--with-http_realip_module </code><code>enable</code> <code>ngx_http_realip_module</code>
<code> </code><code>--with-http_addition_module </code><code>enable</code> <code>ngx_http_addition_module</code>
<code> </code><code>--with-http_xslt_module </code><code>enable</code> <code>ngx_http_xslt_module</code>
<code> </code><code>--with-http_xslt_module=dynamic </code><code>enable</code> <code>dynamic ngx_http_xslt_module</code>
<code> </code><code>--with-http_image_filter_module </code><code>enable</code> <code>ngx_http_image_filter_module</code>
<code> </code><code>--with-http_image_filter_module=dynamic</code>
<code> </code>
<code> </code><code>......</code>
注:以上模块编译时需要有选择性的安装,万一哪个模块安装时漏掉了也不用担心,因为nginx支持热部署,可以随时增加需要的模块!
下一步进行编译:
<code>[root@nginx nginx-1.10.2]</code><code># ./configure \ </code>
<code>--prefix=</code><code>/usr/local/nginx-1</code><code>.10.2 \ </code>
<code>--error-log-path=</code><code>/data/nginx/logs/error</code><code>.log \ </code>
<code>--pid-path=</code><code>/data/nginx/nginx</code><code>.pid \ </code>
<code>--lock-path=</code><code>/data/nginx/nginx</code><code>.lock \ </code>
<code>--user=nginx \</code>
<code>--group=nginx \</code>
<code>--with-threads \</code>
<code>--with-http_ssl_module \ </code>
<code>--with-http_image_filter_module \</code>
<code>--with-http_image_filter_module=dynamic \</code>
<code>--with-http_flv_module \ </code>
<code>--with-http_mp4_module \ </code>
<code>--with-http_gunzip_module \ </code>
<code>--with-http_gzip_static_module \ </code>
<code>--with-http_slice_module \</code>
<code>--with-stream \ </code>
<code>[root@nginx nginx-1.10.2]</code><code># echo $?</code>
<code>0</code>
<code>[root@nginx nginx-1.10.2]</code><code># make && make install</code>
做软连接:
<code>[root@nginx nginx-1.10.2]</code><code># cd</code>
<code>[root@nginx ~]</code><code># ls /usr/local/nginx-1.10.2/ -d</code>
<code>/usr/local/nginx-1</code><code>.10.2/</code>
<code>[root@nginx ~]</code><code># ln -sv /usr/local/nginx-1.10.2/ /usr/local/nginx</code>
<code>"/usr/local/nginx"</code> <code>-> </code><code>"/usr/local/nginx-1.10.2/"</code>
<code>[root@nginx ~]</code><code>#</code>
启动服务并查看监听端口:
<code>[root@nginx ~]</code><code># /usr/local/nginx/sbin/nginx </code>
<code>[root@nginx ~]</code><code># ss -tunlp |egrep "nginx"</code>
<code>tcp LISTEN 0 128 *:80 *:* </code><code>users</code><code>:((</code><code>"nginx"</code><code>,pid=11743,fd=6),(</code><code>"nginx"</code><code>,pid=11742,fd=6))</code>
提前关闭防火墙.
<code>[root@nginx ~]</code><code># systemctl list-unit-files |grep firewalld</code>
<code>firewalld.service enabled </code>
<code>[root@nginx ~]</code><code># systemctl disable firewalld.service</code>
<code>Removed </code><code>symlink</code> <code>/etc/systemd/system/dbus-org</code><code>.fedoraproject.FirewallD1.service.</code>
<code>Removed </code><code>symlink</code> <code>/etc/systemd/system/basic</code><code>.target.wants</code><code>/firewalld</code><code>.service.</code>
<code>firewalld.service disabled</code>
浏览器访问测试:
<a href="https://s3.51cto.com/wyfs02/M02/8C/B2/wKiom1h0oqLAKGAMAAB2xkkyfoY063.png" target="_blank"></a>
六、nginx相关命令.
1. 开启nginx服务的初始命令:
# /usr/local/nginx/sbin/nginx
注:该命令自己也可以写成脚本,通过systemctl(CentOS7)或service(CentOS6)启动.
2. 新改的配置生效方式
# /usr/local/nginx/sbin/nginx -s SIGNAL
SIGNAL包括: reload, stop, quit, reopen
3. 查看系统已装载的nginx模块选项:
# /usr/local/nginx/sbin/nginx -V
七、Nginx的配置文件介绍.
Nginx的配置段主要有以下三项:
1、main配置段:全局配置段
2、event: 定义event模型工作特性
3、http{}: 定义http协议相关的配置
配置文件介绍:
<code>[root@nginx ~]</code><code># vim /usr/local/nginx/conf/nginx.conf</code>
<code> </code>
<code>#user nobody; //定义Nginx进程运行的用户和用户组</code>
<code>worker_processes 1; </code><code>//nginx</code><code>进程数,建议设置为物理CPU总核心减1个。</code>
<code>#error_log logs/error.log;</code>
<code>#error_log logs/error.log notice;</code>
<code>#error_log logs/error.log info; //定义全局日志类型</code>
<code>#pid logs/nginx.pid; //保存nginx进程的pid文件</code>
<code>events {</code>
<code> </code><code>worker_connections 1024; </code><code>//</code><code>单个进程最大连接数(最大连接数=连接数*进程数)</code>
<code> </code>
<code>}</code>
<code>http {</code>
<code> </code><code>include mime.types; </code><code>//</code><code>文件扩展名与文件类型映射表.</code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>; </code><code>//</code><code>默认文件类型</code>
<code>http:</code><code>//blog</code><code>.51cto.com</code><code>/user_index</code><code>.php?action=addblog_new&did=304899</code>
<code> </code><code>#log_format main '$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code># '$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code># '"$http_user_agent" "$http_x_forwarded_for"';</code>
<code> </code><code>#access_log logs/access.log main;</code>
<code> </code><code>sendfile on;</code>
<code> </code><code>#tcp_nopush on;</code>
<code> </code><code>#keepalive_timeout 0;</code>
<code> </code><code>keepalive_timeout 65; </code><code>//</code><code>长连接超时时间,单位是秒</code>
<code> </code><code>gzip</code> <code>on; </code><code>//</code><code>开启</code><code>gzip</code><code>压缩输出</code>
<code> </code><code>server { </code><code>//</code><code>虚拟主机的配置</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name localhost; </code><code>//</code><code>域名可以有多个,用空格隔开</code>
<code> </code><code>#charset koi8-r;</code>
<code> </code><code>#access_log logs/host.access.log main;</code>
<code> </code><code>location / {</code>
<code> </code><code>root html;</code>
<code> </code><code>index index.html index.htm;</code>
<code> </code><code>}</code>
<code> </code><code>#error_page 404 /404.html;</code>
<code> </code><code># redirect server error pages to the static page /50x.html</code>
<code> </code><code>#</code>
<code> </code><code>error_page 500 502 503 504 </code><code>/50x</code><code>.html;</code>
<code> </code><code>location = </code><code>/50x</code><code>.html {</code>
<code> </code><code># proxy the PHP scripts to Apache listening on 127.0.0.1:80</code>
<code> </code><code>#location ~ \.php$ {</code>
<code> </code><code># proxy_pass http://127.0.0.1;</code>
<code> </code><code>#}</code>
<code> </code><code># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</code>
<code> </code><code># root html;</code>
<code> </code><code># fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code># fastcgi_index index.php;</code>
<code> </code><code># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;</code>
<code> </code><code># include fastcgi_params;</code>
<code> </code><code># deny access to .htaccess files, if Apache's document root</code>
<code> </code><code># concurs with nginx's one</code>
<code> </code><code>#location ~ /\.ht {</code>
<code> </code><code># deny all;</code>
<code> </code><code>}</code>
<code> </code><code># another virtual host using mix of IP-, name-, and port-based configuration</code>
<code> </code><code>#</code>
<code> </code><code>#server {</code>
<code> </code><code># listen 8000;</code>
<code> </code><code># listen somename:8080;</code>
<code> </code><code># server_name somename alias another.alias;</code>
<code> </code><code># location / {</code>
<code> </code><code># root html;</code>
<code> </code><code># index index.html index.htm;</code>
<code> </code><code># }</code>
<code> </code><code>#}</code>
<code> </code><code># HTTPS server //https配置段.</code>
<code> </code><code># listen 443 ssl;</code>
<code> </code><code># server_name localhost;</code>
<code> </code><code># ssl_certificate cert.pem;</code>
<code> </code><code># ssl_certificate_key cert.key;</code>
<code> </code><code># ssl_session_cache shared:SSL:1m;</code>
<code> </code><code># ssl_session_timeout 5m;</code>
<code> </code><code># ssl_ciphers HIGH:!aNULL:!MD5;</code>
<code> </code><code># ssl_prefer_server_ciphers on;</code>
八、Nginx的配置指令详解.
1. 正常运行的必备配置段:
1) user USERNAME [GROUPNAME]
指定运行worker进程的用户和组;
例:user nginx nginx
注: 用户和组如果一起部署且相同的话,GROUPNAME可以省略.
2) pid /path/to/PID_FILE
指定nginx守护进程的pid文件.
例: pid /data/nginx/nginx.pid
注: pid文件的作用在于防止进程启动多个副本.只有获得pid文件写入权限的进程才能正常启动并 把自身进程pid写入该文件中,其他同一个程序的多余进程会自动退出.
查看本系统的pid文件:
<code>[root@nginx ~]</code><code># cat /data/nginx/nginx.pid</code>
<code>cat</code><code>: </code><code>/data/nginx/nginx</code><code>.pid: 没有那个文件或目录</code>
<code>2561</code>
<code>[root@nginx ~]</code><code># ps -aux |egrep "nginx"</code>
<code>root 2529 0.1 0.5 151800 5436 pts</code><code>/0</code> <code>S+ 10:18 0:00 vim </code><code>/usr/local/nginx/conf/nginx</code><code>.conf</code>
<code>root 2561 0.0 0.1 45376 1112 ? Ss 10:27 0:00 nginx: master process </code><code>/usr/local/nginx/sbin/nginx</code>
<code>nginx 2562 0.0 0.1 45824 1884 ? S 10:27 0:00 nginx: worker process</code>
<code>root 2565 0.0 0.0 112664 972 pts</code><code>/1</code> <code>S+ 10:28 0:00 </code><code>grep</code> <code>-E --color=auto nginx</code>
3) worker_connections NUM;
指定所有worker进程所能够打开的最大文件句柄数;
默认打开文件最大数为1024个.
2. 性能优化相关的配置:
1) worker_processes NUM;
指定nginx进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。
例: worker_processes 4;
2) worker_cpu_affinity cpumask ...;
指定为每个进程分配CPU,提升缓存的命中率.
例: worker_cpu_affinity 00000001 00000010 00000100 00001000;
上例中将4个进程分配到4个cpu,注意与worker_process数相对应.当然可以写多个,或者将一个进程分配到多个cpu。
3) timer_resolution TIME;
指定计时器解析度:降低此值,可减少gettimeofday()系统调用的次数;
默认值:none
例: timer_resolution 100ms;
该配置指令允许用户减少调用gettimeofday()的次数。默认情况下,该函数在每次I/O端口监听 (比如epoll_wait)返回后都将被调用,而通过timer_resolution配置选项可以直接指定调用 gettimeofday()函数的间隔时间.
4) worker_priority NUM:
指明worker进程的nice值,即worker进程的优先级;
nice值越小,优先级越高,默认只能由管理员有权限调整nice值;
3. 事件相关的配置:
1) accept_mutex {off|on};
指定master进程调度用户请求至各worker进程时使用的负载均衡锁; on表示能让多个worker轮流地、序列化地去响应新请求;
2) lock_file file;
accept_mutex用到的锁文件路径;
3) use [epoll|rtsig|select|poll];
指明使用的时间模型;建议让nginx自行选择;
4) worker_connections #;
设定单个worker进程所能够处理的最大并发连接数量;
计算公式:worker_connections * work_processes , 可能会小于这个值;
4. 用户用于调试, 定位问题:
1) daemon {on|off};
是否以守护进程方式运行nginx: 调试时应该设置为off。
2) master_process {on|off};
是否以master/worker模型来运行nginx;调试时可以设置为off;
3) error_log file |stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg];
语法: error_log [位置] [级别];
若要使用debug级别, 需要在编译nginx时使用--with-debug选项;
5. 总结:常需要进行调整的参数:
worker_processes,worker_connections,worker_cpu_affinity,worker_priority.
--- 第一部分完成!
本文转自 羽丰1995 51CTO博客,原文链接:http://blog.51cto.com/13683137989/1891105