Unlike Apache's decentralized .htaccess
system, Nginx employs a centralized configuration structure. The primary files you'll work with are:
/etc/nginx/nginx.conf
- Main configuration file (handles global settings)/etc/nginx/sites-available/
- Contains virtual host configurations/etc/nginx/sites-enabled/
- Symbolic links to active configurations
For CodeIgniter and PHP applications, /etc/nginx/sites-available/default (or your custom site configuration) is indeed the correct file for implementing:
server {
listen 80;
server_name example.com;
root /var/www/html;
# CodeIgniter front controller pattern
location / {
try_files $uri $uri/ /index.php?$args;
}
# PHP handling
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Three critical architectural differences:
- Nginx requires server reload (
sudo service nginx reload
) for changes - No per-directory configuration files like .htaccess
- Rewrite rules are written in server/location contexts
Here's a production-ready setup for Ubuntu:
server {
listen 80;
server_name yourdomain.com;
root /var/www/ci_project/public;
index index.php;
# Security headers
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
# Main rewrite rule
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP processing
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
# Block access to hidden files
location ~ /\. {
deny all;
}
}
When facing 404 errors or routing problems:
- Verify
root
points to your public directory - Check PHP-FPM socket path matches your PHP version
- Ensure proper filesystem permissions (www-data:www-data)
Test your configuration with sudo nginx -t
before reloading.
Unlike Apache which uses per-directory .htaccess
files, Nginx employs a centralized configuration approach. The primary configuration files are typically found in these locations on Ubuntu systems:
/etc/nginx/nginx.conf (main configuration file)
/etc/nginx/sites-available/ (virtual host configurations)
/etc/nginx/sites-enabled/ (symlinks to enabled sites)
/etc/nginx/conf.d/ (additional configuration snippets)
For CodeIgniter and PHP applications, you'll primarily work with the virtual host file in /etc/nginx/sites-available/
. The default file you mentioned (/etc/nginx/sites-available/default
) is indeed the correct starting point.
Here's a typical structure for a PHP application:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
For CodeIgniter applications, you need to handle the URI routing properly. Here's a complete working example:
server {
listen 80;
server_name ci-app.example.com;
root /var/www/ci-app/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
expires max;
log_not_found off;
}
}
- Nginx configurations are server-wide, not per-directory
- Changes require server reload (
sudo systemctl reload nginx
) - Syntax is completely different from Apache's directives
- No .htaccess-style override capability by default
Problem: CodeIgniter routes not working properly
Solution: Ensure your try_files
directive includes the query string:
try_files $uri $uri/ /index.php?$query_string;
Problem: PHP files being downloaded instead of executed
Solution: Verify your PHP-FPM socket path and include the proper fastcgi_params:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
include fastcgi_params;