1

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.

Nrgyzer
  • 783
  • 1
  • 14
  • 38
  • Pretty sure you just need a default index? https://stackoverflow.com/questions/10002439/make-index-html-default-but-allow-index-php-to-be-visited-if-typed-in – Joe May 28 '17 at 08:07
  • Does nginx has a "DefaultIndex" directive in the configuration file? The link provided shows the directive for htaccess but I'm not using htaccess. – Nrgyzer May 28 '17 at 08:14
  • It wouldn't be an nginx thing, it would be a web server config thing. I don't know how to do it with all of the possible server setups. I know that a DirectoryIndex declaration in the .htaccess will work if you don't specify an index in the URL, but I don't know how to make a web server do that automatically. I know that the server can be set to throw a forbidden error for directory traversal, which is why a lot of sites just have a blank index.html in directories they don't want people playing around in. – Joe May 28 '17 at 08:19
  • You cannot use the `index` directive with your split root scheme. It relies on the files being in the same directory. – Richard Smith May 28 '17 at 08:27

1 Answers1

0

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.

Sakura Kinomoto
  • 1,784
  • 2
  • 21
  • 30