美文网首页转载部分
nginx之proxy_cache缓存、upstream 分组的

nginx之proxy_cache缓存、upstream 分组的

作者: M36_tongwei | 来源:发表于2019-06-10 15:12 被阅读22次

实验一、nginx之proxy_cache缓存

1、准备环境
image.png

1.1 编译安装nginx

[root@centos7 ~]#vim install_nginx.sh 

yum -y install yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed                                                     
cd /data/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --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
make && make install
useradd nginx -s /sbin/nologin -u 2000
chown nginx.nginx -R /apps/nginx/
#/apps/nginx/sbin/nginx
#/apps/nginx/sbin/nginx -s stop

sed -ri '/#pid/s@^#(pid.*)@\1@' /apps/nginx/conf/nginx.conf
cat > /usr/lib/systemd/system/nginx.service << EOF
[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/logs/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

[Install]
WantedBy=multi-user.target
EOF

ln -s /apps/nginx/sbin/nginx /usr/sbin/
systemctl start nginx

允许安装脚本,安装完毕
1.2 后端web服务器安装httpd包

[root@centos7 ~]#yum -y install httpd
[root@centos7 ~]#ll /var/www/html/index.html -h
-rw-r--r-- 1 root root 129K Jun  2 13:17 /var/www/html/index.html   
生产环境中web页面一般有几百k,模拟生成环境,往页面中编辑点内容,让其达到大小
2、 配置nginx反向代理服务器配置和web1服务器配置

2.1 配置nginx服务器主配置文件

[root@nginx conf.d]#vim /apps/nginx/conf/nginx.conf
    #        index  index.html index.htm;
    #    }
    #}
include /apps/nginx/conf.d/*.conf;  增加还包含哪些配置文件路径
}   
[root@nginx ~]#cd /apps/nginx/conf.d/
[root@nginx conf.d]#vim pc.conf

[root@nginx conf.d]#vim pc.conf

server {
    listen       80;
    server_name  pc.tongwei.com;                                                                               
    location / {
        root /data/nginx/html/pc;
        index index.html index.php;
        }

    location /web {
        proxy_pass http://192.168.18.27:80/;  #注意有后面的/,
    }
}

[root@nginx conf.d]#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@nginx conf.d]#nginx -s reload  重新加载服务

2.2 测试,查看能否通过nginx反向代理服务器访问web1服务器页面


image.png
3、非缓存场景压测

注:一般测试三次

[root@client ~]#yum -y install httpd-tools  注:安装ab压测工具
[root@client ~]#ab -2000 -c200 http://pc.tongwei.com/web
注:一共2000个请求,每次200个请求
image.png

4、修改nginx配置文件,准备缓存配置

4.1 修改nginx主配置文件,定义缓存功能

[root@nginx ~]#vim /apps/nginx/conf/nginx.conf
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m
inactive=120s max_size=1g; #配置在nginx.conf http配置段

4.2 修改nginx的server配置文件,调用缓存功能

[root@nginx conf.d]#vim /apps/nginx/conf.d/pc.conf

server {
    listen       80;
    server_name  pc.tongwei.com;
    location / {
        root /data/nginx/html/pc;
        index index.html index.php;
        }

    location /web {
        proxy_pass http://192.168.18.27:80/;
        proxy_cache proxycache;   注:调用缓存
        proxy_cache_key $request_uri;  注:缓存uri
        proxy_cache_valid 200 302 301 10m;     注:缓存哪些返回状态码的uri,缓存时间定义                                                                
        proxy_cache_valid any 1m;  注:出来以上状态码的缓存时间是10m,其余的都缓存1分钟

    }                                                                                                          
}


[root@nginx conf.d]#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@nginx conf.d]#nginx -s reload

[root@nginx ~]#ll /data/nginx/proxycache -d
drwx------ 2 nginx root 6 Jun  2 14:25 /data/nginx/proxycache
注:/proxycache目录自动生成

5、访问并验证缓存文件

压测

[root@client ~]#ab -n2000 -c200 http://pc.tongwei.com/web
image.png

压测访问之后,nginx反向代理服务器就有缓存了

[root@nginx ~]#ll /data/nginx/proxycache
total 0
drwx------ 3 nginx nginx 15 Jun  2 14:39 8

再次压测,速度明显提升


image.png

实验二、实现以upstream 分组的方式实现http反向代理

1、 环境准备
image.png

1.1 web端安装httpd服务

yum -y install httpd

[root@web1 ~]#echo "web1 192.168.18.27" > /var/www/html/index.html

[root@wenb2 ~]#echo "web2 192.168.18.37" > /var/www/html/index.html

1.2 编译安装nignx服务,

运行脚本
[root@centos7 ~]#vim install_nginx.sh 

yum -y install yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed                                                     
cd /data/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --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
make && make install
useradd nginx -s /sbin/nologin -u 2000
chown nginx.nginx -R /apps/nginx/
#/apps/nginx/sbin/nginx
#/apps/nginx/sbin/nginx -s stop

sed -ri '/#pid/s@^#(pid.*)@\1@' /apps/nginx/conf/nginx.conf
cat > /usr/lib/systemd/system/nginx.service << EOF
[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/logs/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

[Install]
WantedBy=multi-user.target
EOF

ln -s /apps/nginx/sbin/nginx /usr/sbin/
systemctl start nginx

2. 配置nginx配置文件

2.1 设置修改server配置文件(自定义一组服务器,配置在http内)

[root@nginx ~]#vim /apps/nginx/conf.d/pc.conf
upstream webserver {
        server 192.168.18.27:80 weight=1 fail_timeout=5s max_fails=3;
        server 192.168.18.37:80 weight=1 fail_timeout=5s max_fails=3;                        
    }
注:自定义服务器组为:webserver
将被代理服务器27,37添加到组里
权重设置为1
对后端服务器的单次监测超时时间设置为5s
对后端服务器连续监测失败3次就标记为不可用
server {
    listen       80;
    server_name  pc.tongwei.com;
    location / {
        root /data/nginx/html/pc;
        index index.html index.php;
        }

    location /web {
        proxy_pass http://webserver/;                                                        
    }
}

[root@nginx ~]#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@nginx ~]#nginx -s reload


3. 测试
[root@client ~]#while true; do curl http://pc.tongwei.com/web; sleep 0.5;done
web1 192.168.18.27
web2 192.168.18.37
web1 192.168.18.27
web2 192.168.18.37
web1 192.168.18.27
web2 192.168.18.37

实验三、实现以upstream 分组的方式实现tcp反向代理

注:通过nginx反向代理实现 mysql负载均衡

1. 环境配置
image.png

1.1 编译安装nginx,见实验1
1.2 在mysql服务器上安装mariadb-server包

[root@mysql network-scripts]#yum -y install mariadb-server
[root@mysql network-scripts]#systemctl start mariadb
[root@mysql network-scripts]#systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

1.3客户端安装服务器客户端mariadb包

[root@client ~]#yum -y install mariadb

2. 配置nginx配置文件

2.1 tcp负载均衡配置参数

[root@nginx pc]#mkdir /apps/nginx/tcp
[root@nginx pc]#cd /apps/nginx/tcp
[root@nginx tcp]#vim tcp_proxy.conf

stream {
    upstream mysql_host {   注:创建基于tcp反向代理服务的组
        server 192.168.18.47:3306;  注:组内成员可以写n个,本次实验写一个,以及监听端口
    }

    server {
        listen 192.168.18.17:3306;  注:nginx本机监听端口端口
        proxy_connect_timeout 5s;
        proxy_timeout 5s;
        proxy_pass mysql_host;                                                                                   
    }
}

2.2 导入tcp负载均衡配置文件路径到nginx主配置文件中

[root@nginx conf.d]#vim /apps/nginx/conf/nginx.conf
.......
include /apps/nginx/tcp/*.conf;      此路径一定要放在http块之外                                                                            
http {..........


[root@nginx conf.d]#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@nginx conf.d]#nginx -s reload


[root@nginx conf.d]#ss -tnl
State       Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN      0      128              192.168.18.17:3306                                     *:*  
注:本机3306端口已经监听

2.3 mysql后端服务器添加用户

[root@mysql network-scripts]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.18.17' IDENTIFIED BY '123456' WITH GRANT OPTION; 
Query OK, 0 rows affected (0.00 sec)

3. 测试
[root@client ~]#mysql -uroot -p123456 -h 192.168.18.17
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

实现简单的lnmp架构

image.png

相关文章

网友评论

    本文标题:nginx之proxy_cache缓存、upstream 分组的

    本文链接:https://www.haomeiwen.com/subject/rrrxxctx.html