comparison xml/cn/docs/http/converting_rewrite_rules.xml @ 558:149f54c158f0

Added initial translation in simplified Chinese submitted by the Server Platforms Team at Taobao.com.
author Ruslan Ermilov <ru@nginx.com>
date Thu, 28 Jun 2012 10:27:07 +0000
parents
children 130fad6dc1b4
comparison
equal deleted inserted replaced
557:654096219aba 558:149f54c158f0
1 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
2
3 <article name="转换rewrite规则"
4 link="/cn/docs/http/converting_rewrite_rules.html"
5 lang="cn">
6
7
8 <section name="重定向到主站">
9
10 <para>
11 共享站点的管理员,习惯于<i>只</i>在Apache下使用.htaccess文件配置<i>所有</i>信息,通常会将下面规则
12
13 <programlisting>
14 RewriteCond %{HTTP_HOST} nginx.org
15 RewriteRule (.*) http://www.nginx.org$1
16 </programlisting>
17
18 翻译成这样:
19
20 <programlisting>
21 server {
22 listen 80;
23 server_name www.nginx.org nginx.org;
24 if ($http_host = nginx.org) {
25 rewrite (.*) http://www.nginx.org$1;
26 }
27 ...
28 }
29 </programlisting>
30 </para>
31
32 <para>
33 这种做法是错的,复杂而且低效。正确的方式是为<url>nginx.org</url>定义一个单独的服务器:
34
35 <programlisting>
36 server {
37 listen 80;
38 server_name nginx.org;
39 return 301 http://www.nginx.org$request_uri;
40 }
41
42 server {
43 listen 80;
44 server_name www.nginx.org;
45 ...
46 }
47 </programlisting>
48
49 <note>
50 在0.9.1版本(含)以前,可以这样实现重定向:
51 <programlisting>
52 rewrite ^ http://www.nginx.org$request_uri?;
53 </programlisting>
54 </note>
55
56 </para>
57
58 </section>
59
60
61 <section>
62
63 <para>
64 再举一个例子,处理一个和刚才相反的逻辑:既不是来自<url>nginx.com</url>,又不是来自<url>www.nginx.com</url>:
65
66 <programlisting>
67 RewriteCond %{HTTP_HOST} !nginx.com
68 RewriteCond %{HTTP_HOST} !www.nginx.com
69 RewriteRule (.*) http://www.nginx.com$1
70 </programlisting>
71
72 应该按下面这样分开定义<url>nginx.com</url>、<url>www.nginx.com</url>和其他站点:
73
74 <programlisting>
75 server {
76 listen 80;
77 server_name nginx.com www.nginx.com;
78 ...
79 }
80
81 server {
82 listen 80 default_server;
83 server_name _;
84 return 301 http://nginx.com$request_uri;
85 }
86 </programlisting>
87
88 <note>
89 在0.9.1版本(含)以前,可以这样实现重定向:
90 <programlisting>
91 rewrite ^ http://nginx.com$request_uri?;
92 </programlisting>
93 </note>
94
95 </para>
96
97 </section>
98
99
100 <section id="converting_mongrel_rules"
101 name="转化混合规则">
102
103 <para>
104 典型的混合规则如下:
105
106 <programlisting>
107 DocumentRoot /var/www/myapp.com/current/public
108
109 RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
110 RewriteCond %{SCRIPT_FILENAME} !maintenance.html
111 RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]
112
113 RewriteCond %{REQUEST_FILENAME} -f
114 RewriteRule ^(.*)$ $1 [QSA,L]
115
116 RewriteCond %{REQUEST_FILENAME}/index.html -f
117 RewriteRule ^(.*)$ $1/index.html [QSA,L]
118
119 RewriteCond %{REQUEST_FILENAME}.html -f
120 RewriteRule ^(.*)$ $1/index.html [QSA,L]
121
122 RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
123 </programlisting>
124
125 转换成nginx配置应该是这样:
126
127 <programlisting>
128 location / {
129 root /var/www/myapp.com/current/public;
130
131 try_files /system/maintenance.html
132 $uri $uri/index.html $uri.html
133 @mongrel;
134 }
135
136 location @mongrel {
137 proxy_pass http://mongrel;
138 }
139 </programlisting>
140 </para>
141
142 </section>
143
144 </article>