nginx + wordpress in subfolder + debian configuration

beta asked:

In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog

site config looks as follows:

server {
        listen 80;
        listen [::]:80;
        root /var/www/example.com/html; 
        index index.php index.html index.htm index.nginx-debian.html;   
        server_name example.com www.example.com;

        location /blog {
                alias /var/www/example.comblog/html;
                index index.php;
                try_files $uri $uri/ /blog/index.php?q=$uri&$args;
        }

        location ~ /blog/.+\.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        location ~ /\.ht {
                deny all;
        }
}
  • The wordpress files reside in the folder
    /var/www/example.comblog/html. When accessing example.com/blog,
    the browser shows a 404 error.

  • In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0

  • nginx version: nginx/1.6.2

  • /var/log/nginx/error.log does not show anything of interest

UPDATE 1:

After setting error logging to debug, (among others) the following lines appear. Maybe this helps:

open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/staat/index.php"

The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/staat/index.php instead of /var/www/example.com/html/staat/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/staat/index.php.

Update 2:

Okay there seems to be a long standing issue with using alias together with try_files.

My answer:


You are not running into the long standing issue with using alias with try_files.

You simply aren’t using alias at all in the appropriate location. Thus the document root is inherited from the level above.

You can either add the alias directive to the location block, or make it a nested location as in the answer you linked to.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.