栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

反向代理与负载均衡

环境说明

主机名IP服务
nginx192.168.129.3nginx
agent192.168.129.33nginx
httpd192.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.33nginx
httpd192.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地址访问测试

  • 访问静态资源

  • 访问动态资源

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/360786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号