Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Location For Static Location in Nginx Configuration

Tags:

nginx

I have a two locations where my app will serve static files, one is /my/path/project/static and the other is /my/path/project/jsutils/static.

I'm having a hard time getting the webserver to look in both directories for static content. Here is my entry for static location in the nginx configuration file for my app.

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

I get an error that says : "alias" directive is duplicate, "root" directive was specified earlier.

I'm not sure how to go about having nginx look in both these paths for static content.

Thank you in advance for any help.

like image 486
pratik Avatar asked Apr 01 '13 04:04

pratik


2 Answers

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;
}

So first the file will be searched in /my/path/project/static and if that could not be found there, the secondStatic location will be triggered where the root is changed to /my/path/project/jsutils/static.

like image 124
Ludwig Avatar answered Sep 19 '22 23:09

Ludwig


You may use try_files (http://wiki.nginx.org/HttpCoreModule#try_files). Assuming that you static files are in /my/path/project/static and /my/path/project/jsutils/static. you can try this:

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

Let me know if it works. Thanks!

like image 27
Chuan Ma Avatar answered Sep 21 '22 23:09

Chuan Ma