将HTACCESS mod_rewrite指令转换为nginx格式?

我对nginx是全新的,我正试图转换我从Apache写的应用程序,因为我需要一次性为大量客户端提供服务,而无需太多开销! 我得到了设置nginx和FastCGI PHP的窍门,但是我现在还不能把头围绕nginx的rewrite格式。 我知道你必须编写一些简单的脚本放在nginxconfiguration中的服务器块中,但我还不熟悉这个语法。 任何有Apache和Nginx的经验的人都可以帮我把它转换成nginx格式吗? 谢谢!

# ------------------------------------------------------ # # Rewrite from canonical domain (remove www.) # # ------------------------------------------------------ # RewriteCond %{HTTP_HOST} ^www.domain.com RewriteRule (.*) http://domain.com/$1 [R=301,L] # ------------------------------------------------------ # # This redirects index.php to / # # ------------------------------------------------------ # RewriteCond %{THE_REQUEST} ^[AZ]+\ /(index|index\.php)\ HTTP/ RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] # ------------------------------------------------------ # # This rewrites 'directories' to their PHP files, # # fixes trailing-slash issues, and redirects .php # # to 'directory' to avoid duplicate content. # # ------------------------------------------------------ # RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)$ $1.php [L] RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[AZ]+\ /[^.]+\.php\ HTTP/ RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L] # ------------------------------------------------------ # # If it wasn't redirected previously and is not # # a file on the server, rewrite to image generation # # ------------------------------------------------------ # RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=${escapemap:$1}&template=${escapemap:$2} [NC,L] 

我逐个部分地转换以更容易地跟随。

阿帕奇:

 RewriteCond %{HTTP_HOST} ^www.domain.com RewriteRule (.*) http://domain.com/$1 [R=301,L] 

Nginx的:

 server { listen 80; server_name www.domain.com; root /var/www/localhost/htdocs; rewrite ^ http://domain.com$request_uri permanent; } 

阿帕奇:

 RewriteCond %{THE_REQUEST} ^[AZ]+\ /(index|index\.php)\ HTTP/ RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] 

Nginx的:

 location = /index { try_files $uri @homepage; } location @homepage { rewrite ^ http://domain.com break; } location ~ \.php$ { if ($request_uri ~* /index\.php$) { rewrite ^ http://domain.com break; } fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_intercept_errors on; error_page 404 /error/404.php; } 

阿帕奇:

 RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)$ $1.php [L] RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[AZ]+\ /[^.]+\.php\ HTTP/ RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L] 

Nginx的:

 rewrite ^/(.*)/$ /$1 permanent; if (-f $document_root/$request_uri.php) { rewrite ^(.*)$ $1.php last; } location ~ \.php$ { if (-f $document_root/$request_uri) { rewrite ^([^.]+)\.php$ http://domain.com$1 permanent; } ... }