Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am quite new to managing http server. How should I configure nginx to achieve behaviour like this: When there is a request for, nginx should serve index.html from /var/www/foo directory. When there is a request for, nginx should serve index.html from /var/www/bar directory.

The second part of URL (foo, bar) should not be hardcoded in nginx.conf, I need something like variable.

This is part of my nginx.conf, I tried something with variable in location section but I can't get this working.

server {

        #access_log  logs/localhost.access.log  main;

        location ~/(\d+) {
            root   /var/www/$1;
            index  index.html index.htm;
share|improve this question
You don't need to do anything special for this! Set root /var/www; and you're done. – Michael Hampton Apr 2 '14 at 19:37
This indeed works, however only if I call or How to get this working with my domain:, ? I tried putting server_name;, but didn't help. – user1091733 Apr 2 '14 at 20:47

1 Answer 1

up vote 0 down vote accepted

You just need to set the root folder to make things work as expected.

Setting this within server {...} directive is enough, no need to specify a sub location directive.

Like this :

server {
   root /var/www;
   index  index.html index.htm;

Then, just ensure you have index.html or index.htm in

  • /var/www/foo
  • /var/www/bar
  • /var/www/whateveryouwant
share|improve this answer
Working great but only when URL is like this when I change the address to I get 403 Forbidden. Do you have any idea why this is happening? – user1091733 Apr 2 '14 at 21:55
@user1091733 Should not happens. Please can you update your question with your vhost config ? There is certainly a bad config somewhere. – krisFR Apr 2 '14 at 21:58
@user1091733 My idea is that DNS does not resolve with ip Check DNS resolution. – krisFR Apr 2 '14 at 22:08
Thank you for response. Weird thing - if i put index.html into /var/www and go to - I get the proper result. The problem appears only if I try to access index.html located in /var/www/foo by entering - then I get 403 Forbidden. My vhost config looks excatly like the one you posted in your answer. – user1091733 Apr 2 '14 at 22:42
@user1091733 It could be a permission issue. Check for the x bit for others on /var/www/foo folder : chmod o+x /var/www/foo – krisFR Apr 2 '14 at 22:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.