37

I have some locations on my server. I want to catch all other locations which users give via browser. How to to that? For example

 server {
     ...
     location /location1 {
              do something;
     }
     location /location2 {
              do something;
     }
     location /all_other_locations {
            return 301 http://www.google.de
     }
alabamajack
  • 475
  • 1
  • 4
  • 5

1 Answers1

54

nginx's locations are prefix based (except regexp ones), so location / matches all requests unless more specific one matches.

server {
    location / {
        # catch all unless more specific location match
    }

    location /location1 {
        # do something
    }

    location /location2 {
        # do domething
    }
}
Alexey Ten
  • 8,435
  • 1
  • 34
  • 36
  • 2
    What if you wanted `/` to match something specific and then everything else be caught by something else? Would you use wildcard matching then? – Tim Tisdall Mar 21 '16 at 15:14
  • 9
    ah.. I think it may be to use `location = /`. The explicit `/` request will be matched by it and everything else will go to `location /` unless something else matches. – Tim Tisdall Mar 21 '16 at 15:44