6

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;
    }
}
Jonathan
  • 1,309
  • 2
  • 22
  • 29

1 Answers1

10

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).

Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • I've noticed this doesn't work with a file with multiple periods (e.g. style.main.css). Is there any way of simplifying this? I'm thinking something along the lines of `rewrite ^/blog/(.*)$ /$1 break;`. – Jonathan Nov 06 '14 at 22:36
  • @Jonathan Multiple periods seems a weird way of naming files to me. You can use `rewrite ^/blog/(.*\.[^.]+)$ /$1 break;` if you can't change file names. – Xavier Lucas Nov 06 '14 at 22:38
  • 1
    `.min.css` and `.min.js` is a fairly standard convention for minified files (take a look at StackOverflow's source!). I ended up going with `^/blog/(.+)$ /$1 break;` for the sake of simplicity. Many thanks again. – Jonathan Nov 06 '14 at 22:43
  • @Jonathan Didn't say it was not commonly used. Still, I prefer subfolders. Personal opinion :) – Xavier Lucas Nov 06 '14 at 22:48
  • Fair enough... :) – Jonathan Nov 06 '14 at 23:06
  • I searched google about 2 hours for this. Thank you! – pyronaur Jul 14 '17 at 11:35