Mercurial > hg > nginx-site
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61e04fc01027 |
---|---|
1 <!DOCTYPE digest SYSTEM "../../../../dtd/article.dtd"> | |
2 | |
3 <article title="Converting rewrite rules" | |
4 link="/en/docs/http/converting_rewrite_rules.html" | |
5 lang="en"> | |
6 | |
7 | |
8 <section title="A redirect to a main site"> | |
9 | |
10 <para> | |
11 People who during their shared hosting life used to configure | |
12 <i>everything</i> using <i>only</i> Apache’s .htaccess files, | |
13 translate usually the following rules: | |
14 | |
15 <programlisting> | |
16 RewriteCond %{HTTP_HOST} nginx.org | |
17 RewriteRule (.*) http://www.nginx.org$1 | |
18 </programlisting> | |
19 | |
20 in something like this: | |
21 | |
22 <programlisting> | |
23 server { | |
24 listen 80; | |
25 server_name www.nginx.org nginx.org; | |
26 if ($http_host = nginx.org) { | |
27 rewrite (.*) http://www.nginx.org$1; | |
28 } | |
29 ... | |
30 } | |
31 </programlisting> | |
32 </para> | |
33 | |
34 <para> | |
35 This is a wrong, cumbersome, and ineffective way. | |
36 The right way is to define a separate server for <url>nginx.org</url>: | |
37 | |
38 <programlisting> | |
39 server { | |
40 listen 80; | |
41 server_name nginx.org; | |
42 rewrite ^ http://www.nginx.org$request_uri?; | |
43 } | |
44 | |
45 server { | |
46 listen 80; | |
47 server_name www.nginx.org; | |
48 ... | |
49 } | |
50 </programlisting> | |
51 </para> | |
52 | |
53 </section> | |
54 | |
55 | |
56 <section> | |
57 | |
58 <para> | |
59 Another example, instead of backward logic: all that is not | |
60 <url>nginx.com</url> and is not <url>www.nginx.com</url>: | |
61 | |
62 <programlisting> | |
63 RewriteCond %{HTTP_HOST} !nginx.com | |
64 RewriteCond %{HTTP_HOST} !www.nginx.com | |
65 RewriteRule (.*) http://www.nginx.com$1 | |
66 </programlisting> | |
67 | |
68 you should define just <url>nginx.com</url>, <url>www.nginx.com</url>, | |
69 and anything else: | |
70 | |
71 <programlisting> | |
72 server { | |
73 listen 80; | |
74 server_name nginx.com www.nginx.com; | |
75 ... | |
76 } | |
77 | |
78 server { | |
79 listen 80 default_server; | |
80 server_name _; | |
81 rewrite ^ http://nginx.com$request_uri?; | |
82 } | |
83 </programlisting> | |
84 </para> | |
85 | |
86 </section> | |
87 | |
88 | |
89 <section name="converting_mongrel_rules" | |
90 title="Converting Mongrel rules"> | |
91 | |
92 <para> | |
93 Typical Mongrel rules: | |
94 | |
95 <programlisting> | |
96 DocumentRoot /var/www/myapp.com/current/public | |
97 | |
98 RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f | |
99 RewriteCond %{SCRIPT_FILENAME} !maintenance.html | |
100 RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L] | |
101 | |
102 RewriteCond %{REQUEST_FILENAME} -f | |
103 RewriteRule ^(.*)$ $1 [QSA,L] | |
104 | |
105 RewriteCond %{REQUEST_FILENAME}/index.html -f | |
106 RewriteRule ^(.*)$ $1/index.html [QSA,L] | |
107 | |
108 RewriteCond %{REQUEST_FILENAME}.html -f | |
109 RewriteRule ^(.*)$ $1/index.html [QSA,L] | |
110 | |
111 RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L] | |
112 </programlisting> | |
113 | |
114 should be converted to | |
115 | |
116 <programlisting> | |
117 location / { | |
118 root /var/www/myapp.com/current/public; | |
119 | |
120 try_files /system/maintenance.html | |
121 $uri $uri/index.html $uri.html | |
122 @mongrel; | |
123 } | |
124 | |
125 location @mongrel { | |
126 proxy_pass http://mongrel; | |
127 } | |
128 </programlisting> | |
129 </para> | |
130 | |
131 </section> | |
132 | |
133 </article> |