nginx使用proxy\u pass重定向

nginx使用proxy\u pass重定向,nginx,nginx-location,Nginx,Nginx Location,我有一个在本地机器上运行的nginx代理服务器配置。它在docker中运行,node.js实例也在运行,它为我的ghost博客的本地实例提供服务 因为我刚从一个旧的博客引擎迁移过来,我的url现在不同了,所以我想配置nginx,将301(永久)重定向返回到新的url方案。我尝试过使用rewrite和proxy\u redirect这两种方法,在这两种情况下我都得到了401个错误。这是我的default.conf # If we receive X-Forwarded-Proto, pass it

我有一个在本地机器上运行的nginx代理服务器配置。它在docker中运行,node.js实例也在运行,它为我的ghost博客的本地实例提供服务

因为我刚从一个旧的博客引擎迁移过来,我的url现在不同了,所以我想配置nginx,将301(永久)重定向返回到新的url方案。我尝试过使用
rewrite
proxy\u redirect
这两种方法,在这两种情况下我都得到了401个错误。这是我的default.conf

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    return 503;
}
upstream localhost {
    server 172.17.0.3:2368;
}
server {
    server_name localhost;
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    location / {
#        rewrite "^/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)$" /$1 break; 
        proxy_pass http://localhost;
#        proxy_redirect "-*/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)" /$1;
    }
}
如果我取消对rewrite或proxy_redirect指令的注释,则什么也不会发生——请求被转发到node.js,我得到404

如果我使用以下多个位置指令:

location /archive/ {
    rewrite "^/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)$" /$1 break; 
}    
location / {
    proxy_pass http://localhost;
}

我从nginx获得了一个404,请求从未发送到node.js。

您的
重写
在一个单独的位置块中,应该使用
last
而不是
break
(详细信息请参阅):

proxy\u redirect
不重写请求,它重写来自上游的3xx响应。有关详细信息,请参阅


我看不出您的
重写方法有任何错误,除非正则表达式是错误的。您可以尝试添加
rewrite\u登录
逐步记录重写。有关详细信息,请参见。

在单独的位置块中,您的
重写
应使用
最后一次
,而不是
中断
(有关详细信息,请参见):

proxy\u redirect
不重写请求,它重写来自上游的3xx响应。有关详细信息,请参阅


我看不出您的
重写方法有任何错误,除非正则表达式是错误的。您可以尝试添加
rewrite\u登录
逐步记录重写。有关详细信息,请参见。

在单独的位置块中,您的
重写
应该使用
最后一次
,而不是
中断
<代码>代理重定向
不重写请求。但是我看不出你的重写方法有任何错误,除非正则表达式是错误的。您可以尝试
rewrite\u登录
逐步记录重写。将单独的位置块更改为
last
有效!我也尝试过将其作为一个单独的位置块,这也很有效。如果你发一个答案,我会把它标记为接受。非常感谢。您的
重写
在一个单独的位置块中,应该使用
last
而不是
break
<代码>代理重定向
不重写请求。但是我看不出你的重写方法有任何错误,除非正则表达式是错误的。您可以尝试
rewrite\u登录
逐步记录重写。将单独的位置块更改为
last
有效!我也尝试过将其作为一个单独的位置块,这也很有效。如果你发一个答案,我会把它标记为接受。非常感谢。
location /archive/ {
    rewrite "^/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)$" /$1 last;
}