Enabling URL Rewriting for CodeIgniter on NGINX
The configuration snippet you provided establishes a standard PHP-FPM backend connection but fails to support pretty URLs, which is critical for modern frameworks like CodeIgniter. The current location / block uses try_files $uri $uri/ =404;. This directive attempts to serve a static file first; if it doesn't exist, it looks for a directory. If neither exists, it returns a 404 error immediately. It never passes the request to your PHP application to handle the routing logic.
To fix this, you must explicitly route all requests that do not resolve to static assets (images, CSS, JS) to your PHP handler. CodeIgniter relies on its index.php entry point to parse the URL and map it to internal controllers. Without this interception, NGINX will attempt to serve a non-existent file for every request like /about or /products, resulting in 404s before your router ever gets a chance to work.
The core of the fix lies in adjusting the location / block and ensuring the PHP location block has priority. Here is the corrected configuration structure:
server {
listen 80;
listen [::]:80;
server_name 192.168.56.101 192.168.101.100 localhost;
# Point the root specifically to your CI folder
root /var/www/html/ci;
index index.php index.html index.htm;
# Handle static assets efficiently
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
try_files $uri =404;
expires 30d;
add_header Cache-Control "public, no-transform";
}
# Handle PHP requests
location ~ \.php$ {
try_files $uri /index.php?$query_string;
include fastcgi_params;
# Ensure SCRIPT_FILENAME includes the root path defined above
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
# Security: Disable autoindex and set buffer limits
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
# Deny access to hidden files like .htaccess (standard for Linux)
location ~ /\.ht {
deny all;
}
error_page 404 /index.php;
error_page 500 502 503 504 /index.php;
}
The Critical Change: try_files Logic
The most significant alteration is within the PHP location block. Instead of blindly returning a 404 if $uri doesn't exist, we now instruct NGINX to rewrite the request path to /index.php while preserving the original query string via $query_string.
location ~ \.php$ {
try_files $uri /index.php?$query_string;
...
}
This tells NGINX: "If you can't find a file ending in .php at that specific path, rewrite the URL to /index.php and pass along the arguments." CodeIgniter's router then interprets the URI (e.g., domain.com/about) based on your .htaccess or app/routes.php configuration.
Security Considerations
Your original config included a generic location / { try_files $uri $uri/ =404; }. This is dangerous for PHP applications because it inadvertently blocks access to your entry point if the file structure isn't perfectly flat, and it prevents NGINX from leveraging its caching capabilities for static assets. By separating the handling of static files (images, CSS) from dynamic content, we improve performance by serving static hits directly from disk without involving the PHP-FPM process.
Additionally, ensure that your php.ini file has open_basedir restricted appropriately if you are deploying in a shared environment, though for a dedicated VPS or container, this is less of a concern. Always verify that the SCRIPT_FILENAME parameter includes your actual root path (/var/www/html/ci). If NGINX thinks the document root is /var/www/html, it will look for files outside your CI folder when processing requests from within it, potentially exposing sensitive system files or causing 403/404 errors.
Deployment Steps
- Save the configuration to
/etc/nginx/conf.d/default.conf(or a more specific name likeci.conf). - Run a syntax check before reloading:
sudo nginx -t - If successful, reload NGINX to apply changes without dropping active connections:
sudo systemctl reload nginx
If you are managing multiple PHP applications on the same server, consider moving this configuration into a dedicated file like /etc/nginx/sites-available/ci and enabling it via a symlink in sites-enabled. This practice prevents conflicts if you decide to update your CodeIgniter installation or switch frameworks later. For more advanced routing strategies involving subdomains or specific path prefixes, NGINX variables allow for granular control, but the principle of routing unknown paths to an entry point remains the same.