无法从 Nginx 反向代理后面的 docker 容器提供静态资源

CSh*_*arp 5 nginx docker create-react-app

我正在尝试使用 Nginx 作为反向代理来为两个容器提供服务。这是我的 Nginx conf 文件的一部分:

upstream dashboard {
    server dashboard:80;
}

upstream editor {
    server editor:80;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass         http://dashboard;
    }

    location /editor/ {
        rewrite ^/editor(.*)$ $1 break;
        proxy_pass         http://editor;
    } 
Run Code Online (Sandbox Code Playgroud)

当我导航到浏览器中的 url 时,我收到 404 错误/editor,因为该页面正在提交对驻留在上游容器“editor”中的静态资源的请求。

在此输入图像描述

我对 Nginx 还很陌生,但我推测当它收到带有 url 的请求时: http://example.com/static/css/2.3d394414.chunk.css

Nginx 无法知道相应的 css 位于editor容器内部。我该如何修改配置来解决这个问题?我已经看到一些配置为任何静态资产提供了通用路径,但我需要一个可以处理 docker 容器内资产的解决方案。

b0g*_*usb 1

如果我理解正确的话,您在上游editor和上游都有静态资源dashboard,并且在这两种情况下,URL 是相同的/static/some.resource\n因为您无法根据 URL 进行区分,您可以配置nginxdashboard首先尝试该文件是否存在,如果找不到则将请求代理到editor

\n\n
 upstream editor {\n    server editor:80;\n   }\n\n   upstream dashboard {\n     server dashboard:80;\n   }\n\n   server {\n     location /static {\n     # Send 404s to editor\n     error_page 404 = @editor;\n     proxy_intercept_errors on;\n\n     proxy_pass http://dashboard\n\n     }\n\n     location @editor {\n       # If dashboard does not have the file try with editor\n       proxy_pass http://editor\n     }\n   }\n
Run Code Online (Sandbox Code Playgroud)\n\n

另请参阅nginx \xe2\x80\x93 在多个指定位置或服务器上尝试文件

\n\n

希望能帮助到你。

\n