反向代理&企业级动静态分离案例

该场景是,通过nginx实现动静分离,配置反向代理规则,实现动态请求和静态请求分别转发给不同的服务器解析,以解决网站性能、安全、用户体验等问题。

案例配置准备

可以使用克隆的形式创建虚拟机
10.0.0.30    lb01 #负载均衡服务器
10.0.0.40    www01 #后台节点服务器
10.0.0.50    www02 #后台节点服务器
10.0.0.60 www03 #后台节点服务器
当然,根据需要可以配置多台后台节点服务器

以下在负载均衡服务器上配置

【配置静态服务器池】

     upstream static_pools {
        server 10.0.0.40;
}

【配置上传服务器池】

     upstream upload_pools {
        server 10.0.0.50;
}

【默认地址池,动态地址池】

     upstream default_pools {
        server 10.0.0.60;
}

实际配置思路

【方案1】

使用nginx的location功能,匹配不同的URL(路径),请求分发给不同的服务器池

location /static/ {
            proxy_pass http://static_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
}

        location /upload {
            proxy_pass http://upload_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
}



        location / {
            proxy_pass http://default_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
}

 【后台节点服务器配置】

www01机器:

    server {
        listen       80;
        server_name  www.bbq.com;

        #charset koi8-r;
        charset utf-8;
        #access_log  logs/host.access.log  main;

        location / {
            root   html/bbq;
            index  index.html index.htm;
        }
[root@www01 ~]# mkdir /opt/nginx-1.25.1/html/bbq/static/
[root@www01 ~]# echo "这里是分布式静态服务器static" > /opt/nginx-1.25.1/html/bbq/static/index.html 
[root@www01 ~]# nginx -s reload

www02机器:

server {
        listen       80;
        server_name  www.bbq.com;

        #charset koi8-r;
        charset utf-8;    #添加支持中文字符格式

        #access_log  logs/host.access.log  main;

        location / {
            root   html/bbq;
            index  index.html index.htm;
        }
[root@www02 ~]# mkdir -p /opt/nginx-1.25.1/html/bbq/upload/
[root@www02 ~]# echo "这里是upload服务器" > /opt/nginx-1.25.1/html/bbq/upload/index.html 
[root@www02 ~]# nginx -s reload

www03机器:

    server {
        listen       80;
        server_name  www.bbq.com;

        #charset koi8-r;
        charset utf-8;    #同上
        #access_log  logs/host.access.log  main;
  #直接默认让www03机器location到/(也就是upstream地址池定位的服务器目录上)
        location / {
            root   html/bbq;
            index  index.html index.htm;
        }

明确配置

主机IP,端口

测试地址

www0110.0.0.40junwu.com/static/
www0210.0.0.50junwu.com/upload/
www0310.0.0.60junwu.com

【默认服务器】

 【静态服务器】

 【上传服务器】

 

posted @ 2023-06-27 01:47  Junwu’sblog  阅读(9)  评论(0编辑  收藏  举报