Codeigniter 404 error on nginx

Date Posted:24-05-2017

Recently, we deployed a codeigniter application on one of our server and found that it shows 404 error when we try to access the custom urls(such as domain.tld/login, domain.tld/admin, domain.tld/dashboard). The same application works fine on Apache webserver.

After a long research, we found that it was due to redirection issue on nginx configuration.

Resolution:

This post assumes that nginx is running and we are deploying the codeigniter on the documentroot.

Open the nginx configuration and add the below lines which applies to all the / directory.

vi /etc/nginx/sites-enabled/default

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

Incase, if there is a separate configuration for domain, then we need to edit the configuration accordingly/.

Verify the nginx syntax

nginx -t

Restart the nginx service.

systemctl restart nginx

Additional Information:

If we deploy the application on the subdirectory (say for example /directory) then we need to apply the below redirection rule.

location /directory1 {
try_files $uri $uri/ /directory1/index.php;
}

Verify the syntax and restart the nginx service.

 

 

Leave a Reply