![Nginx 404 子目录中的 Codeigniter](https://linux22.com/image/659326/Nginx%20404%20%E5%AD%90%E7%9B%AE%E5%BD%95%E4%B8%AD%E7%9A%84%20Codeigniter.png)
我正在尝试在我的域的子目录中设置 Codeigniter。目前,我在索引页上仅收到来自 Codeigniter 的 404。当我想要访问时,example.com/api/welcome/index
nginx 会立即返回 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;
}
}