简体   繁体   中英

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

I use docker-compose with the following 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

The nginx conf is:

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)(/.+)$;
      }    
    }
}

It work well if I create index.html . But if I want to access index.php , I have a 403 error and the following log in 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"

I've tried advices found in other Q&A (like here ), but I can't get it to work.

From your log I see that you're trying to access / location. And I don't see entries in your nginx config which will intercept such request. So I assume following in that case:

  1. for requests to / nginx tries serve static files from specified root ;
  2. by-default index file is index.html and you don't override this setting. So thats why it starts to work fine when you add index.html ;
  3. by-default directory index is forbidden and this is what nginx tries to do in case index file is missing. So you're getting 403 response and I clearly see that in your log.

Most likely it should start to work if you make request with /index.php

What you should do to make request to site root work is add something like that:

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

Keep in mind that instead of /index.php$is_args$args in your case should be something specific to script/framework you're using.

I had the same error, but my problem was permission on the main folder. The html folder itself had no permission. In my case running the command "chmod -R 755" solved. With "-R" I also made sure that all the subdirectories had the same permission (Note that this may cause a security problem depending on your case, my server is a test server so I didn't bother).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM