现在我们已经设置好了服务器,这样流量就会通过 .NET 服务器,并向 Node 服务器发送 React 请求。我正在尝试将我们的开发和准备环境切换到相反的方向,这样所有内容都会首先通过我们的 Node 应用程序,然后代理到旧服务器,处理我们尚未在 React 中完成的任何操作。
此设置涉及.NET(旧版)服务器、AWS、TeamCity 和许多其他我从未接触过的东西,因此有可能我在其他地方遇到了问题,但我很确定是 NGINX 配置导致了我的麻烦。
目前,大多数内容都可以正确呈现,但我们有许多由旧版应用提供的资源未被获取。例如,我们有一个 CSS 文件,当请求时,它实际上返回 200 响应,但显示的是 404 页面。
这是相关的 nginx.conf。第 38 行注释掉的位置似乎与其下面的行没有任何不同。基本上所有不在 24 行中的路径,我都需要从旧版应用程序中获取。有什么想法吗?
upstream nodeserver {
server 127.0.0.1:4010;
}
resolver 8.8.8.8 valid=5s;
server {
listen 80;
server_name dev.stagingserver.com render.dev.stagingserver.com;
set $legacy "dev.stagingserver.com/";
location ~* \.js {
gzip_static on;
root /web/public;
}
location ~* \.css {
gzip_static on;
root /web/public;
try_files $uri $uri/ @legacy;
}
location ~* ^/(these|are|features|served|by|nodeserver)/ {
proxy_intercept_errors on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host dev.stagingserver.com;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://nodeserver;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# location ~* ^/(these|features|are|on|legacy|server)/ {
location / {
proxy_intercept_errors on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host dev.stagingserver.com;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$legacy;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cookie_domain $legacy $host;
}
location @legacy {
proxy_intercept_errors on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host dev.stagingserver.com;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$legacy$request_uri/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
error_page 404 @legacy;
error_page 500 501 502 503 504 507 508 509 /maintenance.html;
location = /maintenance.html {
root /web;
internal;
}
}
答案1
我的问题最终部分出在方案上,部分出在我将 legacy 设置成什么,部分出在 127.0.0.1 不是我所想的那样。以下是我们最终使用的设置。
upstream nodeserver {
server nodeserver-docker-image-name:4010;
}
resolver 8.8.8.8 valid=300s;
map $http_x_forwarded_proto $forwarded_scheme {
default $scheme;
}
server {
listen 80;
server_name dev.stagingserver.com render.dev.stagingserver.com;
set $legacy "dev.stagingserver-aws-internal-dns.com";
location ~* ^/(all|relevant|features) {
proxy_pass http://nodeserver;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host dev.stagingserver.com;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
try_files $uri $uri/ @legacy;
}
location @legacy {
proxy_pass $forwarded_scheme://$legacy$request_uri;
proxy_redirect off;
proxy_cookie_domain $legacy dev.stagingserver.com;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host dev.stagingserver.com;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
error_page 500 501 502 503 504 507 508 509 /maintenance.html;
location = /maintenance.html {
root /web;
internal;
}
}