Mercurial > hg > nginx-site
diff xml/en/docs/http/converting_rewrite_rules.xml @ 0:61e04fc01027
Initial import of the nginx.org website.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Thu, 11 Aug 2011 12:19:13 +0000 |
parents | |
children | 9d544687d02c |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/xml/en/docs/http/converting_rewrite_rules.xml @@ -0,0 +1,133 @@ +<!DOCTYPE digest SYSTEM "../../../../dtd/article.dtd"> + +<article title="Converting rewrite rules" + link="/en/docs/http/converting_rewrite_rules.html" + lang="en"> + + +<section title="A redirect to a main site"> + +<para> +People who during their shared hosting life used to configure +<i>everything</i> using <i>only</i> Apache’s .htaccess files, +translate usually the following rules: + +<programlisting> +RewriteCond %{HTTP_HOST} nginx.org +RewriteRule (.*) http://www.nginx.org$1 +</programlisting> + +in something like this: + +<programlisting> +server { + listen 80; + server_name www.nginx.org nginx.org; + if ($http_host = nginx.org) { + rewrite (.*) http://www.nginx.org$1; + } + ... +} +</programlisting> +</para> + +<para> +This is a wrong, cumbersome, and ineffective way. +The right way is to define a separate server for <url>nginx.org</url>: + +<programlisting> +server { + listen 80; + server_name nginx.org; + rewrite ^ http://www.nginx.org$request_uri?; +} + +server { + listen 80; + server_name www.nginx.org; + ... +} +</programlisting> +</para> + +</section> + + +<section> + +<para> +Another example, instead of backward logic: all that is not +<url>nginx.com</url> and is not <url>www.nginx.com</url>: + +<programlisting> +RewriteCond %{HTTP_HOST} !nginx.com +RewriteCond %{HTTP_HOST} !www.nginx.com +RewriteRule (.*) http://www.nginx.com$1 +</programlisting> + +you should define just <url>nginx.com</url>, <url>www.nginx.com</url>, +and anything else: + +<programlisting> +server { + listen 80; + server_name nginx.com www.nginx.com; + ... +} + +server { + listen 80 default_server; + server_name _; + rewrite ^ http://nginx.com$request_uri?; +} +</programlisting> +</para> + +</section> + + +<section name="converting_mongrel_rules" + title="Converting Mongrel rules"> + +<para> +Typical Mongrel rules: + +<programlisting> +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] +</programlisting> + +should be converted to + +<programlisting> +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; +} +</programlisting> +</para> + +</section> + +</article>