我正在尝试将CodeIgniter下的一个站点从Apache迁移到Nginx。我的第一次测试显示出非常好的表现,所以这是最糟糕的尝试。
但我找不到正确的Nginx配置来取代那些RewriteRules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
我在Nginx网站上找到了这个食谱https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
location / {
try_files $uri $uri/ /index.php;
}
它在大多数情况下都有效,除非我有一个尾随斜杠。意思http://example.com/controller/param/
将呼叫http://example.com/controller/function/index.php(并返回一个404(
而不是http://example.com/index.php/controller/function/。。。就像通常使用Apache重写一样。
我试图添加一个重写:
rewrite ^/(.*)/$ /$1 permanent;
但它将POST重定向到GET,所以我丢失了所有的帖子数据。。。
有线索吗?
感谢
检查这是否有帮助。替换两次出现的<source_directory>
;使用正确的值:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
if ($host ~* ^www.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# use fastcgi for all php files
location ~ .php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}