【问题标题】:CentOS, Nginx: 403 Forbidden in Laravel's folderCentOS,Nginx:Laravel 文件夹中的 403 Forbidden
【发布时间】:2018-08-05 18:38:44
【问题描述】:

我使用laravel new test建立Laravel项目并将所有目录移动到Nginx默认根目录/usr/share/nginx/html/。 为了检查nginx的配置是否正确,我简单的配置一下,trun onautoindex,把index,html放到/usr/share/nginx/html/test/public/,也就是laravel的index.php的位置。

default.conf 的 nginx

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    root   /usr/share/nginx/html/test;
    index  index.html index.htm index.php;

    location / {
        autoindex on;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

当我通过localhost/public/ 访问index.html 时,nginx 返回403。我将index.html 复制到一个名为/usr/share/nginx/html/test/public_b 的新文件夹中,并尝试通过localhost/public_b/ 访问新的index.html。 Nginx 将正确的内容返回给我!这就像一个奇迹。我的目录的统计数据如下。

$ll /usr/share/html/test
total 432
drwxrwxr-x.  6 nginx nginx     84 Feb 26 15:39 app
-rw-rw-r--.  1 nginx nginx   1686 Feb 26 15:39 artisan
drwxrwxr-x.  3 nginx nginx     34 Feb 26 15:39 bootstrap
-rw-rw-r--.  1 nginx nginx   1512 Feb 26 15:39 composer.json
-rw-rw-r--.  1 nginx nginx 145089 Feb 26 15:39 composer.lock
drwxrwxr-x.  2 nginx nginx    247 Feb 26 15:39 config
drwxrwxr-x.  5 nginx nginx     72 Feb 26 15:39 database
-rwxrwxr-x.  1 nginx nginx     13 Feb 26 17:46 index.html
-rw-rw-r--.  1 nginx nginx   1150 Feb 26 15:39 package.json
-rw-rw-r--.  1 nginx nginx   1040 Feb 26 15:39 phpunit.xml
drwxr-xr-x.  4 root  root     116 Feb 26 18:05 public
drwxr-xr-x.  2 root  root      24 Feb 26 17:50 public_c
drwxrwxr-x.  5 nginx nginx     45 Feb 26 15:39 resources
drwxrwxr-x.  2 nginx nginx     75 Feb 26 16:50 routes
-rw-rw-r--.  1 nginx nginx    563 Feb 26 15:39 server.php
drwxr-xr-x.  5 nginx nginx     46 Feb 26 15:39 storage
drwxrwxr-x.  4 nginx nginx     83 Feb 26 15:39 tests
drwxrwxr-x. 37 nginx nginx   4096 Feb 26 15:40 vendor
-rw-rw-r--.  1 nginx nginx    549 Feb 26 15:39 webpack.mix.js
-rw-rw-r--.  1 nginx nginx 258941 Feb 26 15:39 yarn.lock

$ ll /usr/share/nginx/html/test/public
total 12
drwxrwxr-x. 2 nginx nginx   21 Feb 26 15:39 css
-rw-rw-r--. 1 nginx nginx    0 Feb 26 15:39 favicon.ico
-rwxr-xr-x. 1 root  root    13 Feb 26 17:49 index.html
-rw-rw-r--. 1 nginx nginx 1823 Feb 26 15:39 index.php
drwxrwxr-x. 2 nginx nginx   20 Feb 26 15:39 js
-rw-rw-r--. 1 nginx nginx   24 Feb 26 15:39 robots.txt

$ ll /usr/share/nginx/html/test/public_c/
total 4
-rwxr-xr-x. 1 root root 13 Feb 26 17:50 index.html

我已经阅读了很多相关的问题和页面,但没有任何帮助解决这个问题。 我的操作或设置有什么问题?


更新

感谢@Adam Kozlowski。我尝试了chmod 777 public -R并将所有者更改为root和nginx,但问题没有解决。

【问题讨论】:

  • 你配置php-fpm了吗?
  • nginx 需要它来运行php。我很确定,如果你在/usr/share/nginx/html/test/public/ 中添加index.html,它会显示一些东西,而不是403
  • @DharmaSaputra 我已经配置了php-fpm。但是我想我已经通过nginx的配置切断了nginxphp-fpm之间的联系。
  • 我希望nginx 也会显示我创建的 HTML 的内容。但是 nginx 显示 403。
  • @DharmaSaputra Thnx 很多...它救了我!添加 index.html 就像魅力一样!!

标签: php laravel nginx


【解决方案1】:

为目录设置适当的权限,作为root用户:

chmod a+rwx directory-name -R

【讨论】:

    【解决方案2】:

    我发现了问题:SELinux 阻止了 Nginx 访问文件夹。

    $ ls /usr/share/nginx/html/test/ -Z
    drwxr-xr-x. root  root  unconfined_u:object_r:config_home_t:s0 public
    drwxr-xr-x. root  root  unconfined_u:object_r:httpd_sys_content_t:s0 public_c
    

    只有带有httpd_sys_content_t标签的文件才能被HTTP访问。

    命令:

    chcon -R -t httpd_sys_content_t /usr/share/nginx/html/test

    我还需要将 httpd_sys_rw_content_t 添加到 /usr/share/nginx/html/test/storage 以让 Laravel 工作。

    命令:

    chcon -R -t httpd_sys_rw_content_t/usr/share/nginx/html/test/storage

    参考Permissions Issue with Laravel on CentOS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2015-12-05
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多