908553dae8d610409778e8b7dcdc5376eaf5ef71
By:
cfcindia

nginx configuration: additional Location throws 404

December 6, 2014 3.2k views

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

2 Answers

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.

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 defaultserver;
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?

Have another answer? Share your knowledge.