nginx 重定向 proxy_pass
nginx redirect with proxy_pass
我在我的本地机器上有一个适用于 nginx 运行 的有效代理服务器配置。它是 docker 中的 运行,服务于我的幽灵博客本地实例的 node.js 实例也是如此。
因为我刚刚从一个旧的博客引擎迁移过来,所以我的 url 现在不同了,所以我想将 nginx 配置为 return 301(永久)重定向到新的 url方案。我尝试同时使用 rewrite
和 proxy_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)$" / break;
proxy_pass http://localhost;
# proxy_redirect "-*/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)" /;
}
}
如果我取消对重写或 proxy_redirect 指令的注释,什么都不会发生 - 请求被转发到 node.js,我得到一个 404。
如果我像这样使用多个位置指令:
location /archive/ {
rewrite "^/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)$" / break;
}
location / {
proxy_pass http://localhost;
}
我从 nginx 收到一个 404,请求从未到达 node.js。
我在我的本地机器上有一个适用于 nginx 运行 的有效代理服务器配置。它是 docker 中的 运行,服务于我的幽灵博客本地实例的 node.js 实例也是如此。
因为我刚刚从一个旧的博客引擎迁移过来,所以我的 url 现在不同了,所以我想将 nginx 配置为 return 301(永久)重定向到新的 url方案。我尝试同时使用 rewrite
和 proxy_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)$" / break;
proxy_pass http://localhost;
# proxy_redirect "-*/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)" /;
}
}
如果我取消对重写或 proxy_redirect 指令的注释,什么都不会发生 - 请求被转发到 node.js,我得到一个 404。
如果我像这样使用多个位置指令:
location /archive/ {
rewrite "^/archive/(\d{4}/\d{2}/\d{2}/.+)\.(?:aspx|html)$" / break;
}
location / {
proxy_pass http://localhost;
}
我从 nginx 收到一个 404,请求从未到达 node.js。