0

I'd like to create a rule to match all the paths except some.

  • site.com/
  • site.com/admin/
  • site.com/user1/
  • site.com/user2/
  • site.com/user3/

Here's what I have so far but looks like something wrong.

location / {
  # configuration for homepage
}
location /admin/ {
  # configuration for admin
}
location ~* ^/(.*)$ {
  # configuration for other pages
}

So how I can change the last rule to match all paths?

ozgrozer
  • 101
  • 3
  • `location /` matches all paths not matched by any other `location`. To match only `/`, you need to use `location = /` – Richard Smith Oct 28 '20 at 13:50
  • @RichardSmith Even when I use `location = /`, it still matches the 3rd rule on all the paths. So when I go site.com/ or site.com/admin/ it just the 3rd rule looks like the matching one. But I need different rules for homepage, admin and the other pages. How can I achieve that? – ozgrozer Oct 28 '20 at 14:12
  • The 3rd rule is wrong. The 3rd rule should be `location /` to match any URI not matched by any other `location`. And depending on what your configuration is for your homepage, you may need other rules to correctly render all of its resources. – Richard Smith Oct 28 '20 at 14:16
  • OK so now first rule is `location = /` and the third rule is `location /` but when try to open the homepage, it's again the third rule is matching. So site.com/ looks like working as site.com/index.html I guess. I think I'm missing something. – ozgrozer Oct 28 '20 at 14:45
  • It depends on how you implement the contents of the first `location` block. Some directives cause Nginx to internally rewrite the URI which means you also need to match the rewritten URI. Also, `index.html` may also contain URLs for js and css resources, which arrive at the server as separate requests and also need to be handled correctly. – Richard Smith Oct 28 '20 at 14:54
  • Adding `location = /index.html` did the job for now. Thank you. – ozgrozer Oct 28 '20 at 15:06

0 Answers0