Nginx嵌套位置与路由重写

时间:2017-02-04 08:21:40

标签: php nginx nginx-location

我想将站点中的几个专用路由嵌套到可能不具有相同名称的特定目录中。我无法弄清楚如何重写它用于try_files的路径。

server {           
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/default;

    index index.html;

    server_name _;

    if ($bad_referer) {
        return 444;
    }

    location / {
        try_files $uri $uri/ =404;
    }

    location /postfixadmin/ {
        access_log /var/log/nginx/postfixadmin/access.log;
        error_log /var/log/nginx/postfixadmin/error.log;

        root /var/www/postfixadmin/;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php;


        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
            expires 168h;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }

    location /email/ {

        #access_log /var/log/nginx/roundcube/access.log;
        #error_log /var/log/nginx/roundcube/error.log;

        root /var/www/roundcube/;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php;

        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
            expires 168h;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }
}

当我导航到www.site.com/email时,我收到了404,我认为这是因为它正在寻找/var/www/roundcube/email/index.php,而不是try_files。存在。在account之前重写文件路径需要做什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,事实证明这很简单。使用alias而不是root仅使用与location匹配的部分之后的字符串部分,因此我查找了正确的目录。另一个问题是PHP没有传递正确的脚本名称,所以它仍然在错误的位置。解决方案是传入fastcgi_param SCRIPT_FILENAME $request_filename;。我也能够摆脱try_files部分,尽管我并不是100%确定原因。

以下是工作解决方案:

server {           
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/default;

    index index.html;

    server_name _;

    if ($bad_referer) {
        return 444;
    }

    try_files $uri $uri/ =404;

    location /postfixadmin {
        alias /var/www/postfixadmin/;
        index index.php index.html index.htm;

        location ~ /postfixadmin/.+\.php$ {
            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
            expires 168h;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }

    location /email {
        alias /var/www/roundcube/;
        index index.php index.html index.htm;

        location ~ /email/.+\.php$ {
            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ {
            expires 168h;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }
}