Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/ja/docs/http/converting_rewrite_rules.xml
1 views
1
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
2
3
<article name="rewrite ルールのコンバート"
4
link="/ja/docs/http/converting_rewrite_rules.html"
5
lang="ja">
6
7
<section name="メインサイトへのリダイレクト">
8
9
<para>
10
共有のホスティングで Apache の .htaccess ファイル<i>のみ</i><i>すべて</i>を設定してきたのなら、次のようにルールをコンバートします:
11
12
<programlisting>
13
RewriteCond %{HTTP_HOST} example.org
14
RewriteRule (.*) http://www.example.org$1
15
</programlisting>
16
17
上記は下記のようになります:
18
19
<programlisting>
20
server {
21
listen 80;
22
server_name www.example.org example.org;
23
if ($http_host = example.org) {
24
rewrite (.*) http://www.example.org$1;
25
}
26
...
27
}
28
</programlisting>
29
</para>
30
31
<para>
32
これは間違っていて面倒で非効率的な方法です。正しい方法は <literal>example.org</literal> 用に別のサーバを定義します:
33
34
<programlisting>
35
server {
36
listen 80;
37
server_name example.org;
38
rewrite ^ http://www.example.org$request_uri?;
39
}
40
41
server {
42
listen 80;
43
server_name www.example.org;
44
...
45
}
46
</programlisting>
47
</para>
48
49
</section>
50
51
52
<section>
53
54
<para>
55
別の例として、<literal>example.com</literal> 以外と <literal>www.example.com</literal> 以外のすべて、という後方ロジックの代わりの例です:
56
57
<programlisting>
58
RewriteCond %{HTTP_HOST} !example.com
59
RewriteCond %{HTTP_HOST} !www.example.com
60
RewriteRule (.*) http://www.example.com$1
61
</programlisting>
62
63
この場合、単に <literal>example.com</literal><literal>www.example.com</literal>、そしてそれ以外を定義します:
64
65
66
<programlisting>
67
server {
68
listen 80;
69
server_name example.org www.example.org;
70
...
71
}
72
73
server {
74
listen 80 default_server;
75
server_name _;
76
rewrite ^ http://example.org$request_uri?;
77
}
78
</programlisting>
79
</para>
80
81
</section>
82
83
84
<section id="converting_mongrel_rules"
85
name="Mongrel ルールのコンバート">
86
87
<para>
88
典型的な Mongrel のルール:
89
90
<programlisting>
91
DocumentRoot /var/www/myapp.com/current/public
92
93
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
94
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
95
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]
96
97
RewriteCond %{REQUEST_FILENAME} -f
98
RewriteRule ^(.*)$ $1 [QSA,L]
99
100
RewriteCond %{REQUEST_FILENAME}/index.html -f
101
RewriteRule ^(.*)$ $1/index.html [QSA,L]
102
103
RewriteCond %{REQUEST_FILENAME}.html -f
104
RewriteRule ^(.*)$ $1/index.html [QSA,L]
105
106
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
107
</programlisting>
108
109
上記は次のようにコンバートされます
110
111
<programlisting>
112
location / {
113
root /var/www/myapp.com/current/public;
114
115
try_files /system/maintenance.html
116
$uri $uri/index.html $uri.html
117
@mongrel;
118
}
119
120
location @mongrel {
121
proxy_pass http://mongrel;
122
}
123
</programlisting>
124
</para>
125
126
</section>
127
128
</article>
129
130