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

nothing_authentic picture nothing_authentic · Feb 10, 2016 · Viewed 121.1k times · Source

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**

Answer

curtwphillips picture curtwphillips · Feb 12, 2016

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 /.