1. 在 Apache 的写法
RewriteCond %{HTTP_HOST} nginx.org RewriteRule (.*) http://www.nginx.org$1
在 Nginx 可以对应写成:
server { listen 80; server_name www.nginx.org nginx.org; if ($http_host = nginx.org) { rewrite (.*) http://www.nginx.org$1; } ... }
但 Nginx 作者更建议的方法是:
server { listen 80; server_name nginx.org; rewrite ^ http://www.nginx.org$request_uri?; } server { listen 80; server_name www.nginx.org; ... }
2. 在 Mongrel的写法:
DocumentRoot /var/www/myapp.com/current/public RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_FILENAME}/index.html -f RewriteRule ^(.*)$ $1/index.html [QSA,L] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ $1/index.html [QSA,L] RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
在 Nginx可对应写成
location / { root /var/www/myapp.com/current/public; try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
这个就比Apache 的简化多了。
---
转载请注明本文标题和链接:《Nginx 一些常用的 URL 重写方法》
发表评论