CentOS 7下Nginx+CodeIgniter配置异常求助
Hey there! Let's get your CodeIgniter project running smoothly on Nginx. Since you've already got Nginx, MySQL, and php-fpm set up with a working phpinfo, we just need to tweak your Nginx config to handle CI's single-entry routing properly.
Common Pitfall You're Likely Hitting
CodeIgniter relies on URL rewriting to funnel all requests through index.php (unlike Apache's .htaccess rules). Your config cuts off mid-location block, which means you're probably missing the critical rewrite logic and proper PHP handling that CI needs.
Here's a Complete, Working Server Block for Your CI Project
Replace your incomplete server block with this, and adjust paths/PHP versions to match your setup:
server { listen 80; server_name 173.249.40.xxx; # Point root to your CodeIgniter project's root directory root /usr/share/nginx/html/ci; index index.php index.html index.htm; # Serve static files directly (css, js, images) to skip unnecessary PHP processing location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # Block access to sensitive CI directories (never expose these publicly!) location ~ ^/(application|system|tests)/ { deny all; return 403; } # Core CI routing: send all non-static requests to index.php location / { try_files $uri $uri/ /index.php?$query_string; } # Handle PHP files with php-fpm location ~ \.php$ { # Update this socket path to match your PHP version (check /run/php/ for options) fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; # Ensure the correct script path is passed to php-fpm fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Critical Post-Config Checks
- Verify php-fpm Socket Path: Run
ls /run/php/to see your actual socket file (e.g.,php8.2-fpm.sockfor PHP 8.2) and update thefastcgi_passline accordingly. - Tweak CI's
config.php:- Set
$config['base_url'] = 'http://173.249.40.xxx'; - Set
$config['index_page'] = '';(this removesindex.phpfrom your URLs)
- Set
- Fix File Permissions: Make sure the Nginx user (usually
www-data) has write access to CI's cache and logs directories:sudo chown -R www-data:www-data /usr/share/nginx/html/ci/application/{cache,logs} sudo chmod -R 755 /usr/share/nginx/html/ci/application/{cache,logs} - Restart Services: Apply changes by restarting Nginx and php-fpm:
sudo systemctl restart nginx sudo systemctl restart php7.4-fpm # Adjust version as needed
If It Still Doesn't Work
Check Nginx's error log for specific clues about what's failing:
sudo tail -f /var/log/nginx/error.log
This will show you exact issues like permission errors, missing files, or php-fpm connection problems.
内容的提问来源于stack exchange,提问作者NomanJaved

