无法从Nginx反向代理后面的Docker容器提供静态资产 [英] Can't serve static assets from docker containers behind Nginx reverse proxy
问题描述
我正在尝试使用Nginx作为反向代理来服务两个容器.这是我的Nginx conf文件的一部分:
I'm trying to use Nginx as a reverse proxy to serve two containers. Here is a part of my Nginx conf file:
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;
}
在浏览器中导航到/editor
URL时出现404错误,因为该页面正在提交对驻留在上游容器编辑器"中的静态资源的请求.
I'm getting 404 errors when I navigate to the /editor
url in my browser because the page is submitting requests for static resources that reside in the upstream container, "editor".
我对Nginx还是很陌生,但是我想它会收到带有url的请求:
http://example.com/static/css/2.3d394414.chunk.css
I'm pretty new to Nginx but I presume when it receives a request with the url:
http://example.com/static/css/2.3d394414.chunk.css
Nginx无法知道相应的CSS位于editor
容器内部.如何修改配置以解决此问题?我已经看到了一些配置,这些配置提供了通往任何静态资产的通用路径,但是我需要一个可以处理Docker容器中资产的解决方案.
Nginx has no way of knowing that the corresponding css is located inside the editor
container. How can I amend the configuration to fix this problem? I've seen some configurations which provide a common path to any static assets but I need a solution that can handle assets within docker containers.
推荐答案
如果我正确理解,您在editor
和dashboard
上游具有静态资源,在两种情况下,URL都是相同的/static/some.resource
由于您无法根据URL进行区分,因此可以配置nginx
首先尝试文件是否在dashboard
上,然后将请求代理到editor
(如果找不到).
If I understood correctly you have static resources on editor
and dashboard
upstreams and in both cases the URL is the same /static/some.resource
Because you cannot differentiate based on the URL you could configure nginx
to try if the file exists on dashboard
first and proxy the request to editor
if not found.
upstream editor {
server editor:80;
}
upstream dashboard {
server dashboard:80;
}
server {
location /static {
# Send 404s to editor
error_page 404 = @editor;
proxy_intercept_errors on;
proxy_pass http://dashboard
}
location @editor {
# If dashboard does not have the file try with editor
proxy_pass http://editor
}
}
希望有帮助.
这篇关于无法从Nginx反向代理后面的Docker容器提供静态资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!