view xml/en/docs/http/converting_rewrite_rules.xml @ 1878:127ae107e5a9

Removed clause about shared memory and Windows versions with ASLR. Starting with nginx 1.9.0 shared memory can be used on Windows versions with address space layout randomization.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 26 Dec 2016 19:38:06 +0300
parents cb7fbecee8e6
children
line wrap: on
line source

<!--
  Copyright (C) Igor Sysoev
  Copyright (C) Nginx, Inc.
  -->

<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">

<article name="Converting rewrite rules"
         link="/en/docs/http/converting_rewrite_rules.html"
         lang="en"
         rev="2">


<section name="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&rsquo;s .htaccess files,
usually translate the following rules:

<programlisting>
RewriteCond  %{HTTP_HOST}  example.org
RewriteRule  (.*)          http://www.example.org$1
</programlisting>

to something like this:

<programlisting>
server {
    listen       80;
    server_name  www.example.org  example.org;
    if ($http_host = example.org) {
        rewrite  (.*)  http://www.example.org$1;
    }
    ...
}
</programlisting>
</para>

<para>
This is a wrong, cumbersome, and ineffective way.
The right way is to define a separate server for <literal>example.org</literal>:

<programlisting>
server {
    listen       80;
    server_name  example.org;
    return       301 http://www.example.org$request_uri;
}

server {
    listen       80;
    server_name  www.example.org;
    ...
}
</programlisting>

<note>
On versions prior to 0.9.1, redirects can be made with:
<programlisting>
    rewrite      ^ http://www.example.org$request_uri?;
</programlisting>
</note>

</para>

</section>


<section>

<para>
Another example.
Instead of the &ldquo;upside-down&rdquo; logic &ldquo;all that is not
<literal>example.com</literal> and is not <literal>www.example.com</literal>&rdquo;:

<programlisting>
RewriteCond  %{HTTP_HOST}  !example.com
RewriteCond  %{HTTP_HOST}  !www.example.com
RewriteRule  (.*)          http://www.example.com$1
</programlisting>

one should simply define <literal>example.com</literal>, <literal>www.example.com</literal>,
and &ldquo;everything else&rdquo;:

<programlisting>
server {
    listen       80;
    server_name  example.com www.example.com;
    ...
}

server {
    listen       80 default_server;
    server_name  _;
    return       301 http://example.com$request_uri;
}
</programlisting>

<note>
On versions prior to 0.9.1, redirects can be made with:
<programlisting>
    rewrite      ^ http://example.com$request_uri?;
</programlisting>
</note>

</para>

</section>


<section id="converting_mongrel_rules"
        name="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.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>