If you’re encountering a “404 Not Found” error with Nginx while running a CodeIgniter application, the issue is usually related to the Nginx configuration or the CodeIgniter URL routing. Here’s a step-by-step guide to resolve this issue:
1. Check Nginx Configuration:
- Open your Nginx configuration file. This file is usually located in /etc/nginx/sites-available/your_site.conf or /etc/nginx/nginx.conf.
- Ensure your server block is correctly set up to handle CodeIgniter’s URL structure. Here’s an example of a typical Nginx configuration for a CodeIgniter application:
nginx
server {
listen 80;
server_name your_domain.com;
root /var/www/your_codeigniter_project;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
} location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust based on your PHP version
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
} -
Make sure to replace your_domain.com and /var/www/your_codeigniter_project with your actual domain and project path.
2. Ensure Proper Handling of Index.php:
- CodeIgniter typically uses index.php as the front controller for all incoming requests. Ensure that Nginx is correctly routing requests to index.php.
- The try_files directive in the above configuration handles this by trying the requested URI first and then falling back to index.php.
3. Check Permissions:
- Ensure that the Nginx user (usually www-data) has the necessary read and execute permissions on your CodeIgniter project directory and files.
- You can set the appropriate permissions using:
bash
sudo chown -R www-data:www-data /var/www/your_codeigniter_project
sudo chmod -R 755 /var/www/your_codeigniter_project
4. Remove index.php
from URLs (Optional):
- If you’re trying to remove index.php from the URLs, you need to adjust CodeIgniter’s configuration and ensure Nginx is properly set up.
- In your application/config/config.php, set:
php
$config['index_page'] = '';
- Ensure the try_files directive in the Nginx configuration includes index.php as shown earlier.
5. Restart Nginx:
- After making any changes to the Nginx configuration, you need to restart Nginx to apply the changes:
bash
sudo systemctl restart nginx
6. Check CodeIgniter Routes:
- If the Nginx configuration is correct, but you’re still encountering a 404 error, check your CodeIgniter application/config/routes.php file to ensure that the routing is correctly configured.
- Ensure that the default controller is correctly set, like this:
php
$route['default_controller'] = 'welcome'; // Replace 'welcome' with your default controller
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
7. Review the CodeIgniter Environment:
- Ensure that your CodeIgniter environment is set to production if you’re running on a live server. This can be set in the index.php file at the root of your CodeIgniter project:
php
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
By following these steps, you should be able to resolve the “404 Not Found” error when running a CodeIgniter application on Nginx. If the problem persists, double-check the configuration files for any typos or missing directives.