Nginx - wordpress in a subdirectory, what data should be passed?

Matthew picture Matthew · May 27, 2011 · Viewed 33.5k times · Source

I've tried so many different things. The point I'm at right now is this:

location ^~ /wordpress {
    alias /var/www/example.com/wordpress;
    index index.php index.html index.htm;
    try_files $uri $uri/ /wordpress/index.php;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(/wordpress)(/.*)$;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/wordpress/index.php;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Right now, all resources as far as I can tell (images, etc) are loading correctly. And http://www.example.com/wordpress loads wordpress, but a page that says "page not found". (Wordpress is in use for this though). If I try any post urls I get the same result, "page not found". So I know the problem is that wordpress isn't obtaining the data about the path or something. Another potential problem is that if I run example.com/wp-admin.php then it will still run index.php.

What data needs to be passed? What may be going wrong here?

Answer

kolbyjack picture kolbyjack · May 27, 2011

Since your location alias end match, you should just use root. Also, not everything is routed through index.php on wordpress afaik. Also, unless you know you need path info, you probably dont. I think you want something like:

location @wp {
  rewrite ^/wordpress(.*) /wordpress/index.php?q=$1;
}

location ^~ /wordpress {
    root /var/www/example.com;
    index index.php index.html index.htm;
    try_files $uri $uri/ @wp;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass 127.0.0.1:9000;
    }
}

or if you really do need path info (urls look like /wordpress/index.php/foo/bar):

location ^~ /wordpress {
    root /var/www/example.com;
    index index.php index.html index.htm;
    try_files $uri $uri/ /wordpress/index.php;

    location ~ \.php {
        fastcgi_split_path_info ^(.*\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_pass 127.0.0.1:9000;
    }
}

EDIT: Updated first server{} to strip initial /wordpress from uri and pass remainder as q param

EDIT2: Named locations are only valid at server level