使用 nginx、PHP-FPM 和 docker 的 PHP 文件出现 403 错误

403 error on PHP files with nginx, PHP-FPM and docker

我使用 docker-compose 与以下 docker-compose.yml:

web_db:
   image: mariadb:latest
   restart: always
   volumes:
    - ./var/mysql:/var/lib/mysql
   environment:
    MYSQL_ROOT_PASSWORD: "1234"

web_front:
  image: nginx
  restart: always
  ports:
    - 80:80
  links:
    - web_fpm
  volumes:
    - ./www:/var/www/html:rw
    - ./etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro

web_fpm:
  build: ./PHP-FPM/
  restart: always
  links:
    - web_db:mysql
  volumes:
    - ./www:/var/www/html

nginx conf 是:

worker_processes  1;


events {
    worker_connections  1024;
}

http {
    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";  
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server {
      listen         80;
      server_name    localhost;
      root /var/www/html;

   location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        }
          root           /var/www/html;
          fastcgi_pass   web_fpm:9000;
          fastcgi_index  index.php;
          fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
          include        fastcgi_params;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
      }    
    }
}

如果我创建 index.html,效果很好。但是如果我要访问index.php,出现403错误,下面登录docker-compose:

web_front_1  | 2016/05/12 12:59:44 [error] 7#7: *1 directory index of "/var/www/html/" is forbidden, client: IP, server: localhost, request: "GET / HTTP/1.1", host: "IP"
web_front_1  | IP - - [12/May/2016:12:59:44 +0000] "GET / HTTP/1.1" 403 197 "-" "Mozilla/5.0 (Linux; Android 6.0.1; A0001 Build/MMB29X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.105 Mobile Safari/537.36"

我已经尝试过在其他问答中找到的建议(例如 here),但我无法让它发挥作用。

从您的日志中我看到您正在尝试访问 / 位置。而且我没有在您的 nginx 配置中看到会拦截此类请求的条目。所以我假设在这种情况下如下:

  1. 对于 / 的请求,nginx 会尝试从指定的 root 提供静态文件;
  2. 默认情况下索引文件是 index.html 并且您不会覆盖此设置。这就是为什么当您添加 index.html;
  3. 时它开始正常工作的原因
  4. 默认目录索引是被禁止的,这是 nginx 在索引文件丢失的情况下尝试做的事情。所以你收到了 403 响应,我在你的日志中清楚地看到了。

如果您使用 /index.php

提出请求,它很可能会开始工作

要向站点根工作发出请求,您应该做的是添加类似的内容:

location / {
  root      /var/www/html;
  try_files $uri /index.php$is_args$args;
}

请记住,在您的情况下,应该使用特定于您正在使用的 script/framework 的 /index.php$is_args$args