将 nginx 设置为将一台服务器上的失败请求代理到另一台服务器 [英] Setting up nginx to proxy failed requests on one server to another

查看:85
本文介绍了将 nginx 设置为将一台服务器上的失败请求代理到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新版本的 api,它是对当前 api 的完全重写.一开始,新 api 不会处理任何请求,但随着时间的推移,越来越多的路由将在新 api 中实现(其中大多数使用它们在旧 api 中使用的相同路径).我在与新 api 服务(在端口 3000 上运行的节点)相同的服务器上设置了 nginx,而旧的 api 服务在 api.example.com (192.168.1.25) 上运行.我想要做的是将 api.example.com 指向新的 api 服务,然后当请求进来时,让 nginx 首先尝试新 api 服务(127.0.0.1:3000)上的请求,如果该请求返回 404,然后将请求发送到旧的api服务(192.168.1.25).

I am working on a new version of an api that is a complete rewrite of the current api. To start, the new api will not handle any requests, but over time, more and more routes will be implemented in the new api (most of them using the same path that they use in the old api). I have nginx set up on the same server as the new api service (node running on port 3000) and the old api service is running at api.example.com (192.168.1.25). What I want to do is point api.example.com at the new api service, then when a request comes in, have nginx first try the request on the new api service (127.0.0.1:3000) and if that request returns a 404, then send the request to the old api service (192.168.1.25).

推荐答案

我最终使用以下配置使其与标头和 cookie 支持一起使用.

I ended up getting this to work with header and cookie support using the following config.

http {
  upstream new_api_backend {
    server 127.0.0.1:3000;
  }

  upstream old_api_backend {
    server old.example.com:443;
  }

  server {
    listen         80;
    return         301 https://$http_host$request_uri;
  }

  server {
    proxy_http_version 1.1;

    listen               443;
    ssl                  on;
    ssl_certificate      /etc/nginx/ssl/my_cert.crt;
    ssl_certificate_key  /etc/nginx/ssl/my_cert.key;

    location / {
      proxy_intercept_errors on;
      error_page 417 = @old_backend;
      proxy_pass http://new_api_backend;
    }

    location @old_backend {
      proxy_set_header Host old.example.com;
      proxy_redirect https://old.example.com/ https://$http_host/;
      proxy_cookie_domain old.example.com $http_host;
      proxy_pass https://old_api_backend;
    }
  }
}

注意 error_page 417 = @old_backend.这使得 nginx 捕获来自新服务器的 417 响应作为使用旧服务器的触发器.然后我只是在新服务器上添加了一个包罗万象的路由来返回 417,这样 404 仍然可以在新服务器上适当的时候使用.417 Expectation Failed 可能不是最适合此用例的代码,但看起来已经足够接近了.

Note the error_page 417 = @old_backend. This makes nginx catch a 417 response from the new server as the trigger to use the old server. Then I just added a catchall route to the new server to return 417, that way 404s can still be used when appropriate on the new server. 417 Expectation Failed may not be the most appropriate code for this use case, but it looked close enough.

此外,这将正确地将 http://example.com/some/path 代理到 https://old.example.com/some/path.

Also, this will correctly proxy http://example.com/some/path to https://old.example.com/some/path.

这篇关于将 nginx 设置为将一台服务器上的失败请求代理到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆