Nginx 404 子目录中的 Codeigniter

Nginx 404 子目录中的 Codeigniter

我正在尝试在我的域的子目录中设置 Codeigniter。目前,我在索引页上仅收到来自 Codeigniter 的 404。当我想要访问时,example.com/api/welcome/indexnginx 会立即返回 404。

我的nginx.conf

location /api/ {
    alias  /var/www/api/;
    try_files $uri $uri/ /api/index.php =404;
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index   index.php;
        fastcgi_pass    backend;
        include fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Codeigniter.conf

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

答案1

成功了,解决方案是使用 $request_filename; 而不是 $document_root$fastcgi_script_name;

location /api/ {
    alias  /var/www/api/;
    try_files $uri $uri/ /api/index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index   index.php;
        fastcgi_pass    backend;
        include /etc/nginx/fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $request_filename;
    }
}

答案2

这对我有用(nginx 1.18)

location ^~ /api {
    root /var/www/api;
    index index.php;
    try_files $uri $uri/ /api/index.php;

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

}

相关内容