location(别名)块禁止使用

时间:2017-01-27 00:51:10

标签: nginx nginx-location

当我访问http://my_ip/cachet/时,我收到了一个禁止错误

location /cachet/ {
    alias /var/www/cachet/public;
    try_files $uri $uri/ /index.php?$query_string;

    location ~ \.php$ {
        fastcgi_pass    unix:/run/php/php7.0-fpm.sock;
        fastcgi_index   index.php;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

当我转到http://my_ip/cachet/index.php时,它说"文件未找到。"

Nginx应该能够阅读cachet文件夹

drwxr-xr-x 13 www-data www-data 4096 jan 27 01:26 cachet

运行PHP和nginx进程:

root@h2638935:/var/www# ps aux -P | grep nginx
root     21623  0.0  0.0 125756  1684 ?        Ss   01:43   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 21624  0.0  0.0 125756  2300 ?        S    01:43   0:00 nginx: worker process
www-data 21625  0.0  0.0 125756  2992 ?        S    01:43   0:00 nginx: worker process
root     21636  0.0  0.0  12888  1120 pts/0    S+   01:50   0:00 grep --color=auto nginx
root@h2638935:/var/www# ps aux -P | grep php
root     17562  0.0  0.4 280252 19476 ?        Ss   jan26   0:00 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data 17563  0.0  0.6 285472 26408 ?        S    jan26   0:00 php-fpm: pool www
www-data 17564  0.0  0.6 285792 26708 ?        S    jan26   0:00 php-fpm: pool www
root     21638  0.0  0.0  12888  1116 pts/0    S+   01:50   0:00 grep --color=auto php

2 个答案:

答案 0 :(得分:0)

尝试:

location ^~ /cachet/ {
    alias /var/www/cachet/public/;
    if (!-e $request_filename) { rewrite ^ /cachet/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass    unix:/run/php/php7.0-fpm.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $request_filename;
    }
}

答案 1 :(得分:0)

永远不要忘记try_files指令的行为: try_files指令的最后一个参数导致重新评估当前作用域中的所有location块(通常为server)。

/index.php?$query_string;/cachet/前缀不匹配,因此永远不会到达PHP引擎的嵌套位置。

相关问题