Nginxconfiguration中的静态位置的多个位置

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

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

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

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

我不知道如何让nginx看起来在这两个静态内容的path。

预先感谢您的任何帮助。

你可以使用try_files( http://wiki.nginx.org/HttpCoremodulee#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; } 

让我知道如果它的作品。 谢谢!

只需用nginx语言实现你的配置:

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

我有完全相同的问题,它看起来像nginx不喜欢何时被alias覆盖。 我通过首先删除服务器部分内部的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; } } 
 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的地方触发。