Using 'set' values in 'location'?

I’m still working on getting nginx installed but I want to get a head
start on the configuration. I have a couple questions regarding ‘set’:

  1. Can I use variables I create with ‘set’ in ‘location’ as regular
    strings? Such as:
server {
  set $subdir "/somesubdir";

  location $subdir/ {
    try_files $uri $uri/ $subdir/index.php;
  1. Can I use variables I create with ‘set’ in ‘location’ regular
    expressions? Such as:
server {
  set $subdir "/somesubdir";

  location ~ ^$subdir(.*/)?anotherdir/$ {
    rewrite ^ $subdir/index.php last;

I recognize that probably isn’t correct syntax for the regular
expression portion since $ indicates ‘match end of string’, but my goal
here should be clear.

Also open to alternative solutions. I am aware that “if is evil” and
I’ve read the “pitfalls” wiki documentation, so I was trying to avoid
using ‘if’ and try to use ‘location’ and ‘rewrite’/‘try_files’
exclusively. I also don’t have access to a working nginx server yet or
I’d probably just test it myself.


Posted at Nginx Forum:,170551,170551#msg-170551

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs