nginx 配置

weight=number 设置服务器的权重,默认为1

max_fails=number  设置在fail_timeout 时间内不成功的尝试次数,判断此机器是否可用,默认尝试次数为1

fail_timeout=time  设置判断机器是否可用尝试的时间,默认时间为10s

backup 标明此机器为备份机器,当主机器不可用的时候起作用。

down  标明此机器不可用。

max_conns=number 限制代理服务器的最大连接数。默认值为0,也就是没有限制

resolve  监听ip地址以及对应的服务器域名的改变,自动修改配置,不用重启nginx.

route=string 设置服务器的路由名称

slow_start=time  设置服务器权重从0恢复到标准值的时间,默认值为0

ip_hash 请求基于客户端的ip被分发到不同的机器,把客户端IPv4地址的前三个字节或者整个ipv6地址作为一个key,确保当一个请求来自同一个客户端,发布到同一个服务器上直到这台机器不可用,才被分发到另外一台机器上。

upstream backend {
    ip_hash;

    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
    server backend4.example.com;
}
keepalive 设置服务器持续连接的最大连接数的参数,当这个数目被超过之后,最近最少使用的连接被关闭。
upstream memcached_backend {
    server 127.0.0.1:11211;
    server 10.0.0.2:11211;

    keepalive 32;
}
upstream http_backend {
    server 127.0.0.1:8080;

    keepalive 16;
}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}

health_check :
interval=time 时间间隔,两个连续检查的时间间隔,默认为5s
   fails=number 连续失败次数,根据这个参数判断此机器是否健康,默认值为1.
   passes=number 连续通过的次数,很据这个参数达到这个参数判断此机器是健康的,默认值为1.
   match=name 指定回应正确的判断条件,默认是这个response 的返回码应该是2xx或者3xx
例子:
http {
    server {
    ...
        location / {
            proxy_pass http://backend;
            health_check match=welcome;
        }
    }

    match welcome {
        status 200;
        header Content-Type = text/html;
        body ~ "Welcome to nginx!";
    }
}

match=name:
status 200;
status is 200
status ! 500;
status is not 500
status 200 204;
status is 200 or 204
status ! 301 302;
status is neither 301 nor 302
status 200-399;
status is in the range from 200 to 399
status ! 400-599;
status is not in the range from 400 to 599
status 301-303 307;
status is either 301, 302, 303, or 307
header Content-Type = text/html;
header contains “Content-Type” with value text/html
header Content-Type != text/html;
header contains “Content-Type” with value other than text/html
header Connection ~ close;
header contains “Connection” with value matching regular expression close
header Connection !~ close;
header contains “Connection” with value not matching regular expression close
header Host;
header contains “Host”
header ! X-Accel-Redirect;
header lacks “X-Accel-Redirect”
body ~ "Welcome to nginx!";
body matches regular expression “Welcome to nginx!
body !~ "Welcome to nginx!";
body does not match regular expression “Welcome to nginx!
例子:
match welcome {
    status 200;
    header Content-Type = text/html;
    body ~ "Welcome to nginx!";
}
# status is not one of 301, 302, 303, or 307, and header does not have "Refresh:"
match not_redirect {
    status ! 301-303 307;
    header ! Refresh;
}
# status ok and not in maintenance mode
match server_ok {
    status 200-399;
    body !~ "maintenance mode";
}
queue number  当请求数达到最大值时,请求会放到队列里面,当队列满了的时候并且服务器不能再接受请求,然后返回客户端502。

原文地址:https://www.cnblogs.com/fanminghui/p/5799678.html