Nginx 代理所有以 /feed 结尾的东西

Nginx proxy everything ending with /feed

我想将所有以 /feed 结尾的内容代理到另一个域。

比如我要

http://example.com/blog/feed变成

http://api.example2.com/blog/feed

http://example.com/blog/categories/my-category/feed变成

http://api.example2.com/blog/categories/my-category/feed

这是我目前得到的

server {
  listen 80;
  server_name example.com

  location ~ \feed$ {
    proxy_pass api.example2.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_hide_header Content-Type;
    add_header Content-Type application/rss+xml;
    proxy_redirect off;
  }

您自己的解决方案应该是正确的,但您需要使用正斜杠而不是反斜杠。

location ~ /feed$ {
    proxy_pass api.example2.com;
    # the rest of the settings
}

对于那些什么都不说就投反对票的人,我只是复制了 written in the documentation:

When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):

location /name/ {
    rewrite    /name/([^/]+) /users?name= break;
    proxy_pass http://127.0.0.1;
}

In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.