我该如何处理不同服务器中的静态文件

时间:2018-06-11 08:55:03

标签: nginx

upstream app_server {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
 }

upstream another_server {
    server 192.168.128.11;
}

server {
    server_name test.com;
    listen 80;


    location       / {
        proxy_pass http://app_server;
        proxy_set_header Host $host;
    }

    location    ~ \.(js|css|jpeg|jpg|png|gif|ico|mp4|json) {
        root     /www/app;
        try_files $uri /$1/$2.$4;
    }

    location ~ /anotherapp {
        proxy_pass http://another_server;
        proxy_set_header Host $host;
    }
}

我在不同的机器上有两个服务器。我不知道如何处理another_server中的静态文件。

当我获得test.com/anotherapp/index.js资源时,它会返回app_server中的文件,但不会返回another_app服务器中的文件。

问题是如何处理another_app服务器中的静态文件

1 个答案:

答案 0 :(得分:0)

按顺序计算正则表达式location语句,直到找到匹配的正则表达式。

所以/anotherapp/index.js在匹配\.(js|css|jpeg|jpg|png|gif|ico|mp4|json)之前匹配/anotherapp

您有两种选择:

或者,颠倒正则表达式location块的顺序,以便首先遇到location ~ /anotherapp

或者,使用带有location修饰符的前缀^~(它始终优先于任何正则表达式location),例如:

location ^~ /anotherapp { ... }

有关详细信息,请参阅this document