一、proxy_pass结尾写法的问题

proxy_pass的url后加上了 / (斜杠)符号相当于是加了路径,则url就不会显示location的路径
反之,则会把location写的路径部分保留显示

1.有/符号

location ^~ /data/
{
    proxy_cache css_cache;
    proxy_set_header Host www.lxtian.com;
    proxy_pass http://www.lxtian.com/;
}

如上配置:
请求url: http: //域名/data/1.php
会代理到:http: //www.lxtian.com/1.php

2.无/符号

location ^~ /data/
{
    proxy_cache css_cache;
    proxy_set_header Host www.lxtian.com;
    proxy_pass http://www.lxtian.com;
}

如上配置:
请求url: http:// 域名/data/1.php
会代理到:http:// www.lxtian.com/data/1.php

二、负载均衡

状态的配置

down:表示当前的server暂时不参与负载均衡。
backup:预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
fail_timeout:在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

#热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。
#服务器处理请求的顺序:AAAAAA突然A挂啦,BBB.....
upstream images { 
    server 192.168.1.50:8080; 
    server 192.168.1.50:8080 backup;  #热备     
}
#轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB....
upstream images1 { 
    server 192.168.1.50:8080;
    server 192.168.1.51:8080;       
}
#加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。
#如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB....
 upstream images2 { 
    server 192.168.1.50:8080 weight=1;
    server 192.168.1.51:8080 weight=2;
}
#ip_hash:nginx会让相同的客户端ip请求相同的服务器。
upstream images3 { 
    server 192.168.1.50:8080; 
    server 192.168.1.51:8080;
    ip_hash;
}
#失败max_fails(次),fail_timeout暂停多久(秒)
upstream images4 {
    server 192.168.1.50:8080   max_fails=2 fail_timeout=30s;
    server 192.168.1.51:8080   max_fails=2 fail_timeout=30s;
}
server {
    listen 80;
    server_name _;

    location / {
        proxy_read_timeout 1800;
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://images;
    }
}

 

本文转载于:https://www.jianshu.com/p/d2100c4f2fe8