我正在尝试基于 codeigniter 在 nginx 上运行一个网站。有些部分可以运行,但有些部分会失败。我在访问日志中注意到 index.php 末尾有时有两个斜杠,而不是一个,我也无法正确获取我的帖子(返回 404)。
我的默认站点的 nginx 配置如下(它是服务器上唯一的站点)
server {
server_name xxx.com;
return 301 $scheme://www.xxx.com$request_uri;
}
server {
root /srv/www/xxx;
index index.php index.html index.htm;
server_name www.xxx.com;
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
if (!-e $request_filename)
{
rewrite ^(.*)$ /index.php/$1 last;
break;
}
location \ {
try_files $uri $uri/ @no_php_ext;
}
# catch all
error_page 404 /index.php;
location ~ \.(php)$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/xxx/$fastcgi_script_name;
include fastcgi_params;
#fastcgi_read_timeout 900;
}
location @no-php-ext {
rewrite ^(.*)$ index.php/$1 last;
}
}
我尝试过多次重写,但都没有成功。非常感谢任何帮助。
答案1
这个对我有用。现在所有页面都返回了 200。我仍然对 cron 作业存在问题,但这个问题应该在另一个问题中提出。
server {
root /srv/www/xxx;
index index.php index.html index.htm;
server_name www.xxx.com;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log;
location = / {
try_files $uri $uri/ /index.php;
rewrite ^/(.*)$ /index.php;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}