nginx负载均衡和故障转移

2018-12-21  本文已影响0人  凝碧_0e80

upstream配置如下:

#user  nobody;
worker_processes 1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
#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  logs/access.log  main;
    sendfile on;
#tcp_nopush     on;
#keepalive_timeout  0;
    keepalive_timeout 65;
#这里的域名要和下面proxy_pass的一样
    upstream test {
        server localhost:8000  weight=1 max_fails=1 fail_timeout=5s;
#发生错误一次,隔5秒再启用这个服务器
        server localhost:3000  weight=1 max_fails=1 fail_timeout=5s;
    }
#gzip  on;
    server {
        listen 8080;
        server_name test;
#charset koi8-r;
#access_log  logs/host.access.log  main;
        location / {
            root html;
            index index.html index.htm;
#对应上面的test,代理的地址
            proxy_pass http://test;
#故障转移
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429 non_idempotent;
#将请求传递到下一个服务器可以通过尝试次数和时间来限制,限制请求可以传递到下一个服务器的时间。 0值关闭此限制。
            proxy_next_upstream_timeout 0;
#限制将请求传递到下一个服务器的可能尝试次数。 0值关闭此限制。
            proxy_next_upstream_tries 0;
            proxy_read_timeout 5s;
            proxy_connect_timeout 5s;
            proxy_send_timeout 10s;
        }
        location = /50x.html {
            root html;
        }
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
        error_page 500 502 503 504  /50x.html;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读