nginxnginx-location

How can I have same rule for two locations in NGINX config?


How can I have same rule for two locations in NGINX config?

I have tried the following

server {
  location /first/location/ | /second/location/ {
  ..
  ..
  }
}

but nginx reload threw this error:

nginx: [emerg] invalid number of arguments in "location" directive**

Solution

  • Try

    location ~ ^/(first/location|second/location)/ {
      ...
    }
    

    The ~ means to use a regular expression for the url. The ^ means to check from the first character. This will look for a / followed by either of the locations and then another /. Quoting from the docs,

    A regular expression is preceded with the tilde (~) for case-sensitive matching, or the tilde-asterisk (~*) for case-insensitive matching.