Question

nginx configuration: additional Location throws 404

  • Posted on December 6, 2014
  • cfcindiaAsked by cfcindia

I am trying to modify my drupal droplet nginx configuration to serve files from an another location.

/var/www/html/mysite <-- already configured and works fine and is accessible through the webserver

/var/www/html/resources <-- I want to configure the webserver to serve content from this directory

The modification I made in /etc/nginx/sites-available/drupal is not taking effect

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  root /var/www/html/mysite;
  index index.php index.html index.htm;
.
.
.
location ~* ^/more/ { 
root /var/www/html/resources; 
index index.html index.htm; 
try_files $uri $uri/ =404; 
}

}

Still resources served from under the /resources is throwing 404 for e.g. http://cfcindia.org/resources/en/books/pdf/AGF.pdf

what is the nginx configuration changes to serve files from /resources?

I am an nginx newbie, and have researched existing community questions, and the internet, but could not get it to work. HELP!

Rabi


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi Kamain7,

Thanks for the suggestion, and it makes sense.

However replacing in my nginx conf as below still does not work

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on;

root /var/www/html/mysite; index index.php index.html index.htm; . . . location /more { alias /var/www/html/resources; index index.html index.htm; }

I did restart nginx as you suggested.

Suggestions?

Because of the way nginx handles the root directive, it would try to serve /var/www/html/resources/more/en/books/pdf/AGF.pdf when you browsed to http://cfcindia.org/more/en/books/pdf/AGF.pdf. Replace it with alias and you should be good to go:

location /more { 
    alias /var/www/html/resources; 
    index index.html index.htm;
}

Don’t forget to restart nginx so that the changes take effect:

sudo service nginx restart

Check out the nginx documentation on alias and root.