这个转换的Apache重写规则的Nginx [英] Convert this Apache rewrite to Nginx rules

查看:208
本文介绍了这个转换的Apache重写规则的Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换这个的.htaccess规则nginx的规则:

I need to convert this .htaccess rules to nginx rules:

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ navigation.php
RewriteRule .* - [E=HTTP_X_REQUESTED_WITH:%{HTTP:X-Requested-With}]

我用的在线转换工具,这给:

I used this online conversion tool, which gives:

if (!-f $request_filename){
    set $rule_0 1$rule_0;
}
if (!-d $request_filename){
    set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
    rewrite ^/(.*)$ /navigation.php;
}
     HTTP_X_REQUESTED_WITH:$http_x_requested_with;
#ignored: "-" thing used or unknown variable in regex/rew 

但结果不是nginx的0.76可用的,并且我得到了一个错误说

However the result is not usable in nginx 0.76, and I got an error saying

unknown directive "setenv" 

当我消除我的nginx的配置中的最后一行,该脚本无法显示某些页面。

And when I eliminate the last line from my nginx config, the script fails to show some pages.

所以,我真的AP preciate如果你可以做转换。

So I really appreciate if you could do the conversion.

推荐答案

通过Nginx的你不需要重新映射完全相同的方式,你这样做是在Apache中。

With Nginx you do not need to remap exactly the way you did it in Apache.

你有没有阅读 IfIsEvil nginx的页面?

Did you ever read the IfIsEvil nginx page?

要测试一个文件不是nginx的应用规则的真正简单的方法之前的静态文件或目录并不多,如果像你这样但try_file指令(的检查文件中的陷阱页面存在)。

To test a file is not a static file or directory before applying a rule in nginx the real simple way is not a multi-if like you did but a try_file directive (check if file exists in pitfall page).

所以,你应该与该规定启动:

So you should start with that "rule":

server {
  root /var/www/domain.com;
  location / {
    HTTP_X_REQUESTED_WITH:$http_x_requested_with;
    try_files $uri $uri/ /navigation.php;
  }
}

现在你有可能其他一些地方通过FastCGI的处理PHP文件,这也许是你需要的HTTP_X_REQUESTED_WITH转移为 fastcgi_param 实处。

Now you have maybe some other locations to handle PHP files via fastcgi, this is maybe the real place where you need to transfer the HTTP_X_REQUESTED_WITH as a fastcgi_param.

这篇关于这个转换的Apache重写规则的Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆