为codeIgniter Nginx的重写规则 [英] Nginx rewrite rule for CodeIgniter
问题描述
下面是英文的规则:
的任何HTTP请求比对的index.php,资产文件夹,文件夹和robots.txt的将被视为您的index.php文件的请求等。
我有一个的.htaccess
文件的Apache服务器上正常工作:
的RewriteCond $ l ^(指数\ .PHP |资产|文件|机器人\ .TXT)!
重写规则^(。*)$的index.php / $ 1 [L]
一些正确的结果这条规则:
example.com
= example.com/index.php
example.com/index.php/welcome
= example.com/welcome
example.com/assets/css/main.css
!= example.com/index.php/资产/ CSS /的main.css
我尝试了一些工具来htaccess的规则转换为nginx的规则,但所有是不正确的。
通过 http://winginx.com/htaccess (缺少资产文件夹中的例外规则...)
的位置/ {改写^ $ /index.php/$1突破(*); }
通过 http://www.anilcetin.com/convert-apache- htaccess的到的nginx / ($ 1值误差):
如果($为1〜^(index.php文件|资产|文件| robots.txt的)!){
设置$ rule_0 1 $ rule_0;
}
如果($ rule_0 =1){
改写^ /(。*)$ /index.php/$1最后;
}
我该如何解决这个问题?这真的很难调试规则。
下面是我的nginx的配置至今:
服务器{
听80;
服务器名www.example.com example.com;
位置 / {
try_files $ URI $ URI / /index.php?/$request_uri;
}
位置〜\的.php $ {
HTML根;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php文件;
fastcgi_param SCRIPT_FILENAME的/ var / www / html等$ fastcgi_script_name;
包括fastcgi_params;
}
位置〜/\.ht {
拒绝所有;
}
}
您可以添加到您的配置:
位置〜* ^ /(资产|文件|机器人\ .TXT){}
这将正常工作与你的位置/
规则。
您的配置还需要添加根文档和默认的索引文件。
...
根/ FTP /衣柜;
指数的index.php index.html的index.htm的;
位置 / {
try_files $ URI $ URI / /index.php?/$request_uri;
}
位置〜* ^ /(资产|文件|机器人\ .TXT){}
...
Here is the rule in English:
Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.
I have an .htaccess
file that works correctly on Apache server:
RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Some correct results for this rule:
example.com
= example.com/index.php
example.com/index.php/welcome
= example.com/welcome
example.com/assets/css/main.css
!= example.com/index.php/assets/css/main.css
I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.
Via http://winginx.com/htaccess (missing the exception rules for assets folder...):
location / { rewrite ^(.*)$ /index.php/$1 break; }
Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in $1 value):
if ($1 !~ "^(index.php|assets|files|robots.txt)"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ /index.php/$1 last;
}
How can I fix this? It's really hard to debug the rule.
Here is my nginx config so far:
server {
listen 80;
server_name www.example.com example.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You can add this to your config:
location ~* ^/(assets|files|robots\.txt) { }
This will work correctly with your location /
rule.
Your config also need to add root document and default index file.
...
root /ftp/wardrobe;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
...
这篇关于为codeIgniter Nginx的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!