17

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
     }
28

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
    }
}
  • 1
    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
  • 3
    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

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.