nginx版本无缝升级与回滚

chookie和session

  • 你们公司的会话保持怎么做的?

    1、开发做的:记录用户登陆的状态,将用户登陆状态保存到,redis服务器中,nfs,mysql。

    ​ 记录用户的登陆状态。

    ​ 通过登陆用户对应的userid和cookie结合,记录用户的状态;如果是保存在本地,那么在其它用户使用该机器的时候去更改cookie的userid就能登陆其它用户的登陆状态,这样很不安全。

    ​ 这样就引入了session。

    2、运维:因为开发没有写会话保持功能,我们公司使用的是nginx的upstream模块的ip_hash调度算法。

  • 什么是session

    session保存在服务端

  • 什么是cookie?

    保存在客户端

源码安装nginx

# 1、下载源码包
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

# 2、解压
[root@lb01 ~]# tar -xf nginx-1.14.2.tar.gz

# 3、查看yum安装时所需要的模块
[root@lb01 ~/nginx-1.14.2]# nginx -V
--with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

# 4、安装依耐
[root@lb01 ~/nginx-1.14.2]# yum -y install openssl-devel

# 5、生成
[root@lb01 ~/nginx-1.14.2]# ./configure --prefix=/app/nginx-1.14.2 --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

# 6、编译安装
[root@lb01 ~/nginx-1.14.2]# make && make install

# 7、创建软链接
[root@lb01 ~]# ln -s /app/nginx-1.14.2/ /app/nginx

# 8、添加环境变量并生效
[root@lb01 ~]# vi /etc/profile.d/nginx.sh
export PATH="/app/nginx/sbin:$PATH"

[root@lb01 ~]# source /etc/profile

# 9、编辑配置文件
[root@lb01 /app/nginx/conf]# vi nginx.conf
...
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
# 10、放网站配置文件的地方
    include conf.d/*.conf;
}

## 11、创建一个配置文件的目录。
[root@lb01 /app/nginx/conf]# mkdir conf.d

# 12、配置一个负载均衡
[root@lb01 /app/nginx/conf/conf.d]# vi upstream.conf
upstream phpadmin {
        server 172.16.1.7;
        server 172.16.1.8;
}

server {
        listen 80;
        server_name php.gong.com;
        location / {
                proxy_pass http://phpadmin;
                include proxy_params;
        }
}

# ----------------------------# 
[root@lb01 /app/nginx/conf]# vi proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
 
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
 
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

# 11、启动nginx;访问测试,目前是1.14.2的版本

版本升级

在不影响用户体验的情况下把nginx的版本升级到nginx-1.16.1的版本,并添加nginx负载均衡的健康检查模块。

# 1、下载1.16.1版本的额包
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

# 2、下载补丁包
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 3、解压nginx
[root@lb01 ~]# tar -xf nginx-1.16.1.tar.gz

# 4、解压master扩展包
[root@lb01 ~]# unzip master.zip

# 5、安装打补丁的命令
[root@lb01 ~]# yum -y install patch

# 6、打补丁
[root@lb01 ~/nginx-1.16.1]# patch -p1 < ../nginx_upstream_check_module-master/check_1.16.1+.patch

# 7、生成,指定安装模块
[root@lb01 ~/nginx-1.16.1]# ./configure --prefix=/app/nginx-1.16.1 --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/nginx_upstream_check_module-master

# 8、编译安装
[root@lb01 ~/nginx-1.16.1]# make && make install

# 9、安装好的目录下生成的文件
[root@lb01 /app]# ll
total 0
lrwxrwxrwx  1 root root  18 May 28 22:02 nginx -> /app/nginx-1.14.2/
drwxr-xr-x 11 root root 151 May 28 22:10 nginx-1.14.2
drwxr-xr-x  6 root root  54 May 28 22:31 nginx-1.16.1

# 10、复制必要的文件
## 主配置文件
[root@lb01 /app]# cp nginx-1.14.2/conf/nginx.conf nginx-1.16.1/conf/nginx.conf
## 添加的配置文件
[root@lb01 /app]# cp nginx-1.14.2/conf/conf.d/upstream.conf nginx-1.16.1/conf/conf.d/upstream.conf
## 配置文件里面指定的优化参数 
[root@lb01 /app]# cp nginx-1.14.2/conf/proxy_params nginx-1.16.1/conf/proxy_params
## 复制pid
[root@lb01 /app]# cp nginx-1.14.2/logs/nginx.pid nginx-1.16.1/logs/

# 11、创建新的链接文件
[root@lb01 /app]# rm -fr nginx && ln -s /app/nginx-1.16.1/  nginx

# 12、秒级重启nginx;现在使用nginx的命令走的是/app/nginx-1.16.1/sbin/下的nginx;但是stop结束的是原来版本下的pid文件,从老版本复制过来的,所以实现了对原来版本的进程结束;通过&&在瞬间进行nginx新版本的启动,添加新的进程和pid
[root@lb01 /app]# nginx -s stop && nginx

访问测试的脚本

[root@db01 ~]# cat test.sh 
#!/bin/bash
while true;do
status=$(curl -I -m 10 -o /dev/null -s -w %{http_code} php.gong.com)
	if [ $status -eq 200 ];then
		echo "$(date +%F-%T)_访问成功"
	else
		echo "$(date +%F-%T)_访问失败"
	fi
sleep 2
done

健康检查的配置

[root@lb01 /app]# vi /app/nginx/conf/conf.d/upstream.conf 
upstream phpadmin {
        server 172.16.1.7;
        server 172.16.1.8;
        check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
             #interval  检测间隔时间,单位为毫秒
   	 		#rise      表示请求2次正常,标记此后端的状态为up
    		#fall      表示请求3次失败,标记此后端的状态为down
    		#type      类型为tcp
    		#timeout   超时时间,单位为毫秒
}

server {
        listen 80;
        server_name php.gong.com;
        location / {
                proxy_pass http://phpadmin;
                include proxy_params;
                }
        location /uc {
                check_status;
        }
        }



原文地址:https://www.cnblogs.com/gshelldon/p/13591945.html