7

I have really simple nginx configuration with 3 locations inside. Each of them have it's own root directory + I should be able to add another in the future easily.

What I want:

Request /admin => location ^/admin(/|$)

Request /admin/ => location ^/admin(/|$)

Request /admin/blabla => location ^/admin(/|$)

Request /client => location ^/client(/|$)

Request /client/ => location ^/client(/|$)

Request /client/blabla => location ^/client(/|$)

Request /blabla => location /

Request /admin-blabla => location /

Request /client-blabla => location /

Actual result:

All requests goes to location /.

I tried many different suggestions from docs, stackoverflow and other sources using different combinations of aliases, try_files, roots and regexes, but nothing worked for me.

Only when I tried to use just return 200 'admin'; and return 200 'front' it worked as intended.

Minimal config:

server {
    listen 80;
    index index.html;

    location / {
        root /var/www/html/www_new/front;
        try_files $uri $uri/ /index.html;
    }

    location ~ ^/admin(/|$) {
        root /var/www/html/www_new/admin;
        try_files $uri $uri/ /index.html;
    }

    location ~ ^/client(/|$) {
        root /var/www/html/www_new/client;
        try_files $uri $uri/ /index.html;
    }
}

Directory structure:

  • /admin
  • /client
  • /front

Thank you

David Kudera
  • 806
  • 2
  • 8
  • 14
  • Do set the 'default' root at the top of your server{} block - this is a best-practice and described in [Pitfalls and Common Mistakes](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/) – Ed Randall Oct 19 '18 at 08:24

1 Answers1

11

When you change the root it'll still include the directory name, so what you want to do is only set the root for location /. You also don't need any additional regex on /admin as the location modifier ~ already tells nginx 'anything starting with'.

This works for your use case:

server {
    listen 80;
    index index.html;

    location / {
        root /var/www/html/www_new/front;
        try_files $uri $uri/ /index.html;
    }

    location ~ ^/admin {
        root /var/www/html/www_new; # the directory (/admin) will be appended to this, so don't include it in the root otherwise it'll look for /var/www/html/www_new/admin/admin
        try_files $uri $uri/ /admin/index.html; # try_files will need to be relative to root
    }
}
sjdaws
  • 3,466
  • 16
  • 20
  • Thank you for your answer. I have to keep the whole regex in admin location, because I don't want to match eg. admin-blabla. Now the only problem is that url localhost:15000/admin is for some reason redirected to localhost/admin/ (without port). – David Kudera Jun 11 '17 at 07:56
  • Got ya, is `localhost:15000/admin/` also redirected (with trailing slash)? – sjdaws Jun 11 '17 at 07:59
  • /admin/ or /admin/blabla works fine. Problem is only without slash. Also there is nothing more in my config right now – David Kudera Jun 11 '17 at 08:40
  • You need to remove `$uri/` from your try files then it will no longer redirect. – sjdaws Jun 11 '17 at 08:47