如何使用Nginx代理静态文件到多个主机?

时间:2016-08-09 14:31:25

标签: nginx reverse-proxy

我有两个在nginx docker容器上运行的应用程序,每个都是一个前端应用程序,有css,JS等。 我需要通过反向代理(另一个容器中的nginx)访问, 但是在代理容器中搜索静态文件而不是rigth容器(app.local或login.local)。

我的代理配置:

server {
    listen       80;
    server_name  localhost;

    location /login {

        rewrite /login(.*) $1 break;

        proxy_pass http://login.local:80;
        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_set_header X-Forwarded-Proto $scheme;
    }

    location /app {

        rewrite /app(.*) $1 break;

        proxy_pass http://app.local:80;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

我的nginx代理日志:

192.168.150.13 - - [09/Aug/2016:13:59:13 +0000] "GET /login HTTP/1.1" 200 574 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" "-"
2016/08/09 13:59:13 [error] 334#334: *118 open() "/etc/nginx/html/css/main.css" failed (2: No such file or directory), client: 192.168.150.13, server: machine.local, request: "GET /css/main.css?1470692258780 HTTP/1.1", host: "machine.local:7000", referrer: "http://machine.local:7000/login"
192.168.150.13 - - [09/Aug/2016:13:59:13 +0000] "GET /css/main.css?1470692258780 HTTP/1.1" 404 571 "http://machine.local:7000/login" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" "-"

1 个答案:

答案 0 :(得分:0)

您需要使用Nginx Http子模块修改代理内容,以便为返回的内容网址添加前缀。

http://nginx.org/en/docs/http/ngx_http_sub_module.html