Routing in CodeIgniter with Nginx While setting up Codeigniter in Nginx if a 404 error occurs when you have to set Codeigniter for subfolders in Nginx.
Why Choose Nginx?
Nginx is known for its high performance, stability, and low resource consumption. It’s an excellent choice for serving web applications, including those built with CodeIgniter. Its ability to handle a large number of simultaneous connections makes it a preferred web server for many developers.
Configuration Steps
1. Install Nginx
First, ensure that Nginx is installed on your server. You can install it using the package manager:
sudo apt-get update
sudo apt-get install nginx
2 Configure Nginx for CodeIgniter
Open the Nginx Configuration File
Edit the default configuration file located at /etc/nginx/sites-available/default
:
sudo nano /etc/nginx/sites-available/default
Add the CodeIgniter Configuration
Add the following code to the configuration file to handle requests properly:
server {
listen 80;
server_name yourdomain.com;
root /var/www/html/CodeIgniter-3-1-4;
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
3. CodeIgniter in a Subfolder
If your CodeIgniter application is in a subfolder, adjust the location
block accordingly:
For that open /etc/Nginx/sites-available/default and then add the following code to the configuration:
location /subfolder {
try_files $uri $uri/ /subfolder/index.php?$query_string;
}
4. Restart Nginx
After making the changes, restart Nginx to apply the new configuration:
sudo systemctl restart nginx
Conclusion
Configuring CodeIgniter with Nginx requires careful setup, but with the right configuration, you can achieve a robust and efficient web server environment. By following the steps outlined above and ensuring your Nginx and PHP-FPM settings are correctly configured, you can avoid common pitfalls and ensure your CodeIgniter application runs smoothly.