How to Fix CodeIgniter 404 Errors on Nginx
Introduction
CodeIgniter applications may return a 404 Not Found error when deployed on an Nginx web server, especially when accessing custom application URLs such as /login, /admin, or /dashboard.
The same application may work correctly on Apache because Apache commonly uses .htaccess rules for URL rewriting. Nginx does not process .htaccess files, so the rewrite rules need to be configured directly in the Nginx server configuration.
In this guide, we will explain how to configure Nginx to correctly route CodeIgniter requests to index.php.
Prerequisites
Before proceeding, make sure the following requirements are met:
- Nginx is installed and running on the Ubuntu/Linux server.
- The CodeIgniter application has been deployed on the server.
- PHP and PHP-FPM are installed and running.
- You have
sudoor root access to modify the Nginx configuration. - The Nginx document root points to the correct CodeIgniter application directory.
- You know the PHP-FPM socket or upstream configuration used by Nginx.
For CodeIgniter 4, the recommended document root is normally the application’s public directory.
Implementation
Step 1: Open the Nginx Configuration
Open the Nginx virtual host configuration for your application.
For example:
sudo vi /etc/nginx/sites-enabled/default
If the application has its own virtual host configuration, edit that configuration instead.
Step 2: Configure URL Rewriting
Inside the server block, add the following configuration:
location / {
try_files $uri $uri/ /index.php?$query_string;
}The try_files directive tells Nginx to:
- Check whether the requested file exists.
- Check whether the requested directory exists.
- If neither exists, forward the request to
index.php. - Pass the original query string to the CodeIgniter application.
This allows CodeIgniter to process URLs such as:
/login /admin /dashboard
instead of Nginx returning a 404 error.
Step 3: Configure PHP-FPM
Make sure PHP requests are passed to PHP-FPM.
For example:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}The PHP-FPM socket may be different depending on the PHP version installed on your server.
You can check the available PHP-FPM sockets with:
ls /run/php/
Step 4: CodeIgniter 4 Configuration
For CodeIgniter 4, the Nginx document root should normally point to the public directory:
server {
listen 80;
server_name example.com;
root /var/www/example.com/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}Replace the domain, application path, and PHP-FPM version according to your environment.
Step 5: Verify the Nginx Configuration
Before applying the configuration, check the Nginx syntax:
sudo nginx -t
If the configuration test is successful, reload Nginx:
sudo systemctl reload nginx
Step 6: Test the Application
Open the CodeIgniter application in a browser and test routes such as:
https://example.com/login https://example.com/admin https://example.com/dashboard
The requests should now be handled by CodeIgniter instead of returning an Nginx 404 error.
CodeIgniter Application in a Subdirectory
If the application is deployed under a subdirectory such as:
https://example.com/directory/
the Nginx configuration can be adjusted as follows:
location /directory/ {
try_files $uri $uri/ /directory/index.php?$query_string;
}After making the change:
sudo nginx -t sudo systemctl reload nginx
Troubleshooting
If the 404 error continues, check the following:
Check the Nginx Error Log
sudo tail -f /var/log/nginx/error.log
Check PHP-FPM
sudo systemctl status php8.3-fpm
Replace php8.3-fpm with the PHP version installed on your server.
Verify the Application Files
For example:
ls -la /var/www/example.com/
For CodeIgniter 4:
ls -la /var/www/example.com/public/
Also verify that index.php exists in the expected location.
Conclusion
A CodeIgniter application can return 404 errors on Nginx when application routes are not correctly forwarded to the CodeIgniter front controller.
The key Nginx configuration is:
location / {
try_files $uri $uri/ /index.php?$query_string;
}After adding the configuration, always validate it using nginx -t and reload Nginx.
For CodeIgniter 4, also ensure that the Nginx document root points to the application’s public directory.
Frequently Asked Questions
1. Why does CodeIgniter work on Apache but return 404 on Nginx?
Apache commonly uses .htaccess files for URL rewriting. Nginx does not process .htaccess, so equivalent rewrite behavior must be configured directly in the Nginx configuration.
2. Why does /index.php/login work but /login return 404?
This usually indicates that Nginx is not forwarding clean URLs to CodeIgniter’s index.php. The try_files directive is generally required to handle these requests.
Related Article
Install Nginx, Php, MariaDB on Ubuntu 16.04.1
Install Nginx, PHP-fpm, Mariadb on Centos 7
How to install and configure PHP with Nginx on centos7
