大家公司的会话保持怎么作的?php
一、开发作的:记录用户登录的状态,将用户登录状态保存到,redis服务器中,nfs,mysql。mysql
记录用户的登录状态。nginx
经过登录用户对应的userid和cookie结合,记录用户的状态;若是是保存在本地,那么在其它用户使用该机器的时候去更改cookie的userid就能登录其它用户的登录状态,这样很不安全。git
这样就引入了session。github
二、运维:由于开发没有写会话保持功能,咱们公司使用的是nginx的upstream模块的ip_hash调度算法。redis
什么是session算法
session保存在服务端sql
什么是cookie?后端
保存在客户端安全
# 一、下载源码包 [root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz # 二、解压 [root@lb01 ~]# tar -xf nginx-1.14.2.tar.gz # 三、查看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' # 四、安装依耐 [root@lb01 ~/nginx-1.14.2]# yum -y install openssl-devel # 五、生成 [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' # 六、编译安装 [root@lb01 ~/nginx-1.14.2]# make && make install # 七、建立软连接 [root@lb01 ~]# ln -s /app/nginx-1.14.2/ /app/nginx # 八、添加环境变量并生效 [root@lb01 ~]# vi /etc/profile.d/nginx.sh export PATH="/app/nginx/sbin:$PATH" [root@lb01 ~]# source /etc/profile # 九、编辑配置文件 [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; # 十、放网站配置文件的地方 include conf.d/*.conf; } ## 十一、建立一个配置文件的目录。 [root@lb01 /app/nginx/conf]# mkdir conf.d # 十二、配置一个负载均衡 [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; # 十一、启动nginx;访问测试,目前是1.14.2的版本
在不影响用户体验的状况下把nginx的版本升级到nginx-1.16.1的版本,并添加nginx负载均衡的健康检查模块。
# 一、下载1.16.1版本的额包 [root@lb01 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz # 二、下载补丁包 [root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip # 三、解压nginx [root@lb01 ~]# tar -xf nginx-1.16.1.tar.gz # 四、解压master扩展包 [root@lb01 ~]# unzip master.zip # 五、安装打补丁的命令 [root@lb01 ~]# yum -y install patch # 六、打补丁 [root@lb01 ~/nginx-1.16.1]# patch -p1 < ../nginx_upstream_check_module-master/check_1.16.1+.patch # 七、生成,指定安装模块 [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 # 八、编译安装 [root@lb01 ~/nginx-1.16.1]# make && make install # 九、安装好的目录下生成的文件 [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 # 十、复制必要的文件 ## 主配置文件 [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/ # 十一、建立新的连接文件 [root@lb01 /app]# rm -fr nginx && ln -s /app/nginx-1.16.1/ nginx # 十二、秒级重启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; } }