为nginx设置laravel(错误,权限被拒绝)

时间:2014-11-24 12:46:08

标签: php laravel nginx

我尝试为nginx设置我的laravel。我的配置:

server {
    listen 80;
    server_name decoder.lan;
    root /var/www/decoder-now/public;

    index index.php;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location @php { ## Depending on your Nginx version, you might need to change this to location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location @handler {
      rewrite / /index.php;
    }

}

我在error.log中收到错误:

  2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [crit] 14063#0: *10 stat() "/var/www/decoder-now/public/index.php" failed (13: Permission denied), client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"
    2014/11/24 15:13:43 [error] 14063#0: *10 rewrite or internal redirection cycle while redirect to named location "@rewrite", client: 127.0.0.1, server: decoder.lan, request: "GET /address/ HTTP/1.1", host: "decoder.lan"

此示例使用url:decoder.lan / address,但decode.lan /也不起作用。

access.log文件:

127.0.0.1 - - [24/Nov/2014:15:13:43 +0300] "GET /address/ HTTP/1.1" 500 603 "-" "Mozilla/5.0 (X11; Linux i686 (x86_64)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36"

我试过检查工作听或不是这个文件。我通过控制台命令检查这个:

sudo -u www-data cat index.php
来自控制台文件的

已打开。

我使用www-data用户,因为在/ect/php5/fpm/pool.d/www.conf中我有:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

文件列表在... / decoder-now / public / folder:

-rwxr-xr-x 1 www-data  www-data 2238 нояб. 17 19:01 favicon.ico
-rwxrwxrwx 1 www-data  www-data 1586 нояб. 17 19:01 index.php
drwxr-xr-x 2 www-data  www-data 4096 нояб. 17 19:01 packages
-rwxr-xr-x 1 www-data  www-data   24 нояб. 17 19:01 robots.txt

在这个实例上,我已经使用过Apache服务器了,但是他现在已经断电了。所有请求都在nginx服务器上。

请帮助我,抱歉我的英语不好:)

2 个答案:

答案 0 :(得分:1)

这是我用于Laravel网站的Nginx设置。

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name SITE_URL;

    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log;
    rewrite_log     on;
    access_log      on;

    root WEB_ROOT;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    location ~* \.php$ {
        fastcgi_pass                unix:/var/run/php5-fpm.sock;
        fastcgi_index               index.php;
        fastcgi_split_path_info     ^(.+\.php)(.*)$;
        include                     /etc/nginx/fastcgi_params;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with Nginx.
     location ~ /\.ht {
         deny all;
     }
}

答案 1 :(得分:0)

首先,您似乎有权利问题,检查用户nginx工作进程正在运行(用户指令)。

其次,您的@php@handler位置永远不会到达。

第三,你的try_files指令负责产生内部重定向循环。只需要一个示例URL并花些时间考虑nginx将做什么:

  1. 您点击了http://decoder.lan/address/
  2. try files指令在运行时与try_files /address/ /address// @rewrite;
  3. 等效
  4. @rewrite命名位置将导致重写的URI:/index.php?_url=/address/
  5. 未指定重写指令的标志时的默认行为是使用last标志,这意味着它在内部重定向到/index.php?_url=/address/并一次又一次进入the try_files
  6. 因此,您可能需要使用try_files $uri $uri/ @rewrite;并将try_files $uri $uri/ /index.php?_url=$request_uri;更改为location @php

    ,而不是使用location ~\.php$