【问题标题】:setup laravel for nginx (error with permission denied)为 nginx 设置 laravel(权限被拒绝的错误)
【发布时间】:2014-11-24 21:32:50
【问题描述】:

我尝试为 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,但 decoder.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/ 文件夹中的文件列表:

-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 服务器上进行。

请帮帮我,对不起我的英语不好:)

【问题讨论】:

    标签: php laravel nginx


    【解决方案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;
         }
    }
    

    【讨论】:

    • 谢谢!它正在工作,但我有新错误:[crit] 25475#0: *1 stat() "/var/www/decoder-now/public/" failed (13: Permission denied) [error] 25475#0: *1 connect() 连接到上游时失败(111:连接被拒绝),客户端:127.0.0.1,服务器:decoder.lan,请求:“GET / HTTP/1.1”,上游:“fastcgi://127.0.0.1:9001”我尝试将端口:9000 更改为 9001,但它不再工作了
    • 我一般把 nginx 用户加到 www-data 组。这可能会解决您的权限问题。使用这个命令:sudo usermod -a -G www-data nginx
    • 另外,如果您的 php.ini 设置为使用 IP 地址,您需要更新我的配置文件中的 fastcgi_pass 行,因为它使用的是 unix sock。
    • 我没有nginx用户,对吧?我必须创造他吗?
    • 在你的服务器上安装 Nginx 应该会创建用户。
    【解决方案2】:

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

    第二件事,永远无法到达您的@php@handler 位置。

    第三件事,你的try_files 指令负责产生一个内部重定向循环。只需举一个示例 URL,花点时间思考一下 nginx 会做什么:

    1. 你打了http://decoder.lan/address/
    2. try files 指令在运行时等同于try_files /address/ /address// @rewrite;
    3. @rewrite 命名位置将导致重写 URI:/index.php?_url=/address/
    4. 如果没有为 rewrite 指令指定标志,默认行为是使用 last 标志,这意味着它会在内部重定向到 /index.php?_url=/address/ 并一次又一次地进入 the try_files

    因此,您实际上可能需要使用try_files $uri $uri/ /index.php?_url=$request_uri;并将location @php更改为location ~\.php$,而不是使用try_files $uri $uri/ @rewrite;

    【讨论】:

    • 谢谢!它正在工作,但我有新错误:[crit] 25475#0: *1 stat() "/var/www/decoder-now/public/" failed (13: Permission denied) [error] 25475#0: *1 connect() 在连接到上游时失败(111:连接被拒绝),客户端:127.0.0.1,服务器:decoder.lan,请求:“GET / HTTP/1.1”,上游:“fastcgi://127.0.0.1:9001”我尝试将端口:9000 更改为 9001,但它不再工作了
    • 我尝试从 /var/run/php5-fpm.sock 开始,又是它的错误
    • @Roman 确保在 nginx.conf 中使用 user www-data; 并且 php-fpm 正在侦听正确的端口(检查 netstat -lntp 用于 TCP 或 netstat -lnxp 用于 Unix 套接字)。跨度>
    • 是的,我使用 www-data。来自 nginx.conf 的第一行 - 用户 www-data;
    • 关于正确的端口:我返回更改为 /var/run/php5-fpm.sock 因为在 netstat -lnxp 中找到
    猜你喜欢
    • 2016-04-21
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2018-12-15
    • 2020-08-25
    • 2021-03-27
    相关资源
    最近更新 更多