Nginx 与 Docker - 在 URL 中使用子目录时出现静态资源 404 错误

问题描述 投票:0回答:1

我有一个应用程序在 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 位置块以重写 URL 并处理静态 资源。
  • 使用 try_files 和别名指令来提供静态服务 内容。
  • 暂时删除了重写指令,但这并没有 解决问题。

问题: 如何配置 Nginx 以在 /customer-portal/ 前缀下正确提供静态资产,而不更改后端应用程序?关于正确处理此 URL 重写有什么建议吗? enter image description here

nginx url-rewriting reverse-proxy
1个回答
0
投票

目前我也有同样的问题....

© www.soinside.com 2019 - 2024. All rights reserved.