Nginx [TOC]
一、Nginx的编译安装 1.1、官方源码包下载地址: https://nginx.org/en/download.html
1.2、编译安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] [root@Rocky src] [root@Rocky src] nginx-1.22.1 nginx-1.22.1.tar.gz [root@Rocky src] [root@Rocky nginx-1.22.1] auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@Rocky nginx-1.22.1] --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module [root@Rocky nginx-1.22.1] [root@Rocky ~] /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 ├── logs └── sbin └── nginx [root@Rocky ~] [root@Rocky ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@Rocky ~] root 29907 0.0 0.0 42572 848 ? Ss 12:05 0:00 nginx: master process /apps/ngin nginx 29908 0.0 0.2 74680 4948 ? S 12:05 0:00 \_ nginx: worker process [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@Rocky ~] [root@Rocky ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@Rocky ~] [root@Rocky ~]
1.3、设置启动方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [root@Rocky ~] [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/apps/nginx/run/nginx.pid ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID LimitNOFILE=100000 [Install] WantedBy=multi-user.target :wq [root@Rocky ~] [root@Rocky ~] pid /apps/nginx/run/nginx.pid; :wq [root@Rocky ~] Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [root@Rocky ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
二、Nginx的平滑升级 2.1、平滑升级的流程
①将旧Nginx二进制文件换成新Nginx程序文件(注意先备份)
②向master进程发送USR2信号启动新nginx进程
③master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
④master进程用新Nginx文件启动新master进程及worker子进程成为旧master的子进程,系统中将有新旧两个Nginx主进程和对应的worker子进程并存
当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进程的PID存放至新生成的pid文件nginx.pid
⑤向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止
⑥向旧master进程发送QUIT信号,关闭旧master,并删除Nginx.pid.oldbin文件
⑦如果发现升级有问题,可以回滚∶向旧master发送HUP,向新master发送QUIT
2.2、将旧Nginx二进制文件换成新Nginx程序文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] [root@Rocky ~] anaconda-ks.cfg nginx-1.23.2 nginx-1.23.2.tar.gz [root@Rocky ~] [root@Rocky nginx-1.23.2] nginx version: nginx/1.22.1 built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC) built with OpenSSL 1.1.1k FIPS 25 Mar 2021 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module [root@Rocky nginx-1.23.2] [root@Rocky nginx-1.23.2] [root@Rocky nginx-1.23.2] auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src [root@Rocky nginx-1.23.2] nginx version: nginx/1.23.2 [root@Rocky nginx-1.23.2] -rwxr-xr-x 1 root root 7587600 Nov 27 16:56 /apps/nginx/sbin/nginx -rwxr-xr-x 1 root root 7630304 Nov 27 17:11 objs/nginx [root@Rocky nginx-1.23.2] [root@centos ~] [root@centos ~] HTTP/1.1 200 OK Server: nginx/1.22.1 [root@Rocky nginx-1.23.2]cp : overwrite '/apps/nginx/sbin/nginx' ? y [root@Rocky nginx-1.23.2] -rwxr-xr-x 1 nginx nginx 7630280 Nov 27 16:21 /apps/nginx/sbin/nginx [root@centos ~] HTTP/1.1 200 OK Server: nginx/1.22.1 [root@Rocky nginx-1.23.2] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
2.3、向master进程发送USR2信号启动新nginx进程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@Rocky ~] root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process [root@Rocky nginx-1.23.2] [root@Rocky nginx-1.23.2] 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 [root@Rocky ~] root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process root 33023 0.0 0.0 42584 852 ? Ss 16:31 0:00 \_nginx: master process nginx nginx 33024 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process
2.4、向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@Rocky nginx-1.23.2] [root@Rocky ~] root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process is shutting down root 33023 0.0 0.0 42584 852 ? Ss 16:31 0:00 \_nginx: master process nginx nginx 33024 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process [root@centos ~] HTTP/1.1 200 OK Server: nginx/1.23.2
2.5、此时新旧共存,测试一段时间新版本是否正常运行 2.5.1、正常运行 1 2 3 4 [root@Rocky nginx-1.23.2] [root@Rocky nginx-1.23.2] nginx version: nginx/1.23.2
2.5.2、不能正常运行 1 2 3 4 5 6 7 8 9 10 11 [root@Rocky nginx-1.23.2] [root@centos ~] HTTP/1.1 200 OK Server: nginx/1.22.1 [root@Rocky nginx-1.23.2] [root@Rocky nginx-1.23.2]
三、多虚拟主机 基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块
ngx_http_core_module实现。
3.1、新建一个PC web站点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [root@Rocky ~] [root@Rocky ~] <h1> www.magedu.org </h1> :wq [root@Rocky ~] [root@Rocky ~] include /apps/nginx/conf.d/*.conf; [root@Rocky ~] [root@Rocky conf.d] server { listen 80; server_name www.magedu.org; location / { root /data/nginx/html/pc; } } :wq server { listen 80; server_name www.magedu.org; root /data/nginx/html/pc; } [root@Rocky conf.d] [root@DNS ~]$TTL 1D @ IN SOA master admin.magedu.org ( 6 1D 10M 1D 6H ) NS master NS slave1 master A 10.0.0.128 slave1 A 10.0.0.184 www A 10.0.0.184 :wq [root@DNS ~] server reload successful [root@client ~] DNS1=10.0.0.128 :wq [root@client ~] [root@client ~] [root@client ~] <h1> www.magedu.org </h1>
3.2、新建一个Mobile web站点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 [root@Rocky ~] [root@Rocky ~] <h1> m.magedu.org </h1> :wq [root@Rocky ~] [root@Rocky ~] include /apps/nginx/conf.d/*.conf; [root@Rocky ~] [root@Rocky conf.d] server { listen 80; server_name m.magedu.org; location / { root /data/nginx/html/pc; } } :wq [root@Rocky conf.d] [root@DNS ~]$TTL 1D @ IN SOA master admin.magedu.org ( 6 1D 10M 1D 6H ) NS master NS slave1 master A 10.0.0.128 slave1 A 10.0.0.184 m A 10.0.0.184 :wq [root@DNS ~] server reload successful [root@client ~] DNS1=10.0.0.128 :wq [root@client ~] [root@client ~] [root@client ~] <h1> m.magedu.org </h1>
3.3、还可以在pc站点下再新建一个子目录 3.3.1、在/data/nginx/html/pc新建about 1 2 3 4 5 [root@Rocky ~] [root@Rocky pc] [root@Rocky pc] <h1> about www.magedu.org </h1> :wq
3.3.2、在新的目录下创建about 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@Rocky ~] [root@Rocky ~] [root@Rocky about] <h1> /opt/pc/about/index.html </h1> :wq [root@Rocky ~] server { listen 80; server_name www.magedu.org; root /data/nginx/html/pc; location /about { alias /opt/pc/about; } } :wq
四、区分不同网站的访问日志,及设置日志的类型 4.1、自定义日志类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 [root@Rocky ~] [root@Rocky logs] access.log error.log nginx.pid [root@Rocky logs] http { ..... log_format testlog '$remote_addr [$time_local] "$request" "$http_referer" $status ' ; ..... } :wq [root@Rocky logs] server { listen 80; server_name www.magedu.org; root /data/nginx/html/pc; access_log /apps/nginx/logs/access-www.magedu.org.log testlog; location /about { alias /opt/pc/about; } } :wq [root@Rocky logs] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky logs] [root@Rocky logs] access.log access-www.magedu.org.log error.log nginx.pid
4.2、把日志格式设置成json格式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 [root@Rocky logs] log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}' ; :wq [root@Rocky logs] server { listen 80; server_name www.magedu.org; root /data/nginx/html/pc; access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; location /about { alias /opt/pc/about; } } :wq [root@Rocky logs] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky logs] [root@Rocky logs] access-json-www.magedu.org.log access.log access-www.magedu.org.log error.log nginx.pid [root@client ~] <h1> www.magedu.org </h1> [root@Rocky logs] {"@timestamp" :"2022-11-28T13:28:46+08:00" ,"host" :"10.0.0.184" ,"clientip" :"10.0.0.185" ,"size" :26,"responsetime" :0.000,"upstreamtime" :"-" ,"upstreamhost" :"-" ,"http_host" :"www.magedu.org" ,"uri" :"/index.html" ,"xff" :"-" ,"referer" :"-" ,"tcp_xff" :"-" ,"http_user_agent" :"curl/7.61.1" ,"status" :"200" } [root@Rocky logs] [root@Rocky logs] { "@timestamp" : "2022-11-28T13:28:46+08:00" , "host" : "10.0.0.184" , "clientip" : "10.0.0.185" , "size" : 26, "responsetime" : 0.000, "upstreamtime" : "-" , "upstreamhost" : "-" , "http_host" : "www.magedu.org" , "uri" : "/index.html" , "xff" : "-" , "referer" : "-" , "tcp_xff" : "-" , "http_user_agent" : "curl/7.61.1" , "status" : "200" }
五、开启流量监控 5.1、开启Nginx的状态页 基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 –with-http_stub_status_module,否则配置完成之后监测会是提示语法错误
注意 : 状态页显示的是整个服务器的状态,而非虚拟主机的状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [root@Rocky ~] nginx version: nginx/1.20.2 built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC) built with OpenSSL 1.1.1k FIPS 25 Mar 2021 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module [root@Rocky ~] location /nginx_status { stub_status; } :wq [root@Rocky ~] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky ~] [root@client ~] Active connections: 1 server accepts handled requests 33 33 29 Reading: 0 Writing: 1 Waiting: 0 accepts: handled: worker_connections限制等被拒绝的连接 requests: Reading: Writing: Waiting: active – (reading+writing) [root@client ~] 0 1 0
5.2、开启Nginx账户认证功能 由 ngx_http_auth_basic_module 模块提供此功能,此模块为默认模块
另外设置账号密码,借助于apache的工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 [root@Rocky ~] [root@Rocky ~] /usr/bin/ab /usr/bin/htdbm /usr/bin/htdigest /usr/bin/htpasswd /usr/bin/httxt2dbm /usr/bin/logresolve /usr/lib/.build-id /usr/lib/.build-id/83 /usr/lib/.build-id/83/e9641f1054bcf7ce84912e496b7d7004e541d8 /usr/lib/.build-id/94/c8074f3944d2d827c17b555b26ddd231ce6d40 /usr/lib/.build-id/b8/8f3b9720fa23977b4d7a8de7fb789e300c2bb3 /usr/lib/.build-id/cb /usr/lib/.build-id/cb/71e4ce4ee4e05e390dc66bdd9b290b78dd595b /usr/lib/.build-id/ec /usr/lib/.build-id/ec/f49ee37342186615ecf97b8224c3f74a0b2252 /usr/lib/.build-id/fb /usr/lib/.build-id/fb/cadf8908e128a1bfc4253b351fc06595960b37 /usr/share/doc/httpd-tools /usr/share/doc/httpd-tools/LICENSE /usr/share/doc/httpd-tools/NOTICE /usr/share/man/man1/ab.1.gz /usr/share/man/man1/htdbm.1.gz /usr/share/man/man1/htdigest.1.gz /usr/share/man/man1/htpasswd.1.gz /usr/share/man/man1/httxt2dbm.1.gz /usr/share/man/man1/logresolve.1.gz [root@Rocky ~] htpasswd: illegal option -- h Usage: htpasswd [-cimB25dpsDv] [-C cost] [-r rounds] passwordfile username htpasswd -b[cmB25dpsDv] [-C cost] [-r rounds] passwordfile username password htpasswd -n[imB25dps] [-C cost] [-r rounds] username htpasswd -nb[mB25dps] [-C cost] [-r rounds] username password -c Create a new file. -n Don't update file; display results on stdout. -b Use the password from the command line rather than prompting for it. [root@Rocky ~]#htpasswd -bc /apps/nginx/conf.d/.nginx-user zhang 123456 Adding password for user zhang # 存放路径 账号 密码 [root@Rocky ~]#cat /apps/nginx/conf.d/.nginx-user zhang:$apr1$xstp1TOk$IRjBaDIckZXRt3hVdQgBW0 #创建第二个账号时,命令为htpasswd -b /apps/nginx/conf.d/.nginx-user 账号 密码。如果+c是替换旧账号,不+c是增加新账号 #进行安全加固 [root@Rocky ~]#chown nginx.nginx /apps/nginx/conf.d/.nginx-user [root@Rocky ~]#chmod 600 /apps/nginx/conf.d/.nginx-user [root@Rocky ~]#vim /apps/nginx/conf.d/pc.conf location /nginx_status { stub_status; auth_basic "waring"; #waring只是提示词,可以随意写 auth_basic_user_file /apps/nginx/conf.d/.nginx-user; } :wq #注意auth_basic和auth_basic_user_file可以放在http(所有网站)、server(其中一个网站)、location(网站中的1个页面) [root@Rocky ~]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky ~]#nginx -s reload
5.3、Nginx 四层访问控制 访问控制基于模块ngx_http_access_module(默认模块)实现,可以通过匹配客户端源IP地址进行限制
注意 : 如果能在防火墙设备控制,最好就不要在nginx上配置,可以更好的节约资源
官方帮助:
1 http://nginx.org/en/docs/http/ngx_http_access_module.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [root@Rocky ~] location /nginx_status { stub_status; auth_basic "waring" ; auth_basic_user_file /apps/nginx/conf.d/.nginx-user; deny 10.0.0.185; allow 10.0.0.0/24; } :wq [root@Rocky ~] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky ~] [root@client ~] <html> <head ><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.20.2</center> </body> </html> [root@Rocky ~] Active connections: 1 server accepts handled requests 46 46 50 Reading: 0 Writing: 1 Waiting: 0
六、安装第三方echo模块实现信息显示 开源的echo模块可以用来打印信息,变量等
1 https://github.com/openresty/echo-nginx-module
6.1、安装echo模块 1 2 3 4 5 6 7 8 9 10 11 [root@Rocky ~] [root@Rocky src] [root@Rocky src] nginx-1.20.2 nginx-1.20.2.tar.gz nginx-module-vts-0.2.1 v0.2.1.tar.gz v0.63.tar.gz [root@Rocky src] [root@Rocky src] echo-nginx-module-0.63 nginx-1.20.2 nginx-1.20.2.tar.gz nginx-module-vts-0.2.1 v0.2.1.tar.gz v0.63.tar.gz [root@Rocky nginx-1.20.2] [root@Rocky nginx-1.20.2] [root@Rocky nginx-1.20.2] [root@Rocky nginx-1.20.2]
6.2、设置配置文件 Nginx里的变量
1 http://nginx.org/en/docs/varindex.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@Rocky ~] location /echo { set $class n68; echo $class ; echo "hello" ; echo $remote_addr ; echo $uri ; } :wq [root@Rocky ~] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@Rocky ~] [root@client ~] n68 hello 10.0.0.185 /echo
七、nginx的反向代理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 环境配置: 10.0.0.184 为反向代理服务器 10.0.0.185 为后端服务器 [root@server conf.d] server { listen 80; server_name www.magedu.org; root /data/nginx/html/pc; access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; location / { proxy_pass http://10.0.0.185; } } :wq [root@server conf.d] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@server conf.d] [root@Rocky nginx] 10.0.0.184 - - [29/Nov/2022:13:20:49 +0800] "GET / HTTP/1.0" 200 607 "-" "curl/7.61.1"
八、nginx的负载均衡 Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
1 https://nginx.org/en/docs/http/ngx_http_upstream_module.html
8.1、http upstream配置参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 upstream name { server ..... ...... } upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com backup; } server address [parameters]; weight=number max_conns=number max_fails=number 多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性 检查,而非周期性的探测 fail_timeout=time 进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒 backup down
8.2、负载均衡参数设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@proxy ~] [root@proxy conf.d] upstream webserver { server 10.0.0.185; server 10.0.0.186; } server { listen 80; server_name www.magedu.org; access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; root /data/nginx/html/pc; location / { proxy_pass http://webserver; } } :wq [root@proxy conf.d] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@proxy conf.d]
九、实现nginx四层负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。
如果编译安装,需要指定 –with-stream 选项才能支持ngx_stream_proxy_module模块
1 2 3 官方文档: https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html
9.1、tcp负载均衡配置参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 stream { upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; } upstream dns { server 10.0.0.1:53535; server dns.example.com:53; } server { listen 12345; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass backend; } server { listen 127.0.0.1:53 udp reuseport; proxy_timeout 20s; proxy_pass dns; } server { listen [::1]:12345; proxy_pass unix:/tmp/stream.socket; } }
9.2、实现负载均衡(redis和mysql类似)
9.2.1、后端服务器安装redis 1 2 3 4 5 6 [root@centos8 ~] [root@centos8 ~] [root@centos8 ~] [root@centos8 ~] LISTEN 0 128 *:6379 *:*
9.2.2、反向代理nginx配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@centos8 ~] include /apps/nginx/conf/tcp/tcp.conf; [root@centos8 ~] [root@centos8 ~] stream { upstream redis_server { server 10.0.0.18:6379 max_fails=3 fail_timeout=30s; server 10.0.0.28:6379 max_fails=3 fail_timeout=30s; } server { listen 10.0.0.8:6379; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass redis_server; } } [root@centos8 ~] [root@centos8 ~] LISTEN 0 128 10.0.0.8:6379 *:* [root@centos8 ~] OK [root@centos8 ~] (nil) [root@centos8 ~]"wang"
十、实现FastCGI(异构代理) 前面的代理都属于同构代理
nginx缺通过与第三方基于协议实现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户的请求,处理完成后返回数据给Nginx并回收进程 ,最后nginx在返回给客户端,那这个约定就是通用网关接口(common gateway interface,简称CGI),CGI(协议) 是web服务器和外部应用程序之间的接口标准,是cgi程序和web服务器之间传递信息的标准化接口。
Nginx基于模块ngx_http_fastcgi_module实现通过fastcgi协议将指定的客户端请求转发至php-fpm处
理,其配置指令如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 fastcgi_pass address; fastcgi_pass localhost:9000; fastcgi_pass unix:/tmp/fastcgi.socket; fastcgi_index name; fastcgi_param parameter value [if_not_empty]; fastcgi_param REMOTE_ADDR $remote_addr ; fastcgi_param REMOTE_PORT $remote_port ; fastcgi_param SERVER_ADDR $server_addr ; fastcgi_param SERVER_PORT $server_port ; fastcgi_param SERVER_NAME $server_name ; Nginx默认配置示例: location ~ \.php$ { root /scripts; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ; 本路径 include fastcgi_params; }
10.1、 FastCGI实战案例: Nginx与php不在同一个服务器 nginx会处理静态请求,但是会转发动态请求到后端指定的php-fpm服务器,因此php代码需要放在后端
的php-fpm服务器,即静态页面放在Nginx服务器上,而动态页面放在后端php-fpm服务器,通常情况
下,一般都是采用在同一个服务器
10.1.1、php-fpm的配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 [root@php-fpm ~] [root@php-fpm ~] /etc/httpd/conf.d/php.conf /etc/logrotate.d/php-fpm /etc/nginx/conf.d/php-fpm.conf /etc/nginx/default.d/php.conf /etc/php-fpm.conf /etc/php-fpm.d /etc/php-fpm.d/www.conf /etc/systemd/system/php-fpm.service.d /run/php-fpm /usr/lib/.build-id /usr/lib/.build-id/eb /usr/lib/.build-id/eb/2f5f408539a6608c445913b3214d215b421869 /usr/lib/systemd/system/httpd.service.d/php-fpm.conf /usr/lib/systemd/system/nginx.service.d/php-fpm.conf /usr/lib/systemd/system/php-fpm.service /usr/sbin/php-fpm /usr/share/doc/php-fpm /usr/share/doc/php-fpm/php-fpm.conf.default /usr/share/doc/php-fpm/www.conf.default /usr/share/fpm /usr/share/fpm/status.html /usr/share/licenses/php-fpm /usr/share/licenses/php-fpm/fpm_LICENSE /usr/share/man/man8/php-fpm.8.gz /var/lib/php/opcache /var/lib/php/session /var/lib/php/wsdlcache /var/log/php-fpm [root@php-fpm ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@php-fpm ~] [root@php-fpm ~] root 1870 0.0 0.9 166940 17744 ? Ss 11:06 0:00 php-fpm: master process (/etc/php-fpm.conf) apache 1871 0.0 0.5 183288 9896 ? S 11:06 0:00 \_ php-fpm: pool www apache 1872 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www apache 1873 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www apache 1874 0.0 0.5 183288 9824 ? S 11:06 0:00 \_ php-fpm: pool www apache 1875 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www [root@php-fpm ~] ; RPM: apache user chosen to provide access to the same directories as httpd user = nginx ; RPM: Keep a group allowed to write in log dir . group = nginx [root@php-fpm ~] [root@php-fpm ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@php-fpm ~] ; Note: This value is mandatory. ;listen = /run/php-fpm/www.sock listen = 9000 [root@php-fpm ~] [root@php-fpm ~] State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:9000 *:* [root@php-fpm ~] ;Default Value: any ;listen.allowed_clients = 127.0.0.1 [root@php-fpm ~] [root@php-fpm ~] [root@php-fpm ~] [root@php-fpm php] <?php phpinfo(); ?> :wq
10.1.2、反向代理nginx的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@proxy conf.d] server { listen 80; server_name www.magedu.org; access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; root /data/nginx/html/pc; location / { proxy_pass http://webserver; } location ~ \.php$ { root /data/php; fastcgi_pass 10.0.0.185:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ; include fastcgi_params; } } :wq [root@proxy conf.d] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@proxy conf.d]
10.1.3、php-fpm也可以开启状态页和ping 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@php-fpm php] ; Default Value: not set pm.status_path = /fpm_status ..... ; Default Value: not set ping.path = /ping .... ; Default Value: pong ping.response = pong :wq [root@php-fpm php] [root@proxy conf.d] server { ...... location ~ ^/(ping|fpm_status)$ { fastcgi_pass 10.0.0.185:9000; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name ; include fastcgi_params; } } [root@proxy conf.d]
十一、项目实战:利用LNMP实现WordPress站点搭建
10.1、LNP的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 [root@LNP ~] [root@LNP ~] /etc/httpd/conf.d/php.conf /etc/logrotate.d/php-fpm /etc/nginx/conf.d/php-fpm.conf /etc/nginx/default.d/php.conf /etc/php-fpm.conf /etc/php-fpm.d /etc/php-fpm.d/www.conf /etc/systemd/system/php-fpm.service.d /run/php-fpm /usr/lib/.build-id /usr/lib/.build-id/eb /usr/lib/.build-id/eb/2f5f408539a6608c445913b3214d215b421869 /usr/lib/systemd/system/httpd.service.d/php-fpm.conf /usr/lib/systemd/system/nginx.service.d/php-fpm.conf /usr/lib/systemd/system/php-fpm.service /usr/sbin/php-fpm /usr/share/doc/php-fpm /usr/share/doc/php-fpm/php-fpm.conf.default /usr/share/doc/php-fpm/www.conf.default /usr/share/fpm /usr/share/fpm/status.html /usr/share/licenses/php-fpm /usr/share/licenses/php-fpm/fpm_LICENSE /usr/share/man/man8/php-fpm.8.gz /var/lib/php/opcache /var/lib/php/session /var/lib/php/wsdlcache /var/log/php-fpm [root@LNP ~] ; RPM: apache user chosen to provide access to the same directories as httpd user = nginx ; RPM: Keep a group allowed to write in log dir . group = nginx ; listen = /run/php-fpm/www.sock listen = 127.0.0.1:9000 ..... ; Default Value: not set pm.status_path = /fpm_status ..... ; Default Value: not set ping.path = /ping .... ; Default Value: pong ping.response = pong :wq [root@LNP ~] [root@LNP ~] server { listen 80; server_name www.magedu.org; access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; root /data/nginx/html/pc; location / { proxy_pass http://webserver; } location ~ \.php$ { root /data/nginx/html/pc; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ; include fastcgi_params; } location ~ ^/(ping|fpm_status)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name ; include fastcgi_params; } } :wq [root@LNP ~] nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@LNP ~] [root@LNP ~] [root@LNP ~] anaconda-ks.cfg install_nginx.sh latest-zh_CN.tar.gz [root@LNP ~] [root@LNP ~] anaconda-ks.cfg install_nginx.sh latest-zh_CN.tar.gz wordpress [root@LNP ~] [root@LNP ~]
10.2、数据库mysql的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@Rocky ~] [root@Rocky ~] Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service. [root@Rocky ~] mysql> create database wordpress; Query OK, 1 row affected (0.01 sec) mysql> create user wordpress@'10.0.0.%' identified by '123456' ; Query OK, 0 rows affected (0.01 sec) mysql> grant all on wordpress.* to wordpress@'10.0.0.%' ; Query OK, 0 rows affected (0.00 sec)
十二、