Nginx的php-禁止使用403

时间:2018-07-09 21:46:29

标签: php nginx yii yii2 yii2-advanced-app

我想用nginx运行项目yii2(高级模板)。我将virtualbox与vagrant(ubuntu 16.04,php 5.6)一起使用

我有以下设置nginx文件

//

vhost1.conf
server {
   listen                *:80;

   server_name  frontend.test;
   client_max_body_size 128m;

   root /var/www/frontend/web/;
     index index.php;

   access_log            /var/log/nginx/vhost1.access.log;
   error_log             /var/log/nginx/vhost1.error.log;
   location / {
     try_files $uri $uri/ /index.php$is_args$args;
   }

   location ~ ^/assets/.*\.php$ {
       deny all;
   }

   location ~ \.php$ {
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     fastcgi_pass 127.0.0.1:9000;
     #fastcgi_pass unix:/var/run/php5-fpm.sock;
     try_files $uri =404;
   }

   location ~* /\. {
     deny all;
   }
 }

我有以下经许可的结构项目:

vagrant@machine1]-[/var/www]-[git master] 
$ ls -la frontend/
total 68
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 ./
drwxrwxr-x 1 vagrant vagrant 4096 Jul  9 16:14 ../
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 assets/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 bootstrap/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 components/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 config/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 controllers/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 data/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 helpers/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 messages/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 models/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 modules/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 runtime/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 validators/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 views/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 web/
drwxrwxr-x 1 vagrant vagrant 4096 Jul  5 14:27 widgets/

nginx错误日志输出:

2018/07/09 21:42:36 [error] 23865#23865: *1 directory index of "/var/www/frontend/web/" is forbidden, client: 192.168.56.1, server: b2bfrontend.test, request: "GET / HTTP/1.1", host: "b2bfrontend.test"

如果我运行 b2bfrontend.test ,则会收到错误消息- 403禁止访问

3 个答案:

答案 0 :(得分:2)

只需修复以下位置:

location / {
  try_files $uri $uri/ /index.php$is_args$args;
}

收件人:

location / {
  try_files $uri /index.php$is_args$args;
}



原因:它尝试转到$uri/(因为它存在)/var/www/frontend/web/,并进行似乎不允许的目录索引。

消息已经说过:

  

“ / var / www / frontend / web /”的目录索引被禁止

答案 1 :(得分:0)

您的nginx服务器很可能在用户nginx或www-data下运行,而您的文件具有vagrant:vagran所有权。检查正在运行nginx的用户是什么,并相应地更改文件的所有权。另外,如果您将php-fpm用于php,请检查php-fpm的配置,并查看在那里定义了哪些用户。

答案 2 :(得分:0)

我取消注释此行,现在它可以工作了:

fastcgi_pass unix:/var/run/php5-fpm.sock;

在我的情况下,我有Debian 9,所以我对此进行了一些更改:

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

我的完整代码是:

location ~ \.(php|twig)$   {
            try_files $uri =404;
    include       fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;

    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}