nginx负载均衡配置实现动静分离

nginx负载均衡配置实现动静分离,第1张

nginx负载均衡配置实现动静分离 反向代理与负载均衡

环境说明

主机名IP服务nginx192.168.129.3nginxagent192.168.129.33nginxhttpd192.168.129.133httpd

注:nginx服务都是源码安装 、httpd为yum安装
准备工作
每台主机开启服务,并关闭防火墙与selinux

修改配置

[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
......

    #gzip  on;

    upstream webservers {                        #配置负载均衡
        server 192.168.129.3;
        server 192.168.129.133;
    }
    

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {                       #配置反向代理
            proxy_pass http://webservers;
        }

        #error_page  404              /404.html;

......

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

使用agent主机IP地址访问,并刷新测试

动静分离

环境说明

主机名IP服务lnmp192.168.129.135lnmp架构agent192.168.129.33nginxhttpd192.168.153.139httpd

准备工作
lnmp架构部署详细步骤:翻阅此文章lnmp

开启服务

//lnmp主机
[root@lnmp ~]# nginx
[root@lnmp ~]# systemctl start php-fpm.service 
[root@lnmp ~]# systemctl start mysqld.service 
[root@lnmp ~]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port               Peer Address:Port       
LISTEN       0            128                    127.0.0.1:9000                    0.0.0.0:*          
LISTEN       0            128                      0.0.0.0:80                      0.0.0.0:*          
LISTEN       0            128                      0.0.0.0:22                      0.0.0.0:*          
LISTEN       0            80                             *:3306                          *:*          
LISTEN       0            128                         [::]:22                         [::]:*          

//httpd主机
[root@httpd ~]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port               Peer Address:Port                       
LISTEN       0            128                      0.0.0.0:22                      0.0.0.0:*                         
LISTEN       0            128                            *:80                            *:*          
LISTEN       0            128                         [::]:22                         [::]:*          

//agent主机
[root@agent ~]# nginx
nginx: [emerg] still could not bind()
[root@agent ~]# ss -anlt
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      100             127.0.0.1:25                                  *:*                  
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      100                   ::1:25                                 :::* 

修改agent主机配置文件

[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
......
    #gzip  on;
    upstream static {                   
        server 192.168.129.33;			#httpd主机的ip
    }
   
    upstream dynamic {             
        server 192.168.129.135;         #lnmp主机的ip
    }

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://static;       			#访问静态资源会自动跳转到进行访问
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        
        location ~ .php$ {
            proxy_pass   http://dynamic;               #访问动态资源会自动跳转到进行访问
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
......

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

使用agent主机IP地址访问测试

  • 访问静态资源

  • 访问动态资源

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4830693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存