At work we are switching from a shared LAMP stack to a VPS running nginx. I’m significantly more comfortable with Apache but learning to use Nginx has been pretty exciting (or as exciting as learning to configure a new webserver can be.
The current issue is this: on several domains we need to remove index.php from the URL for canonicalization. This is the last thing that we currently need to take care of for these domains.
I’ve been researching plenty of different techniques for removing index.php from a URL (most of which are specifically for CI or ExpressionEngine) and I’ve tried to adapt several of these for personal use but I end up with an infinite loop error which I can only imagine is related to the following:
location / {
try_files $uri $uri/ /index.php?$args =404;
}
I’m hell-bent on learning how all of this works but right now I need to ask for help figuring this out so that we can move forward and I can figure out what it is that I’m doing wrong.
I would greatly appreciate any responses and will further appreciate anyone who is willing to go a little in-depth on the subject to help someone new like myself and anyone else who might be in a similar situation that reads this.
Thank you!
UPDATE
To make things easier I’m just going to put my nginx config up here for this vhost.
server {
listen 80;
server_name examplesite.com;
# redirect non-www to www. for canonical urls
rewrite ^/(.*) http://www.examplesite.com/$1 permanent;
}
server {
listen 80;
server_name www.examplesite.com;
error_log /srv/http/nginx/examplesite.com/log/nginx-error.log;
access_log /srv/http/nginx/examplesite.com/log/nginx-access.log;
root /srv/http/nginx/examplesite.com/root;
location / {
try_files $uri $uri/ /index.php?$args =404;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_param PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}
Try this …
Not 100% about the index.php rewrite line as there is a chance it might put you in a redirect loop. If that is the case, then the answer is to just serve the index.php requests but change all links in the application to remove them so that they disappear over time.