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;
}
}
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
I’ve setup quite a few WordPress and WordPress MS instances on NGINX and I’ve never ran in to an instance where q=$uri&$args
was needed instead of just ?$args
.
So instead of:
try_files $uri $uri/ /index.php?q=$uri&$args;
I would recommend using:
try_files $uri $uri/ /index.php?$args;
…
That being said, I would recommend creating a backup of default
, and starting clean. Once you’ve got a backup of default
, run the following command.
This will wipe this file clean, so make sure you have a backup.
truncate -s 0 /etc/nginx/sites-available/default
Once that’s done, open it back up:
nano /etc/nginx/sites-available/default
and paste in:
server {
listen 80;
listen [::]:80;
server_name pinkbulletin.com www.pinkbulletin.com;
return 301 https://$server_name$request_url;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-pinkbulletin.com.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.php index.html;
server_name pinkbulletin.com www.pinkbulletin.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
location /phpmyadmin {
index index.php;
}
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;
}
}
Now, make a backup of /etc/nginx/fastcgi_params
, and then run:
truncate -s 0 /etc/nginx/fastcgi_params
Now open it up:
nano /etc/nginx/fastcgi_params
and paste in:
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 512k;
fastcgi_buffers 512 16k;
fastcgi_busy_buffers_size 1m;
fastcgi_temp_file_write_size 4m;
fastcgi_max_temp_file_size 4m;
fastcgi_intercept_errors off;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param HTTP_PROXY "";
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
Save the file, exit and then restart NGINX.
Once you’ve got those changes applied, let’s see what happens then. If any errors pop up, check the logs again by running:
tail -20 /var/log/nginx/error.log
and paste that to a code block and we’ll continue to troubleshoot :-).
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.
UPDATED /etc/nginx/sites-available/default