I have a site in which I want to create and delete folders with staging wordpress sites.

the structure is something like this:

/wp1
/wp2
/wp3
/...

I am using nginx and I am aware that in order to make this work I have to create multiple location blocks to capture each wordpress site:

location /wp1 {
    try_files $uri $uri/ /wp1/index.php?$args;
}
location /wp2 {
    try_files $uri $uri/ /wp2/index.php?$args;
}
location /wp3 {
    try_files $uri $uri/ /wp3/index.php?$args;
}
...

That coonfiguration works perfectly, but is hard to mantain, so I was trying a regex with the location in order to only use one location block for all sites so I can just delete and create folders not having to worry with the nginx setup:

location ~ ^/wp(?<staging>\d+) {
    try_files $uri $uri/ /wp$staging/index.php?$args;
}

But this does not work. Any ideas on what am I missing?

This is my full configuration file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /home/city/sites/staging1/html;
    index index.php;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }
    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }
    location / {
            try_files $uri $uri/ /index.php?$args;
    }
    location ~ ^/stg(?<staging>\d+) {
            try_files $uri $uri/ /stg$staging/index.php?$args;
    }
    location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/run/php/php7.0-fpm.city.sock;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }
}

This is the output of curl -I http://ipaddress/wp2

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 17 Jan 2017 03:45:43 GMT
Content-Type: text/html
Content-Length: 178
Location: http://ipaddress/wp2/
Connection: keep-alive

This is the output of curl -I http://ipaddress/wp2/

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 17 Jan 2017 03:45:49 GMT
Content-Type: application/octet-stream
Content-Length: 418
Last-Modified: Wed, 25 Sep 2013 00:18:11 GMT
Connection: keep-alive
ETag: "52422bc3-1a2"
Accept-Ranges: bytes

But this downloads the index.php wordpress file, the content type is application/octet-stream which is not what I should get.

This is an expected curl output without the regex location block, a simple wordpress site:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 17 Jan 2017 04:30:30 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Link: <http://ipaddress/wp2/wp-json/>; rel="https://api.w.org/"
Link: <http://ipaddress/wp2/>; rel=shortlink 

On the nginx log files I can see that the regex location is being used, but I don't get what is the problem and why I get that response:

[debug] 24359#24359: *1330 test location: "/"
[debug] 24359#24359: *1330 test location: "favicon.ico"
[debug] 24359#24359: *1330 test location: "robots.txt"
[debug] 24359#24359: *1330 test location: ~ "^/wp(?<staging>\d+)"
[debug] 24359#24359: *1330 http regex set $staging to "2"
[debug] 24359#24359: *1330 using configuration "^/wp(?<staging>\d+)"
[debug] 24359#24359: *1330 http cl:-1 max:1048576
[debug] 24359#24359: *1330 rewrite phase: 3
[debug] 24359#24359: *1330 post rewrite phase: 4
[debug] 24359#24359: *1330 generic phase: 5
[debug] 24359#24359: *1330 generic phase: 6
[debug] 24359#24359: *1330 generic phase: 7
[debug] 24359#24359: *1330 access phase: 8
[debug] 24359#24359: *1330 access phase: 9
[debug] 24359#24359: *1330 access phase: 10
[debug] 24359#24359: *1330 post access phase: 11
[debug] 24359#24359: *1330 try files phase: 12
[debug] 24359#24359: *1330 http script var: "/wp2"
[debug] 24359#24359: *1330 trying to use file: "/wp2" "/home/city/sites/staging1/html/wp2"
[debug] 24359#24359: *1330 http script var: "/wp2"
[debug] 24359#24359: *1330 trying to use dir: "/wp2" "/home/city/sites/staging1/html/wp2"
[debug] 24359#24359: *1330 try file uri: "/wp2"
[debug] 24359#24359: *1330 content phase: 13
[debug] 24359#24359: *1330 content phase: 14
[debug] 24359#24359: *1330 content phase: 15
[debug] 24359#24359: *1330 content phase: 16
[debug] 24359#24359: *1330 content phase: 17
[debug] 24359#24359: *1330 http filename: "/home/city/sites/staging1/html/wp2"
  • The curl with the 200 response code and nginx debug logs look like it works. Can you more precisely describe the "does not work" behavior? Does it give an error in the log, does it not show up in the web browser, etc. Can you show the applicable access log entry? – Tim Jan 17 '17 at 4:18
  • Hi @Tim, the 200 response has a Content-Type: application/octet-stream which is not what is expect and just downloads the index.php file but it is just called "download". This is a wordpress site and should be plain html. I have clarified that in my question. Thank you. – archemiro Jan 17 '17 at 4:48
  • 1
    I wonder if the problem is just that it's not invoking php properly. – Tim Jan 17 '17 at 4:49
  • @Tim. Thats it, thank you so much. It is solved. I was not aware that the file that was downloaded was the php file as it had no name, but now I get it and is solved. I will update my question with the solution. It was just not executint the location ~ \.php$ { ... } so I added this location inside the regex location to solve it. – archemiro Jan 17 '17 at 4:56
  • I'll add an answer so you can mark the question closed. If you can post an answer to your own question, do that, and I'll delete my answer. Editing your question doesn't mark the question as solved. – Tim Jan 17 '17 at 5:14

The problem appears to be that your PHP isn't being executed, it's just being downloaded.

As per your comment you should describe the solution, either in your own answer, a comment on this answer, or editing your question. I think things are a bit limited for users with lower reputation numbers.

  • I have posted the solution as an answer. First I was making sure it was working properly with the new implementation. Thanks!! – archemiro Jan 17 '17 at 5:24
  • It says I can accept my answer in two days. So I mark this as this is already solved. – archemiro Jan 17 '17 at 5:33
up vote 1 down vote accepted

The problem was that the regex location was not using the:

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.city.sock;
}

so what was happening was that the php file were not executing.

To solve the problem just added a location ~ \.php$ inside the problematic regex location:

location ~ ^/stg_(?<staging>\w+) {
    try_files $uri $uri/ /stg_$staging/index.php?$args;
    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.0-fpm.city.sock;
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.