2

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?

masegaloeh
  • 17,978
  • 9
  • 56
  • 104
lenovouser
  • 23
  • 8

1 Answers1

2

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;
    }
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Still getting 404 not found – lenovouser Dec 23 '14 at 14:55
  • Also location ~ ^/ doesnt work – lenovouser Dec 23 '14 at 14:56
  • 1
    @SilasRech recognise that this is the first fundamental error - if it still "doesn't work" debug/investigate and find out why. The [debug log](http://nginx.org/en/docs/debugging_log.html) is very useful for seeing what happens; as is the error message itself (if the error message is no different, the url doesn't match the regex). – AD7six Dec 23 '14 at 19:20
  • 1
    Also, [read the docs](http://nginx.org/en/docs/http/ngx_http_core_module.html#alias): `If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures`. – AD7six Dec 23 '14 at 20:36
  • 1
    @AD7six Aha, you're right! – Michael Hampton Dec 23 '14 at 21:25
  • I dont know, somehow nginx keeps ignoring everything... Even `location /forum/stylesheet.css { alias /srv/minora.me/webservices/forum.minora.me/public/stylesheet.css; }` Does not work. – lenovouser Dec 23 '14 at 22:43
  • 1
    Silas - which probably means you're editing the wrong file, not reloading nginx or some other similar error - personally whenever I find a problem of this kind it's because the application/code isn't reading the same code I am; enable the debug log, read and find out. Michael: (_cough_) root :) (simpler, logically equivalent to that alias =)). – AD7six Dec 26 '14 at 09:21