Nrgyzer
Nrgyzer

Reputation: 1075

nginx: 403 forbidden when php files in separate directory

I've successfully installed nginx that uses PHP-FPM but unfortunately I'm having some trouble when loading my php files from a different directory. All my files are located in subdirectories in /var/www/html (e.g. all css-files are located in /var/www/html/css, all javascript-files are located in /var/www/html/js, all php-files are located in /var/www/html/php). According to this, I changed the root directory path for my php files to /var/www/html/php:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php home.php;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

Unfortunately, when accessing my nginx server using my web browser, I'm getting error 403 (Forbidden). When accessing my index.php (http://192.168.2.109/index.php) directly, everything works fine. So, I think it means that the file permissions are correct but nginx isn't able to index the /var/www/html/php directory. Furthermore, /var/log/nginx/error.log includes:

2017/05/28 07:49:56 [error] 13678#13678: *1 directory index of "/var/www/html/" is forbidden, client: 192.168.2.101, server: _, request: "GET / HTTP/1.1", host: "192.168.2.109"

I already tried to enable autoindex and add the index specifier in the "location ~ .php$ {" section without success. The result is the same :(

Does anyone has an idea what I'm doing wrong/missing here? All suggestions in Nginx 403 error: directory index of [folder] is forbidden didn't solve my problem.

Upvotes: 1

Views: 5460

Answers (1)

Sakura Kinomoto
Sakura Kinomoto

Reputation: 1884

The problem are easy:

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

As I can read, when you request a *.php file, you change the root address. But, when you request "default index", you are requesting /, not a .php

You need a new location, for change root path on / request. Try to use this:

    location = / {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

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

Maybe, on this config, you need to put a "rewrite index.php;", but I didn't know because I've never test this configuration.

Don't think my new location order (location = /) is the same as yours (location /), because my order, with the equal sign, only apply when you request exactly the "main" location without any parameter.

Upvotes: 0

Related Questions