nginx – How to Get Around Using Alias in Nginx

nginxWordpress

Due to a long-standing bug in nginx I was advised to switch my alias directive with a root directive. Unfortunately this now breaks my static files, which are located in a different directory to my site path (mysite.com/blog -> /var/www/mysite/wordpress). How can I get around this, without changing my static file structure? Here's my full location block:

location /blog {
    root /var/www/mysite/wordpress;
    try_files $uri $uri/ /blog/index.php$is_args$args;

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_split_path_info ^(?:\/blog\/)(.+\.php)(.*);
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

Best Answer

Rewrite the URI :

location /blog {

    root /var/www/mysite/wordpress;
    rewrite ^/blog/([^.]+\.[^.]+)$ /$1 break;
    try_files $uri $uri/ /blog/index.php$is_args$args;

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_split_path_info ^(?:\/blog\/)(.+\.php)(.*);
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }

}

This will remove the /blog part of the URI for URIs containing a potential file suffix in it (something.something).