nginx负载均衡

本文详细介绍了如何在四台服务器环境下配置Nginx作为负载均衡器,包括修改hosts文件,安装并配置httpd,以及Nginx的conf文件设置,实现对静态和动态内容的分发。同时,文章提供了解决Apache启动问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、环境

四台机子

1、vim /etc/hosts

      192.168.40.132   web2        192.168.40.211   web1     192.168.40.155   web3 (静态)
      192.168.40.129   nginx        192.168.40.200  php(动态)
2、安装httpd, 防火墙,selinux全关

3、vim /var/www/html/{index.html,index.php}

二、192.168.40.129  nginx端 配置

[root@localhost ~]# vim /etc/nginx/nginx.conf

error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
   

include /etc/nginx/conf.d/*.conf;

upstream  html {
        server 192.168.40.132:80;
        server 192.168.40.211:80;
        server 192.168.40.155:80;
}

upstream  php {
        server 192.168.40.200:80;
}

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
proxy_pass http://html;
        }

       location  ~ \.php$ {
    proxy_pass http://php;
}

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
三、结果测试

结果如图所示

 

 

 

 

 

注意:apache启动不了,端口被占用情况

[root@haproxy ~]# netstat   -tunlp  |   grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15606/haproxy    

[root@haproxy ~]# systemctl stop haproxy
[root@haproxy ~]# netstat -tunlp|grep 80
[root@haproxy ~]# systemctl restart httpd
[root@haproxy ~]# netstat -tunlp|grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      16507/httpd  

转载于:https://www.cnblogs.com/zjz20/p/11544881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值