使用Nginx在发生错误时重定向到其他上游

时间:2019-02-04 18:30:58

标签: nginx flask server uwsgi

说明

我有两个不同的服务/服务器在本地运行,一个使用在端口9000上运行的php5.6-fpm,另一个是在端口5000上运行的uwsgi,我希望能够停止php5.6-fpm并具有请求将重定向到uwsgi服务。

upstream phpfpm {
    server 127.0.0.1:9000 max_fails=1 fail_timeout=5s;
}

upstream darkapp {
    server 0.0.0.0:5000 max_fails=1 fail_timeout=5s;
}



server {
    listen 2000;
    index index.php;

    location ~ \.php$ {
        root /var/www/html;
        fastcgi_pass phpfpm;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_connect_timeout 5s;
        error_page 404 502 = /dark/home;
        #error_page 404 = @fallback;
    }

    #location @fallback
    location = /dark/home {
        #include uwsgi_params;
        uwsgi_pass darkapp;
        #proxy_pass http://localhost:4000;
    }
}

server {
    listen 4000;

    location / { 
        include uwsgi_params;
        uwsgi_pass darkapp;
    }
}

注意

在手动停止fpm服务之前,我可以访问端口2000上的php应用程序,也可以在端口4000上访问python应用程序。

会发生什么

当我停止php-fpm并尝试通过浏览器访问端口2000上的站点时,如果我不包含uwsgi_params,则会得到以下信息

2019/02/04 14:27:54 [error] 21062#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:2000"


2019/02/04 14:27:54 [error] 21062#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "uwsgi://0.0.0.0:5000", host: "localhost:2000"

运行烧瓶应用程序的Uwsgi进程还提供以下内容

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2287, in wsgi_app
    ctx = self.request_context(environ)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2204, in request_context
    return RequestContext(self, environ)
  File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 284, in __init__
    self.url_adapter = app.create_url_adapter(self.request)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2021, in create_url_adapter
    subdomain=subdomain)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1344, in bind_to_environ
    environ['REQUEST_METHOD'], path_info,
KeyError: 'REQUEST_METHOD'

当我确实包含uwsgi参数时,它重定向到/,我得到以下信息

2019/02/04 14:42:34 [error] 22493#0: *43 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:2000"
2019/02/04 14:42:34 [error] 22493#0: *43 directory index of "/usr/share/nginx/html/" is forbidden, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:2000"

1 个答案:

答案 0 :(得分:0)

使用return指令将请求传递给正在侦听uwsgi进程的服务器块

upstream phpfpm {
    server 127.0.0.1:9000 max_fails=1 fail_timeout=5s;
}

upstream darkapp {
    server 0.0.0.0:5000 max_fails=1 fail_timeout=5s;
}



server {
    listen 2000;
    index index.php;
    root /var/www/html;
    location ~ \.php$ { 

        fastcgi_pass phpfpm;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_connect_timeout 5s;
        error_page 403 404 502 = @fallback;
    }

    location @fallback {
        return 301 http://$host:4000/;
    }
}

server {
    listen 4000;

    location / { 
        include uwsgi_params;
        uwsgi_pass darkapp;
    }
}