lamp架构-nginx常见配置(高速缓存memcache+并发优化+平滑升级与回退+负载均衡)

1. nginx高速缓存memcache

1.1 设置高速缓存前
[root@server11 nginx]# cd html/
[root@server11 html]# pwd
/usr/local/openresty/nginx/html
[root@server11 html]# cp ~/memcache-4.0.5.2/example.php . ##将测试页放置到openresty中nginx的默认发布目录下
[root@server11 html]# ls
50x.html  example.php  index.html
[root@foundation Desktop]# ab -c10 -n5000 http://192.168.0.11/example.php

此时采用的是传统的缓存策略:
在这里插入图片描述

在这里插入图片描述

1.2 设置高速缓存
[root@server11 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server11 conf]# vim nginx.conf
  http {
          upstream memcache {
          server 127.0.0.1:11211;
          keepalive 512;
          }

          location /memc {
          internal;                  
          memc_connect_timeout 100ms;
          memc_send_timeout 100ms;
          memc_read_timeout 100ms;
          set $memc_key $query_string;    
          set $memc_exptime 300;
          memc_pass memcache;
          }

          location ~ \.php$ {
              set $key $uri$args;        
              srcache_fetch GET /memc $key; ##现在memcache中找,如果有则直接返回,如果没有再通过fastcgi去后端找
              srcache_store PUT /memc $key; ##将在后端找到的值存储到memcache中,下次再请求的时候可以直接返回     
              root           html;
              fastcgi_pass   127.0.0.1:9000;
              fastcgi_index  index.php;
              #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
              include        fastcgi.conf;
          }

[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -t  ##检测是否有语法错误
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload  ##重新加载
[root@foundation Desktop]# ab -c10 -n5000 http://192.168.0.11/example.php

此时采用的是高速缓存策略:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.3 进行压力测试

在这里插入图片描述

2. nginx并发优化

[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -s stop
[root@server11 ~]# systemctl start nginx.service  ##停止openresty中的nginx,开启原生nginx
[root@server11 ~]# cd /usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf
3 worker_processes  2;  ## 工作进程数(最好将其设置为可用的CPU内核数)或者,可以将其设置为auto。 这样nginx会自动根据核心数为生成对应数量的worker进程。
4 worker_cpu_affinity 01 10;
14     use epoll;
15     worker_connections  65535;  ##单个工作进程并发连接数
[root@server11 conf]# nginx -s reload
[root@server11 conf]# cd /etc/pam.d/
[root@server11 pam.d]# cd ..
[root@server11 etc]# vim /etc/security/limits.conf 
 62 nginx   -       nofile          65535
 
[root@server12 ~]# systemctl start httpd
[root@server12 ~]# cd /var/www/html/
[root@server12 html]# ls
[root@server12 html]# echo server12 > index.html
[root@server12 html]# curl localhost
server12

[root@server13 ~]# systemctl start httpd
[root@server13 ~]# cd /var/www/html/
[root@server13 html]# echo server13 > index.html
[root@server13 html]# curl localhost
server13

01表示启用第一个CPU内核,10表示启用第二个CPU内核;
使用epoll模型
在这里插入图片描述
修改用户进程可打开文件数限制
在这里插入图片描述

[root@server11 conf]# pwd
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf  ## 编辑nginx配置文件
 20     upstream westos {
 21     server 192.168.0.12:80;
 22     server 192.168.0.13:80;
 23     }

 49        # location / {
 50        #     root   html;
 51        #     index  index.php index.html index.htm;
 52        # }

 54        location / {
 55                proxy_pass http://westos;
 56        }
[root@server11 conf]# 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
[root@server11 conf]# nginx -s reload
[root@server11 ~]# curl 192.168.0.11
server13
[root@server11 ~]# curl 192.168.0.11
server12

定义两个server端
在这里插入图片描述
添加反向代理
在这里插入图片描述
负载均衡
在这里插入图片描述

3. nginx平滑升级与版本回退

3.1 平滑升级
[root@server11 ~]# tar zxf nginx-1.19.1.tar.gz 
[root@server11 ~]# cd nginx-1.19.1/
[root@server11 nginx-1.19.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@server11 nginx-1.19.1]# vim auto/cc/gcc  ## 注释debug选项
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@server11 nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@server11 nginx-1.19.1]# make  ##注意不要make install

[root@server11 objs]# cd /usr/local/nginx/sbin/
[root@server11 sbin]# mv nginx nginx.old
[root@server11 ~]# cd nginx-1.19.1/objs/
[root@server11 objs]# cp nginx /usr/local/nginx/sbin/ ##复制新版nginx到相关目录下
[root@server11 objs]# cd /usr/local/nginx/sbin/
[root@server11 sbin]# ls
nginx  nginx.old
[root@server11 sbin]# ./nginx.old -v
nginx version: nginx/1.18.0
[root@server11 sbin]# ./nginx -v
nginx version: nginx/1.19.1
[root@server11 sbin]# curl -I localhost
Server: nginx/1.18.0

[root@server11 sbin]# ps ax | grep nginx
[root@server11 sbin]# kill -USR2 18678  ##升级新程序
[root@server11 sbin]# kill -WINCH 18678  ##关闭原worker进程但保留主进程:为了回退
[root@server11 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.19.1

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.2 版本回退
[root@server11 sbin]# ls
nginx  nginx.old
[root@server11 sbin]# mv nginx nginx.new  ##还原nginx程序
[root@server11 sbin]# mv nginx.old nginx
[root@server11 sbin]# ls
nginx  nginx.new
[root@server11 sbin]# kill -HUP 18678  ##唤醒原进程
[root@server11 sbin]# kill -WINCH  26925  ##回收新版本的worker进程
[root@server11 sbin]# curl -I localhost 
Server: nginx/1.18.0

在这里插入图片描述

4. nginx虚拟主机负载均衡

构建nginx虚拟主机,可以使在访问同一台主机不同域名的时候访问不同的页面

[root@server11 conf]# pwd
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf  ##编辑配置文件添加相应设置
 server {
         listen 80;
         server_name     www.westos.org;
 
         location / {
         proxy_pass http://westos;
         }
 }
[root@server11 conf]# nginx -s reload
[root@server11 conf]# kill 18678 26925 27294 27295 27776 27777
[root@server11 conf]# nginx 
[root@server11 conf]# curl localhost
[root@foundation file_recv]# vim /etc/hosts
192.168.0.11 server11 www.westos.org www.linux.org ##主机添加解析

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

[root@server11 conf]# vim nginx.conf
 server {
         listen 80;
         server_name     www.linux.org;
         
         location / {  
                 root  /web1;
                 index   index.html;
         }
 }
 }
[root@server11 conf]# 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
[root@server11 conf]# nginx -s reload
[root@server11 conf]# mkdir /web1
[root@server11 conf]# cd /web1/
[root@server11 web1]# echo web1 > index.html ##编辑默认发布页面

[root@foundation file_recv]# ping www.linux.org
192.168.0.11 server11 www.westos.org  www.linux.org
[root@foundation file_recv]# curl www.linux.org ##访问www.linux.org 显示web1测试页面
web1
[root@foundation file_recv]# curl www.westos.org ## 访问www.westos.org 负载均衡
server12
[root@foundation file_recv]# curl www.westos.org
server13

在这里插入图片描述
在这里插入图片描述

5. 添加ssl

[root@server11 web1]# cd -
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf  ##编辑配置文件
[root@server11 conf]# cd /etc/pki//tls/certs/ ##在相关目录下创建cert.pem文件
[root@server11 certs]# ls
[root@server11 certs]# make cert.pem  ##创建证书与key的相关文件
[root@server11 certs]# mv cert.pem /usr/local/nginx/conf/ ##将文件移动到nginx配置文件所在目录中
[root@server11 certs]# nginx -t 
[root@server11 certs]# nginx -s reload
[root@server11 certs]# netstat -antlp ##443端口开启
[root@foundation file_recv]# curl 192.168.0.11
[root@foundation file_recv]# curl -k https://192.168.0.11

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6. 权重与backup设置

[root@server11 conf]# vim nginx.conf
 22     server 192.168.0.12:80 weight=3;  62
[root@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13

当某台服务器性能较好时可以适当增加权重
在这里插入图片描述
在这里插入图片描述

[root@server12 html]# systemctl stop httpd
[root@server13 html]# systemctl stop httpd
[root@foundation ~]# curl www.westos.org
<head><title>502 Bad Gateway</title></head>

[root@server11 conf]# vim nginx.conf
 24    server 127.0.0.1:80 backup;    
[root@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
<title>Welcome to nginx!</title>

[root@server12 html]# systemctl start httpd
[root@server13 html]# systemctl start httpd
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13

当两台后端服务器都挂掉后访问不会报错,会返回nginx测试页。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7. 添加算法

7.1 添加ip_hash算法:

源地址不变,则后端调度不变,同一个ip过来的请求会发往同一个后端,不会改变。

[root@server11 conf]# vim nginx.conf
 21     ip_hash;
[root@server11 conf]# 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

[root@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12

注意:添加ip_hash时要注释掉backup选项,因为backup不支持ip_hash算法,同时开启会报错。
在这里插入图片描述

在这里插入图片描述
后端调度不变
在这里插入图片描述

7.2 添加sticky算法

客户访问nginx大致流程:client > cdn加速(反向代理机制)> nginx ip_hash
依照上述访问流程,nginx拿到的是cdn的ip地址,因此此时再加入ip_hash算法就不太合适了,因为nginx此时获得的ip可能不会发生变化,那么ip_hash算法也就失去了其意义,当用户直接访问server端时适合采用ip_hash算法

我们可以采用cookie的方式来解决这个问题:客户端通过浏览器访问server端,server会给每个客户端返回一个cookie,cookie值在浏览器中会缓存,只要不关闭网页或浏览器,这个cookie值会一直保存在缓存区域,之后的请求浏览器可以拿着这个cookie去请求,当server端识别到cookie值后会去匹配内存中的相关session,然后可以达到访问同一台后端的目的

[root@server11 ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
[root@server11 ~]# cd nginx-1.18.0/
[root@server11 nginx-1.18.0]# make clean 
rm -rf Makefile objs

[root@server11 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42  ##添加相关模块重新编译
[root@server11 nginx-1.18.0]# make
[root@server11 nginx-1.18.0]# cd objs/
[root@server11 objs]# nginx -s stop
[root@server11 objs]# cp nginx /usr/local/nginx/sbin/ -f 
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y ##复制重新编译好的nginx到相关目录下
[root@server11 objs]# nginx -v
nginx version: nginx/1.18.0
[root@server11 objs]# nginx
[root@server11 objs]# cd /usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf  ##编辑配置文件
[root@server11 conf]# 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
[root@server11 conf]# nginx -s reload
 22     sticky;
 [root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13
 

在这里插入图片描述在这里插入图片描述
使用curl的方式仍能访问到不同的后端,因为上述基于cookie的算法只在浏览器中生效。
在这里插入图片描述
如果不清除浏览器缓存,访问到的始终是一个后端。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值