1.下载nginx 源码包,下载地址:http://nginx.org
2.使用远程工具上传至相应的服务器(putty、xshell)
3.解压
示例:[root@shuaishuai ~]# tar -xf nginx-1.19.5.tar.gz
4.切换目录
示例:[root@shuaishuai ~]# cd nginx-1.19.5
5.查看之前nginx所安装的模块
示例:[root@shuaishuai nginx-1.19.5]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.10built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module
6.预配置新版本nginx,且进行编译
示例: [root@shuaishuai nginx-1.19.5]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module && make
注:安装路径需与旧版一致,模块也需要与旧版的一致;此处千万不能执行make install,执行install后会修改系统默认的配置文件
7.备份旧二进制文件nginx
示例:[root@shuaishuai nginx-1.19.5]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
8.用新的二进制文件nginx替换旧的
示例:[root@shuaishuai nginx-1.19.5]# cp -p objs/nginx /usr/local/nginx/sbin/nginx
9. 确保配置文件无误
示例:[root@shuaishuai nginx-1.19.5]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
10. 向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21993 0.0 0.2 112724 996 pts/1 S+ 20:42 0:00 grep --color=auto nginx
[root@shuaishuai nginx-1.19.5]# kill -USR2 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
root 21997 0.0 0.2 112724 996 pts/1 R+ 20:44 0:00 grep --color=auto nginx
11. 向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@shuaishuai nginx-1.19.5]# kill -WINCH 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
注:
如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程, 仍使用旧版配置文件。然后可以将新版Nginx进程杀死(使用QUIT、TERM、或者KILL)
[root@shuaishuai nginx-1.19.5]# kill -HUP 19658
12.升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出
[root@shuaishuai nginx-1.19.5]# kill -QUIT 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
13.验证nginx版本号
[root@shuaishuai nginx-1.19.5]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.19.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module
————————————————
版权声明:本文为CSDN博主「帅帅先生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_50512016/article/details/110139860
如何隐藏nginx版本号:
[root@localhost /]# vi /etc/nginx/nginx.conf [root@localhost /]# nginx -t [root@localhost /]# nginx -s reload
配置文件中,http区段中插入“server_tokens off;”,重新载入配置文件
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server_tokens off; expires 5s; sendfile on;
隐藏或修改nginx返回的Server信息:
[root@localhost /]# vi /opt/nginx/nginx-1.2.0/src/http/ngx_http_special_response.c 找到29行:修改展示名称(LLSERVER) [root@localhost /]# "<hr><center>LLSERVER</center>" CRLF [root@localhost /]# cd /opt/nginx/nginx-1.2.0/src/http/ngx_http_header_filter_module.c [root@localhost /]# vi /opt/nginx/nginx-1.2.0/src/http/ngx_http_header_filter_module.c 找到49和50行: 修改展示名称(Server: LLSERVER) [root@localhost /]# static char ngx_http_server_string[] = "Server: LLSERVER" CRLF; [root@localhost /]# static char ngx_http_server_full_string[] = "Server: LLSERVER " NGINX_VER CRLF;
隐藏nginx请求头header:修改nginx.conf ,在location中添加配置如下: [root@localhost /]# proxy_hide_header X-Frame-Options; //将X-Frame-Options换成Location [root@localhost /]# add_header X-Frame-Options SAMEORIGIN always; //将X-Frame-Options换成Location