0

I'd like to use nginx to have one location used for just the root url (https://example.com/) and serve all other file from a different location. Eventually I want to use this to do authentication on / before proxying, but initially I'm just trying with simple file locations.

Based on the documentation I'm using two location blocks:

server {
  listen  443 ssl;
  ssl_certificate      /etc/nginx/cert.pem;
  ssl_certificate_key  /etc/nginx/cert.key;

  location = / {
    root /usr/share/nginx/html;
    index index.html;
  }

  location / {
    root /usr/share/nginx/html2;
  }
}

But requests for https:// example.com/ are getting /usr/share/nginx/html2/index.html rather than /usr/share/nginx/html/index.html

Similarly, I've tried:

  location = / {
    root /usr/share/nginx/html;
    index index.html;
  }

  location ~* ^/.* {
    root /usr/share/nginx/html2;
  }

But I get the same result.

Is there a straightforward way to get this to work?

1

That's because the index directive will make an internal redirect to /index.html thus it's matching the second location block. Change the first block to this :

location ~ ^/(index\.html)?$ {
    root /usr/share/nginx/html;
    index index.html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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