nginx location and root settings dont work

lenovouser asked:

I am trying to rewrite every file after cdn.minora.me/forum/ to another directory:

server {
    listen   443 ssl;
    listen   [::]:443 ssl;
    ssl on;
    ssl_certificate /etc/nginx/ssl/minora.me.crt;
    ssl_certificate_key /etc/nginx/ssl/minora.me.key;    
    root /srv/minora.me/webservices/cdn.minora.me;
    index index.html index.htm index.php;
    server_name cdn.minora.me;
    location ~* .(eot|ttf|woff|js|css|svg)$ {
    add_header Access-Control-Allow-Origin *;
    }
    location /forum/(images|language|sounds|templates|uploads|vendor|src/modules|nodebb.min.js(.map)?|stylesheet.css|admin.css) {
        alias /srv/minora.me/webservices/forum.minora.me/public/;
    }
    location / {
        try_files $uri $uri/ /index.php;
    }
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm-cdn.minora.me.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    location ~ /.ht {
        deny all;
    }
}

The files I am trying to access, e.g.:

cdn.minora.me/forum/stylesheet.css

definitely exist, for example the stylesheet above can be found under the following path on my server

/srv/minora.me/webservices/forum.minora.me/public/stylesheet.css.

But nginx is trowing an 404 not found error when I am trying to access e.g.

cdn.minora.me/forum/stylesheet.css

It seems like it doesnt use the rule at all, here a part of the nginx error log:

2014/12/23 15:30:51 [error] 30277#0: *8 open() "/srv/minora.me/webservices/cdn.minora.me/forum/stylesheet.css" failed (2: No such file or directory), client: 141.101.105.127, server: cdn.minora.me, request: "GET /forum/stylesheet.css HTTP/1.1", host: "cdn.minora.me"
2014/12/23 15:30:53 [error] 30277#0: *9 open() "/srv/minora.me/webservices/cdn.minora.me/forum/stylesheet.css" failed (2: No such file or directory), client: 141.101.105.127, server: cdn.minora.me, request: "GET /forum/stylesheet.css HTTP/1.1", host: "cdn.minora.me"
2014/12/23 15:30:57 [error] 30277#0: *9 open() "/srv/minora.me/webservices/cdn.minora.me/forum/stylesheet.css" failed (2: No such file or directory), client: 141.101.105.127, server: cdn.minora.me, request: "GET /forum/stylesheet.css HTTP/1.1", host: "cdn.minora.me"
2014/12/23 15:31:14 [error] 30277#0: *23 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 141.101.93.222, server: img.minora.me, request: "HEAD /assets/loader/main@2x.gif HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm-img.minora.me.sock:", host: "img.minora.me", referrer: "https://minora.me/"
2014/12/23 15:35:24 [error] 30281#0: *31 open() "/srv/minora.me/webservices/cdn.minora.me/forum/stylesheet.css" failed (2: No such file or directory), client: 141.101.105.127, server: cdn.minora.me, request: "GET /forum/stylesheet.css HTTP/1.1", host: "cdn.minora.me"
2014/12/23 15:35:26 [error] 30278#0: *43 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 141.101.105.168, server: cdn.minora.me, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm-cdn.minora.me.sock:", host: "cdn.minora.me"

Also replacing root with alias doesnt work. Can anyone help?

My answer:


The problem shown by your error log indicates that your location /forum/... is not being matched.

    location /forum/(images|language|sounds|templates|uploads|vendor|src/modules|nodebb.min.js(.map)?|stylesheet.css|admin.css) {
        alias /srv/minora.me/webservices/forum.minora.me/public/;
    }

The problem I see here is that this isn’t treated as a regex match, but is being treated literally, because it doesn’t start with location ~.

Further, with alias and a regex match, the alias must include a variable for the captured part of the location.

This should resolve the problem.

    location ~ /forum/(images|language|sounds|templates|uploads|vendor|src/modules|nodebb.min.js(.map)?|stylesheet.css|admin.css) {
        alias /srv/minora.me/webservices/forum.minora.me/public/$1;
    }

View the full question and answer on Server Fault.

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