如何让Nginx先尝试新API(reverse_proxy),然后回退到旧API(fastcgi)

时间:2020-06-11 02:45:52

标签: nginx

我们有一些旧的API,它们通过 nginx 上的fastcgi提供。我们正在将它们中的一些重新实现在其他服务器中,因此需要反向代理,例如通过proxy_pass

我们如何让nginx尝试首先使用新的API(proxy_pass),如果尚未实现该API,然后回退到旧的API(fastcgi)?

几年前,这里有一个类似的问题-Setting up nginx to proxy failed requests on one server to another,但是这个问题有点不同,因为我有2种不同类型的系统(fastcgi和proxy)。这个较早的问题使用2个相同类型的系统-代理

为旧api提供服务的当前nginx配置与此类似

server {
    listen       443 ssl;
    server_name  localhost;
    include ssl-params.conf

    location / {
        root /var/www/localhost/html;
        try_files $uri $uri/ /index.html?/$request_uri;
    }

    location ~ /(api|bootstrap) {
        alias /var/www/localhost/wsapi/project;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param SCRIPT_FILENAME /var/www/localhost/wsapi/project/main
        fastcgi_param PATH_INFO $request_uri;
        include fastcgi_params;
    }

    include /etc/config/nginx/server/*;
}

我想先添加类似的内容,但到目前为止,在使nginx退回到较旧的API上没有任何成功

    location @uvicorn {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
        proxy_buffering off;
        proxy_pass http://uvicorn;
    }

...

upstream uvicorn {
    server unix:/var/run/uvicorn.sock;
}

我已经找到并尝试过的相关信息,但未能成功使nginx首先尝试新的api,然后回退到旧的api

0 个答案:

没有答案