nginx 位置和根设置不起作用

nginx 位置和根设置不起作用

我正在尝试将 cdn.minora.me/forum/ 之后的每个文件重写到另一个目录:

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;
    }
}

我尝试访问的文件,例如:

cdn.minora.me/forum/stylesheet.css

肯定存在,例如上面的样式表可以在我的服务器上的以下路径下找到

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

但是当我尝试访问例如时,nginx 出现 404 not found 错误

cdn.minora.me/forum/stylesheet.css

看起来它根本没有使用该规则,这里是 nginx 错误日志的一部分:

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/[email protected] 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"

替换也root不起作用alias。有人能帮忙吗?

答案1

您的错误日志所显示的问题表明您的location /forum/...匹配不成功。

    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 ~

此外,使用alias和正则表达式匹配时,别名必须包含用于捕获部分的变量location

这应该可以解决问题。

    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;
    }

相关内容