nginx
一、I/O模型
[root@CentOS7 yum.repos.d]# curl -I www.taobao.com HTTP/1.1 301 Moved Permanently Server: Tengine Date: Thu, 21 Jan 2021 02:30:50 GMT Content-Type: text/html Content-Length: 278 Connection: keep-alive Location: https://www.taobao.com/ Via: cache3.cn2147[,0] Timing-Allow-Origin: * EagleId: 1bd3c51716111962506912372e [root@CentOS7 yum.repos.d]# curl -I -A IE www.sohu.com HTTP/1.1 307 Temporary Redirect Content-Type: text/html Content-Length: 180 Connection: keep-alive Server: nginx Date: Thu, 21 Jan 2021 02:35:13 GMT Location: https://www.sohu.com/ FSS-Cache: from 4242652.6733030.5335298 FSS-Proxy: Powered by 2538690.3325132.3631310 [root@CentOS7 yum.repos.d]# curl -I -A ie http://openresty.org/cn/ HTTP/1.1 200 OK Date: Thu, 21 Jan 2021 02:32:57 GMT Content-Type: text/html; charset=utf-8 Connection: keep-alive Set-Cookie: userid=00002a0000042dfb6ec851b0; Expires=Fri, 21-Jan-22 02:32:57 GMT; Max-Age=31536000; Domain=openresty.org Vary: Accept-Encoding Last-Modified: Mon, 10 Aug 2020 12:53:59 GMT Cache-Control: max-age=3600 Cache-Status: HIT Server: OpenResty Edge 2 Req-ID: 00002a0000042dfb6ec851b0 Edge-Cache-Age: 3627 Cache-Status: EXPIRED
- 静态资源的web服务器 (html,图片,js,css,txt等静态资源)
- http协议反向代理服务器
- FastCGI(LNMP),uWSGI(python),SCGI等协议反向代理动态资源请求
- pop3/imap4协议反向代理服务器
- tcp/udp协议的请求转发(反向代理)
- 模块化(非DSO),如zip,SSL模块 (nginx1.9.11版本后开始支持DSO-动态装载和卸载)
- 虚拟主机(server)
- 支持 keep-alive 和管道连接( 共享TCP连接发起并发的HTTP请求)(一个TCP连接中允许并发HTTP请求)
- 访问日志(支持基于日志缓冲提高其性能)
- url 重定向 rewrite
- 路径别名
- 基于IP及用户的访问控制
- 支持速率限制及并发数限制
- 重新配置和在线升级而无须中断客户的工作进程
- Memcached 的 GET 接口
- 模块化设计,较好的扩展性
- 高可靠性
- 支持热部署:不停机更新配置文件,升级版本,更换日志文件(实际中,作为web服务器,升级版本,更多的是通过前端调度器策略灰度升级)
- 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
- event-driven,aio,mmap,sendfile
2.4 、nginx程序架构
邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、IMAP 协议和 SMTP协议的支持
[root@centos7.6 opt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz #获取源码包 [root@centos7.6 opt]# tar xf nginx-1.18.0.tar.gz [root@centos7.6 opt]# yum install gcc pcre-devel openssl-devel zlib-devel #安装依赖 [root@centos7.6 nginx-1.18.0]# getent passwd nginx [root@centos7.6 nginx-1.18.0]# useradd -r -s /sbin/nologin nginx #建议统一uid,gid [root@centos7.6 nginx-1.18.0]# getent passwd nginx nginx:x:987:981::/home/nginx:/sbin/nologin [root@centos7.6 nginx]# ./configure --help #查看configure
[root@centos7.6 nginx]# ./configure \ #编译 --prefix=/apps/nginx \ --conf-path=/apps/nginx/conf/nginx.conf \ --error-log-path=/apps/nginx/log/nginx/error.log \ --http-log-path=/apps/nginx/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_dav_module \ --with-http_stub_status_module \ --with-threads \ --with-file-aio [root@centos7.6 nginx-1.18.0]# echo $? 0 [root@centos7.6 nginx-1.18.0]# make -j 2 && make #安装 [root@centos7.6 nginx-1.18.0]# echo $? 0
[root@centos7.6 nginx]# tree /apps/nginx/ #自动生成该目录 /apps/nginx/ ├── conf │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── html │ ├── 50x.html │ └── index.html ├── log │ └── nginx └── sbin └── nginx [root@centos7.6 nginx]# ln -s /apps/nginx/sbin/nginx /usr/sbin/ #创建软连接,简介环境变量 [root@centos7.6 nginx]# ll /usr/sbin/nginx lrwxrwxrwx 1 root root 22 Jan 21 13:16 /usr/sbin/nginx -> /apps/nginx/sbin/nginx
[root@centos7.6 nginx]# nginx #启动服务;yum安装也支持此启动方式,但yum安装中systemctl启动服务管理无法管理此种启动方式管理的服务状态
nginx命令说明:
[root@centos7.6 nginx]# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit #检查配置文件语法
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /apps/nginx/)
-c filename : set configuration file (default: /apps/nginx/conf/nginx.conf)
-g directives : set global directives out of configuration file
[root@centos7.6 nginx]# nginx -V #查看编译安装的预编译配置
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments:
--prefix=/apps/nginx
--conf-path=/apps/nginx/conf/nginx.conf
--error-log-path=/apps/nginx/log/nginx/error.log
--http-log-path=/apps/nginx/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_v2_module
--with-http_dav_module
--with-http_stub_status_module
--with-threads
--with-file-aio
[root@centos7.6 nginx]# nginx -v #nginx版本
nginx version: nginx/1.18.0
3.2、nginx目录结构
四、性能优化(全局)
[root@centos7.6 conf]# vi /apps/nginx/conf/nginx.conf worker_processes 2; [root@centos7.6 conf]# ps -aux|grep nginx root 8268 0.0 0.2 46144 2028 ? Ss 14:15 0:00 nginx: master process nginx nginx 8359 0.0 0.2 48664 2132 ? S 14:18 0:00 nginx: worker process nginx 8360 0.0 0.2 48664 2384 ? S 14:18 0:00 nginx: worker process
4.2、worker_cpu_affinity cpumask ...;
[root@centos7.6 conf]# vi /apps/nginx/conf/nginx.conf worker_cpu_affinity 0001 0010; [root@centos7.6 conf]# ps axo user,pid,cmd,psr|grep nginx root 7653 nginx: master process nginx 2 nginx 9062 nginx: worker process 0 nginx 9063 nginx: worker process 1
4.3、worker_priority number;
[root@centos7.6 conf]# ps axo user,pid,cmd,pri|grep nginx root 7653 nginx: master process nginx 19 nginx 9062 nginx: worker process 19 nginx 9063 nginx: worker process 19 [root@centos7.6 conf]# vi /apps/nginx/conf/nginx.conf worker_priority 6; [root@centos7.6 conf]# ps axo user,pid,cmd,pri|grep nginx root 7653 nginx: master process nginx 19 nginx 9357 nginx: worker process 13 nginx 9358 nginx: worker process 13
4.4、worker_rlimit_nofile number;
[root@centos7.6 apps]# tail -f nginx/log/nginx/error.log #观察日志输出 2021/02/18 15:06:40 [crit] 7655#7655: accept4() failed (24: Too many open files) [root@client log]# ab -c 2000 -n 20000 10.0.0.126/index.html [root@centos7.6 conf]# ulimit -a [root@centos7.6 conf]# ulimit -n 66666 #临时有效;永久有效:/etc/security/limits.conf中的nofile [root@centos7.6 conf]# vi /apps/nginx/conf/nginx.conf worker_rlimit_nofile 66666; #并不是越大越好,要考虑服务器性能;虽然nginx的epoll模型没有打开文件的上限,但是当页面很大时,也会收到影响,建议增加内核的ulimit -n [root@centos7.6 conf]# nginx -s reload [root@client log]# ab -c 2000 -n 20000 10.0.0.126/index.html
4.5、事件驱动相关的配置
events {
Default: worker_connections 512;
Context: events
每个worker进程所能够打开的最大并发连接数,如10240,不是越大越好
4.6、调试和定位问题
Context: main
[root@centos7.6 security]# vim /apps/nginx/conf/nginx.conf daemon off; [root@centos7.6 security]# nginx #前台运行
Default: master_process on;
Context: main
默认: [root@centos7.6 ~]# ps aux|grep nginx root 8155 0.0 0.3 46004 3372 pts/0 S+ 21:04 0:00 nginx: master process nginx nginx 8156 0.0 0.2 48544 2016 pts/0 SN+ 21:04 0:00 nginx: worker process nginx 8157 0.0 0.2 48544 2016 pts/0 SN+ 21:04 0:00 nginx: worker process 修改: [root@centos7.6 securivim /apps/nginx/conf/nginx.conf master_process off; #不建议off [root@centos7.6 ~]# ps aux|grep nginx root 8202 0.0 0.3 46456 3632 pts/0 S+ 21:05 0:00 nginx
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
设置某个日志级别将导致记录指定和更严重日志级别的所有消息。
例如,默认级别错误将导致记录error, crit, alert, 和 emerg消息。如果省略此参数,则使用错误。
示例:
nginx-rpm: [root@CentOS7 ~]# vi /etc/nginx/nginx.conf error_log /var/log/nginx/error.log; [root@CentOS7 ~]# nginx -V --error-log-path=/var/log/nginx/error.log nginx-编译: [root@CentOS7.6 nginx]# nginx -V --error-log-path=/apps/nginx/log/nginx/error.log
五、nginx配置之http协议段相关配置
[root@centos7.6 conf]# vi /apps/nginx/conf/mime.types types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; ... } 类型/子类型 文件后缀 text/html html htm shtml;
[root@centos7.6 conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; #在mime.types没有可支持的媒体类型时,指定此默认类型(下载) ... } [root@centos7.6 conf]# cp /etc/fstab /apps/nginx/html/test.xyz #mime.types没有".xyz"媒体类型 [root@centos7.6 conf]# curl -I http://10.0.0.126/test.xyz #观察媒体类型 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 08:57:14 GMT Content-Type: application/octet-stream Content-Length: 737 Last-Modified: Sun, 21 Feb 2021 08:55:46 GMT Connection: keep-alive ETag: "60322012-2e1" Accept-Ranges: bytes

5.1.2、tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
Default: tcp_nopush off;
Context: http, server, location
Default: sendfile off;
Context: http, server, location, if in location
[root@centos7.6 conf]# curl -I http://10.0.0.126 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 09:22:47 GMT Content-Type: text/html Content-Length: 11 Last-Modified: Thu, 18 Feb 2021 06:20:57 GMT Connection: keep-alive ETag: "602e0749-b" Accept-Ranges: bytes [root@centos7.6 conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; charset utf-8;
#include /apps/nginx/conf/conf.d/*.conf; #也可以单独设置配置文件 ... } [root@centos7.6 conf]# curl -I http://10.0.0.126 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 09:24:39 GMT Content-Type: text/html; charset=utf-8 Content-Length: 11 Last-Modified: Thu, 18 Feb 2021 06:20:57 GMT Connection: keep-alive ETag: "602e0749-b" Accept-Ranges: bytes
Default: server_tokens on;
Context: http, server, location
自定义nginx版本信息:
[root@centos7.6 conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; include /apps/nginx/conf/conf.d/*.conf; #另规划配置文件 ... } 基于server_name创建2个虚拟站点: [root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; } server { listen 80; server_name www.magedu.tech; root /data/site2/; } 本地hosts映射: [root@centos7.6 conf]# cat /etc/hosts 10.0.0.126 www.magedu.net www.magedu.tech 创建虚拟站点根目录: [root@centos7.6 conf]# mkdir /data/site1 [root@centos7.6 conf]# mkdir /data/site2 [root@centos7.6 conf]# echo /data/site1/index.html >//data/site1/index.html [root@centos7.6 conf]# echo /data/site2/index.html >//data/site2/index.html 验证: [root@centos7.6 conf]# curl http://www.magedu.net /data/site1/index.html [root@centos7.6 conf]# curl http://www.magedu.tech /data/site2/index.html
Default: —
Context: server, location
简单location设置: [root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /test { root /opt/testdir ; } } 创建虚拟站点根目录: [root@centos7.6 conf]# mkdir /opt/testdir/test [root@centos7.6 conf]# echo /opt/testdir/test/index.html > /opt/testdir/test/index.html 验证: [root@centos7.6 conf]# curl http://10.0.0.126/test/ /opt/testdir/test/index.html
location设置: [root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /test/index.html { root /opt/testdir; } location = /test/index.html { root /opt/testdir2; } } 创建虚拟站点根目录: [root@centos7.6 conf]# mkdir /opt/testdir/test [root@centos7.6 conf]# echo /opt/testdir/test/index.html > /opt/testdir/test/index.html [root@centos7.6 conf]# mkdir /opt/testdir2/test [root@centos7.6 conf]# echo /opt/testdir2/test/index.html > /opt/testdir2/test/index.html 验证: [root@centos7.6 conf]# curl http://10.0.0.126/test/index.html /opt/testdir2/test/index.html [root@centos7.6 conf]# curl http://10.0.0.126/test #location都没匹配上,找/data/site1/下的test/index.html,没有找到 <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
location模拟动静资源分离:
location设置: [root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location ~* \.(jpg|gif|html|txt|js|css)$ { root /opt/static; } location ~ \.(php|jsp|asp)$ { root /opt/dynamic; } } 创建虚拟站点根目录: [root@centos7.6 conf]# mkdir /opt/{static,dynamic} -p [root@centos7.6 conf]# echo /opt/static/index.html >/opt/static/index.html [root@centos7.6 conf]# echo /opt/dynamic/index.php >/opt/dynamic/index.php 验证: [root@centos7.6 conf]# curl http://10.0.0.126/index.html /opt/static/index.html [root@centos7.6 conf]# curl http://10.0.0.126/index.php /opt/dynamic/index.php
alias path;
[root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /about { root /opt/testdir; } } 创建虚拟站点根目录: [root@centos7.6 conf]# mkdir /opt/testdir/about [root@centos7.6 conf]# echo /opt/testdir/about/index.html > /opt/testdir/about/index.html 验证: [root@centos7.6 conf]# curl http://10.0.0.126/about/ /opt/testdir/about/index.html -----------------------------alias-------------------------------------- [root@centos7.6 conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /about { alias /opt/testdir; } } 创建虚拟站点根目录: [root@centos7.6 conf]# echo /opt/testdir/index.html >/opt/testdir/index.html 验证: [root@centos7.6 conf]# curl http://10.0.0.126/about/ /opt/testdir/index.html
5.1.8、
[root@centos7.6 conf.d]# /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; location /about {
root /opt/testdir/; } error_page 404 =200 /404.html; #防止错误劫持 location = /test.html { } } [root@centos7.6 conf.d]# nginx -s reload [root@centos7.6 conf.d]# nginx

5.1.9、try_files
示例:
[root@centos7.6 conf.d]# /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; location /image { alias /opt/image/; try_files $uri $uri/default.jpg =404; } error_page 404 =200 /404.html; location = /test.html { } } [root@centos7.6 conf.d]# nginx -s reload
[root@centos7.6 conf.d]# ls /opt/image/
a.jpg default.jpg
#default.jpg
#a.jpg
#404
浙公网安备 33010602011771号