Apache的mod_rewrite到Nginx的重写规则 [英] Apache mod_rewrite to Nginx rewrite rules

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

问题描述

我的网站上运行Nginx的,我想在使用Apache的mod_rewrite规则网站的子目录中添加软件。例如。 www.mydomain.com/mySubfolder

下面是Apache的.htaccess

  #Options -Indexes
< ifModule mod_rewrite.c>
    RewriteEngine叙述在
    的RewriteCond%{REQUEST_URI} ^ /系统*。
    重写规则^(。*)$的index.php?/ $ 1 [L]
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    重写规则^(。+)$的index.php?/ $ 1 [L]
< / ifModule>

到目前为止,我设法让主网页的工作,但请求登录页面时,它是造成URL重定向循环。例如。 www.myDomain.com/login
有了这个:

 位置/ mySubfolder {
如果(!$ -e REQUEST_FILENAME){
改写^ $ /mySubfolder/index.php?q=$1最后的(*);
打破;
}
}

我一直在读书,并努力学习如何阿帕奇转换为Nginx的,甚至使用Nginx的转换器,我发现在的的.htaccess http://winginx.com/htaccess 但该工具似乎并没有认识到%{REQUEST_URI} ^ /系统。*的一部分。在我的研究和学习,我想出了:

 位置/ mySubfolder {
如果($ REQUEST_URI〜^ /(系统。*)$){
重写^ / $的index.php / $ 1最后(*)?;
}
如果(!$ -e REQUEST_FILENAME){
改写^ $ /mySubfolder/index.php?q=$1最后的(+);
打破;
}
}

我在这一个完整的小白,如果我甚至接近完成这种转换工作有人甚至怀疑。请帮助。

感谢您。


解决方案

  

有人甚至想,如果我甚至接近完成这个转换
  工作


您已经基本上采取了错误的做法,Nginx的使用。

虽然它是一种天然的假设,在Apache的重写规则将被映射到Nginx的重写,他们不是。相反,他们大多是被映射到的位置的规则。

您的 mySubfolder 块应该是这样的:

 位置^ / mySubfolder {
    try_files /mySubfolder/index.php?$args = 404;
}

您实际上并没有任何改写 - 你只是告诉认为,随着/ MySubfolder启动任何请求应通过 try_files 中列出的文件送达nginx的。顺便说一句,你要告诉Nginx的传递查询字符串,通过这是什么 ARGS 正在做什么。

您可以附加原始URL(我认为),虽然它可能更容易只使用 $ _ SERVER ['REQUEST_URI'] 你的脚本中。

我相信你有相同的重写规则开始URI是造成/ mySubFolder以保持匹配。

在Nginx的重写,当你想使外部URL路径由不同的内部URL提供服务,通常重写规则不是一个位置块中仅使用。

例如我有一个文件服务器,它提供了图像和其他文件。我有这些重写规则在服务器块:

 改写^ /图像/(\\ d +)/(\\ w +)/(+)\\。([^ \\。] *)$ /proxy/proxyImage.php?typeID=$1&mode=$2&imagePath=$3.$4&resizeAllowed=TRUE&type=image持续;
    重写^ /图像/(\\ d +)/ \\([^ \\。] *)$ /proxy/proxyImage.php?typeID=$1&imagePath=$2.$3&resizeAllowed=TRUE最后一个(+)。
    改写^ /文件/(\\ d +)/ \\([^ \\。] *)$ /proxy/proxyFile.php?typeID=$1&imagePath=$2.$3&resizeAllowed=FALSE最后一个(+)。

使外部URL看起来不错。但后来他们都被挡位置服务:

 位置〜* ^ /代理{
        try_files $ URI = 404;
        fastcgi_pass UNIX:/opt/local/var/run/php54/php-fpm-images.sock;
        包括/documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;
    }

位置块并不需要了解的URL重写,因为遇到所在地块之前,他们就完成了。

My site is running on Nginx and I am trying to add a software in the sub-directory of the site that uses Apache's mod_rewrite rules. E.g. www.mydomain.com/mySubfolder

Here is the Apache .htaccess

#Options -Indexes
<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ index.php?/$1 [L]
</ifModule>

So far I managed to get the main page to work but when requesting the login page, it is causing a URL redirect loop. E.g. www.myDomain.com/login With this:

location /mySubfolder {
if (!-e $request_filename) {
rewrite ^(.*)$ /mySubfolder/index.php?q=$1 last;
break;
}
}

I have been reading and trying to learn how to convert Apache to Nginx and even used the .htaccess to Nginx converter I found at http://winginx.com/htaccess but the tool doesn't seem to recognize the %{REQUEST_URI} ^/system.* part. Upon my research and study, I came up with:

location /mySubfolder {
if ($request_uri ~ "^/(system.*)$") {
rewrite ^/(.*)$ index.php?/$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /mySubfolder/index.php?q=$1 last;
break;
}
}

I am a complete noob at this and was even wondering if I am even close to accomplish this conversion to work. Please help.

Thank you.

解决方案

was even wondering if I am even close to accomplish this conversion to work

You've basically taken the wrong approach to use in Nginx.

Although it is kind of natural to assume that the rewrite rules in Apache would be mapped to the rewrite in Nginx, they aren't. Instead they are mostly mapped to the location rules.

Your block for mySubfolder should look like:

location ^/mySubfolder {
    try_files /mySubfolder/index.php?$args =404;
}

You aren't actually rewriting anything - you are just telling nginx that any requests that start with /MySubfolder should be served by the files listed in try_files. btw you have to tell Nginx to pass the query string through which is what the args is doing.

You can append the original URL (i think) though it may be easier just to use $_SERVER['REQUEST_URI'] inside your script.

I believe the rewrite rule you have to the same start URI is causing the /mySubFolder to keep matching.

In Nginx rewriting is only used when you want to make external URL paths be served by different internal urls, and normally the rewrite rule is not inside a location block.

For example I have a file server, that serves up images and other files. I have these rewrite rules in the server block:

rewrite  ^/image/(\d+)/(\w+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&mode=$2&imagePath=$3.$4&resizeAllowed=TRUE&type=image last;
    rewrite  ^/image/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&imagePath=$2.$3&resizeAllowed=TRUE  last;
    rewrite  ^/file/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyFile.php?typeID=$1&imagePath=$2.$3&resizeAllowed=FALSE last;

to make the external URLs look nice. But then they are all served by the location block:

location ~* ^/proxy  {
        try_files $uri =404;
        fastcgi_pass   unix:/opt/local/var/run/php54/php-fpm-images.sock;
        include       /documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;
    }

The location block doesn't need to know about the URL rewrites because they are done before the location block is encountered.

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

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