2013-04-01 108 views
3

我有两个位置,我的应用程序将提供静态文件,一个是/ my/path/project/static,另一个是/ my/path/project/jsutils/static。Nginx配置中的静态位置多个位置

我很难让Web服务器在两个目录中查找静态内容。这里是我的应用程序的nginx配置文件中的静态位置条目。

location ^~ /static { 
     root /my/path/project/static; 
     alias /my/path/project/jsutils/static; 
     index index.html index.htm; 
    } 

我得到一个错误,说:“别名”指令是重复的,“root”指令是先前指定的。

我不确定如何让nginx看起来在这两个静态内容的路径。

非常感谢您的帮助。

回答

2

您可以使用try_files(http://wiki.nginx.org/HttpCoreModule#try_files)。假设你的静态文件位于/ my/path/project/static和/ my/path/project/jsutils/static中。你可以试试这个:

location ^~ /static { 
    root /my/path/project; 
    index index.html index.htm; 
    try_files $uri $uri/ /jsutils$uri /jsutils$uri/ =404; 
} 

让我知道它是否有效。谢谢!

0

就实现你的配置nginx的语言:

location /my/path/project/static { 
    try_files $uri =404; 
} 
location /my/path/project/jsutils/static { 
    try_files $uri =404; 
} 
+0

这两种解决方案都不起作用。我想我可能在Django应用程序路径中有不正确的地方。当我使用内部网络服务器在本地运行应用程序时,两个路径都可以访问。 – pratik

+0

可能我没理解你。请指定这种静态文件的请求行的样子。 –

0

我有完全相同的问题,它看起来像nginx的时候rootalias覆盖不喜欢。我固定它通过首先去除root声明是在服务器节内,而不是在位置的部分(注意注释掉线)宣布rootalias适当地直接:

server { 
# root /usr/share/nginx/html; 
    location /logs/ { 
     root /home/user/develop/app_test; 
     autoindex on; 
    } 
    location /logs2/ { 
#  root /home/user/branches/app_test; 
     alias /home/user/branches/app_test/logs/; 
     autoindex on; 
    } 
} 
0
location ^~ /static { 
    root /my/path/project/static; 
    index index.html index.htm; 
    try_files $uri $uri/ @secondStatic; 
} 

location @secondStatic { 
    root /my/path/project/jsutils/static; 
} 

所以第一次将文件将在/ my/path/project/static中搜索,如果在那里找不到,则会在根目录更改为/ my/path/project/jsutils/static的情况下触发secondStatic位置。