You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx部署CodeIgniter仅首页可访问,其他控制器报404求助

问题:CodeIgniter 3 仅首页可访问,其他控制器返回404错误

环境信息

  • Ubuntu 22.04
  • PHP-FPM 8.1
  • Nginx 1.18
  • CodeIgniter 3.1.13
  • 应用目录:/var/www/html/example

当前配置

Nginx 站点配置(sites-available/example.com)

server {
    #listen 80;
    listen [::]:80;

    root /var/www/html/example.com;

    autoindex on;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;
        
        #try_files $uri $uri/ /index.php$is_args$args;
        #try_files $uri $uri/ /index.php;
        #try_files $uri/ $uri /index.php?$query_string;
    location / {
             try_files $uri $uri/ @rewrites;
         }    

         location @rewrites {
             if (!-e $request_filename)
            {
                rewrite ^/(.*)$ /application/index.php/$1 last;
                break;
            }
         }
    location ~* ^/(assets|files|robots\.txt) { }


    location = /favicon.ico {
        return 204;
        access_log     off;
        log_not_found  off;
    }

    location = /.env {
        return 204;
        access_log     off;
        log_not_found  off;
    }
        # set expiration of assets to MAX for caching
        location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                expires max;
                log_not_found off;
        }
  error_page 404 /application/index.php;
  fastcgi_intercept_errors on; 
    location ~ \.php$ {          
                
      fastcgi_param  PATH_INFO            $fastcgi_path_info;
          fastcgi_param  PATH_TRANSLATED      $document_root$fastcgi_path_info;

          fastcgi_param  QUERY_STRING         $query_string;
          fastcgi_param  REQUEST_METHOD       $request_method;
          fastcgi_param  CONTENT_TYPE         $content_type;
          fastcgi_param  CONTENT_LENGTH       $content_length;

          fastcgi_param  SCRIPT_NAME          $fastcgi_script_name;
          fastcgi_param  SCRIPT_FILENAME      $request_filename;
          fastcgi_param  REQUEST_URI          $request_uri;
          fastcgi_param  DOCUMENT_URI         $document_uri;
          fastcgi_param  DOCUMENT_ROOT        $document_root;
          fastcgi_param  SERVER_PROTOCOL      $server_protocol;

          fastcgi_param  SERVER_SOFTWARE      nginx;

          fastcgi_param  REMOTE_ADDR          $remote_addr;
          fastcgi_param  REMOTE_PORT          $remote_port;
          fastcgi_param  SERVER_ADDR          $server_addr;
          fastcgi_param  SERVER_PORT          $server_port;
          fastcgi_param  SERVER_NAME          $server_name;

          fastcgi_index  index.php;
          #fastcgi_pass    unix:/var/run/php/php8.1-fpm.sock;
      fastcgi_pass 127.0.0.1:9000;
      try_files $uri $uri/ /application/index.php;
    }

    location ~ /\.ht {
        deny all;
    }
}

CodeIgniter config.php 配置

$config['base_url'] = 'https://example.com/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

问题修复方案

1. 修正Nginx根目录路径

当前Nginx配置的root为/var/www/html/example.com,但实际应用目录是/var/www/html/example,这会导致Nginx找不到正确的入口文件,需要修改:

root /var/www/html/example;

2. 修复重写规则

CodeIgniter的入口文件是根目录下的index.php,而非application/index.php,当前重写规则指向错误,需要修改@rewrites块:

location @rewrites {
    rewrite ^/(.*)$ /index.php/$1 last;
}

if (!-e $request_filename)可直接移除,因为try_files已经完成了文件存在性检查。

3. 修正PHP location配置

  • 移除try_files $uri $uri/ /application/index.php;,替换为try_files $uri =404;避免错误跳转
  • 增加fastcgi_split_path_info确保PATH_INFO正确解析(CI 3依赖该参数获取路由)
  • 使用include fastcgi_params;简化配置,避免重复定义默认参数

修正后的PHP location块:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

4. 修正错误页面配置

当前error_page 404 /application/index.php;指向错误,改为:

error_page 404 /index.php;

5. 确认CI配置

你的CI配置已符合要求:

  • index_page设为空,适配URL重写需求
  • uri_protocol设为REQUEST_URI,兼容Nginx环境

最终完整Nginx配置示例

server {
    listen 80;
    listen [::]:80;

    root /var/www/html/example;
    index index.php index.html index.htm;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ @rewrites;
    }

    location @rewrites {
        rewrite ^/(.*)$ /index.php/$1 last;
    }

    location ~* ^/(assets|files|robots\.txt) {
        access_log off;
    }

    location = /favicon.ico {
        return 204;
        access_log off;
        log_not_found off;
    }

    location = /.env {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
        access_log off;
    }

    error_page 404 /index.php;
    fastcgi_intercept_errors on;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

验证步骤

  1. 保存修正后的Nginx配置
  2. 测试配置语法:sudo nginx -t
  3. 重启Nginx:sudo systemctl restart nginx
  4. 访问其他控制器路径,确认是否正常加载

内容的提问来源于stack exchange,提问作者AGNY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:20:36