我有一个应用程序在 Docker 容器中运行,Nginx 配置为在子目录(例如 /customer-portal/)下提供服务。后端应用程序不知道此前缀,并期望从根 (/) 提供服务。
Nginx 服务器块:
server {
listen 443 ssl;
server_name test.com;
ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
location /customer-portal/ {
rewrite ^/customer-portal(/.*)$ $1 break;
proxy_pass http://customer-portal_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect / /customer-portal/;
}
}
主应用程序页面加载,但所有静态资源(如 JS 和 CSS 文件)返回 404 错误。当 Nginx 提供服务时,这些资源的请求 URL 缺少 /customer-portal/ 前缀。
例如: https://test.com/_next/static/css/ce20e8d724c2d451.css - 404 未找到 但是,当从根 (/) 提供应用程序时,一切正常。
我尝试过的:
问题: 如何配置 Nginx 以在 /customer-portal/ 前缀下正确提供静态资产,而不更改后端应用程序?关于正确处理此 URL 重写有什么建议吗?
目前我也有同样的问题....