Report this

What is the reason for this report?

Nginx fails to redirect '/' to '/index.php'

Posted on May 27, 2017

Nginx fails to call index.php file at ‘/’ endpoint. In /etc/nginx/sites-available/default, I have added location / section to rewrite to ‘/index.php’ (see below). However, when I enter my-site.com to the url bar, I get 403 Forbidden error ([error] 18041#18041: *2 directory index of “/var/www/html/” is forbidden).

When I enter my-site.com/index.php, it works correctly. Also, I’m able to see a page other than 403 Forbidden if I add an ‘index.html’ fie to my document directory, /var/www/html. Even with fastcgi_index index.php; and removed ‘index.html’, I don’t see the index.php file in ‘/’ endpoint.

/etc/nginx/sites-available/default

server {
	listen 80;
	listen [::]:80;
	server_name my-site.com www.my-site.com;
	return 301 https://$server_name$request_url;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    include snippets/ssl-my-site.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name my-site.com www.my-site.com;

    location ~ [^/].php(/|$) {
        fastcgi_split_path_info ^(.+?.php)(/.*)$;
        
        if (!-f /var/www/html$fastcgi_script_name) {
            return 404;
        }
    
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        fastcgi_pass unix:var/run/php/php7.0-fpm.sock;
        #fastcgi_pass  127.0.0.1:9000;
        fastcgi_read_timeout 300;
    }

    location ~ /.ht {
        deny all;
    }
    
    location ~ /.well-known {
        allow all;
    }

   location /phpmyadmin {
                index index.php;
    }

    # Rewrite rules for WordPress Multi-site.
    if (!-e $request_filename) {
            rewrite /wp-admin$ $scheme://$host$uri/ permanent;
            rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
            rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
    }
}


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

hi friends i to have the same issue i have a website named www.example.tk when i enter this i first get .html file. i only want to display index.php, if i enter following url i.e www.example.tk/index.php so, i dont want index.php to be displayed in my url, i tried changing all configuration file but nothing helped me, so could you please help me. my mailed id is rajulasandeep@gmail.com pls mail if you have any solutions for it.

This comment has been deleted

@wag0325

Generally, for /, you’d have a location block like this:

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

…which is missing in the configuration that you’ve posted. That’s a standard / location block for just about any CMS or blog using PHP.

I’d recommend adding that block to your configuration, and then restarting NGINX.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.