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