summaryrefslogtreecommitdiff
path: root/pod/nginx/converting_rewrite_rules.pod
blob: 18440dcd019dcea4cbf5bf2d8d7c580c958d4bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
=encoding utf-8


=head1 Name


converting_rewrite_rules - Converting rewrite rules


=head1 A redirect to a main site



People who during their shared hosting life used to configure
I<everything> using I<only> ApacheE<rsquo>s .htaccess files,
usually translate the following rules:


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



to something like this:


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







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


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




B<NOTE>

On versions prior to 0.9.1, redirects can be made with:

    
        rewrite      ^ http://www.example.org$request_uri?;








=head1



Another example.
Instead of the E<ldquo>upside-downE<rdquo> logic E<ldquo>all that is not
C<example.com> and is not C<www.example.com>E<rdquo>:


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



one should simply define C<example.com>, C<www.example.com>,
and E<ldquo>everything elseE<rdquo>:


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




B<NOTE>

On versions prior to 0.9.1, redirects can be made with:

    
        rewrite      ^ http://example.com$request_uri?;








=head1 Converting Mongrel rules



Typical Mongrel rules:


    
    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]



should be converted to


    
    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;
    }