简体   繁体   English

将此Apache重写转换为Nginx规则

[英]Convert this Apache rewrite to Nginx rules

I need to convert this .htaccess rules to nginx rules: 我需要将此.htaccess规则转换为nginx规则:

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 

However the result is not usable in nginx 0.76, and I got an error saying 但是结果在nginx 0.76中不可用,并且出现错误提示

unknown directive "setenv" 

And when I eliminate the last line from my nginx config, the script fails to show some pages. 当我从nginx配置中删除最后一行时,脚本无法显示某些页面。

So I really appreciate if you could do the conversion. 因此,如果您可以进行转换,我非常感谢。

With Nginx you do not need to remap exactly the way you did it in Apache. 使用Nginx,您不需要完全按照在Apache中所做的方式重新映射。

Did you ever read the IfIsEvil nginx page? 您是否曾经阅读过IfIsEvil nginx页面?

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 ). 在nginx中应用规则之前,要测试文件不是静态文件还是目录,真正的简单方法不是像您那样的多重选择,而是try_file指令( 检查pitfall页面中是否存在文件 )。

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;
  }
}

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 . 现在您可能还有其他位置可以通过fastcgi处理PHP文件,这也许是您真正需要将HTTP_X_REQUESTED_WITH转换为fastcgi_param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM