nginx反代1024,nginx负载均衡

nginx反向代理1024,假如只用一个VPS、同一个IP反代1024,访问量大了,访问会出现验证码的问题,所以采用负载均衡来分摊流量。

前端服务器 192.168.1.2 nginx 配置模板如下:

upstream 1024{
ip_hash; #根据客户端ip,计算hash值并映射到相应服务器.
server 192.168.1.3 weight=1 max_fails=3 fail_timeout=5;
server 192.168.1.4 weight=1 max_fails=3 fail_timeout=5;
server 192.168.1.5 weight=1 max_fails=3 fail_timeout=5;
server 192.168.1.6 weight=1 max_fails=3 fail_timeout=5;
}

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

    location / {
        proxy_pass http://1024; #要和第一行的upstream名字一致.
        proxy_redirect off; 
        proxy_set_header User-Agent $http_user_agent;
        proxy_set_header Referer http://www.t66y.com;
        proxy_set_header Accept-Encoding "";
        sub_filter "www.t66y.com" "www.test.com";
        sub_filter_once off;
        }

    access_log /root/test.com.log;
    error_log /root/test.com.error.log;
}

 

后端代理服务器 nginx 配置模板如下:

server
{
    listen 80; #可以使用非80端口,避免其他人连接,对应的上面"server 192.168.1.3:端口号",即可.
    server_name test1.com www.test1.com;

    location / 
    {
        proxy_pass http://www.t66y.com;
        proxy_set_header Host www.t66y.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;

allow 192.168.1.2; #只允许负载服务器连接.
deny all; #只允许负载服务器192.168.1.2连接,其他ip全部禁止访问.

    }
    
    location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
    {
        proxy_set_header Host www.t66y.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass http://www.t66y.com;
        
    }
    
    location ~ .*\.(html|htm|png|gif|jpeg|jpg|bmp|js|css)?$
    {
        proxy_set_header Host www.t66y.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass http://www.t66y.com;
        
    }
    
    access_log off;
    error_log off;
}

 

分享到:

0 条评论

昵称

沙发空缺中,还不快抢~