RuHttpRewriteModule
Contents |
[edit] ngx_http_rewrite_module
袦芯写褍谢褜 ngx_http_rewrite_module 锌芯蟹胁芯谢褟械褌 懈蟹屑械薪褟褌褜 URI 褋 锌芯屑芯褖褜褞 褉械谐褍谢褟褉薪褘褏 胁褘褉邪卸械薪懈泄, 写械谢邪褌褜 褉械写懈褉械泻褌褘 懈 胁褘斜懈褉邪褌褜 泻芯薪褎懈谐褍褉邪褑懈褞 胁 蟹邪胁懈褋懈屑芯褋褌懈 芯褌 锌械褉械屑械薪薪褘褏. 袝褋谢懈 写懈褉械泻褌懈胁褘 褝褌芯谐芯 屑芯写褍谢褟 芯锌懈褋邪薪褘 薪邪 褍褉芯胁薪械 褋械褉胁械褉邪, 褌芯 芯薪懈 胁褘锌芯谢薪褟褞褌褋褟 写芯 褌芯谐芯, 泻邪泻 芯锌褉械写械谢褟械褌褋褟 location 写谢褟 蟹邪锌褉芯褋邪. 袝褋谢懈 胁 胁褘斜褉邪薪薪芯屑 location 褌芯卸械 械褋褌褜 写懈褉械泻褌懈胁褘 屑芯写褍谢褟 ngx_http_rewrite_module, 褌芯 芯薪懈 褌邪泻卸械 胁褘锌芯谢薪褟褞褌褋褟. 袝褋谢懈 URI 懈蟹屑械薪懈谢褋褟 胁 褉械蟹褍谢褜褌邪褌械 懈褋锌芯谢薪械薪懈褟 写懈褉械泻褌懈胁 胁薪褍褌褉懈 location, 褌芯 褋薪芯胁邪 芯锌褉械写械谢褟械褌褋褟 location 写谢褟 褍卸械 薪芯胁芯谐芯 URI. 协褌芯褌 褑懈泻谢 屑芯卸械褌 锌芯胁褌芯褉褟褌褜褋褟 写芯 10 褉邪蟹, 锌芯褋谢械 褔械谐芯 nginx 胁芯蟹胁褉邪褖邪械褌 芯褕懈斜泻褍 "Server Internal Error" (500). 小芯写械褉卸邪薪懈械
[edit] 袛懈褉械泻褌懈胁褘
- [#break break]
- [#if if]
- [#return return]
- [#rewrite rewrite]
- [#set set]
- [#uninitialized_variable_warn uninitialized_variable_warn]
袙薪褍褌褉械薪薪械械 褍褋褌褉芯泄褋褌胁芯
[edit] break
syntax: break
default: 薪械褌
context: server, location, if
袛懈褉械泻褌懈胁邪 蟹邪胁械褉褕邪械褌 芯斜褉邪斜芯褌泻褍 褌械泻褍褖械谐芯 薪邪斜芯褉邪 写懈褉械泻褌懈胁 ngx_http_rewrite_module.
袩褉懈屑械褉 懈褋锌芯谢褜蟹芯胁邪薪懈褟:
: if ($slow) { : limit_rate 10k; : break; : }
[edit] if
syntax: if (褍褋谢芯胁懈械) { ... }
default: 薪械褌
context: server, location
袛懈褉械泻褌懈胁邪 if 锌褉芯胁械褉褟械褌 懈褋褌懈薪薪芯褋褌褜 褍褋谢芯胁懈褟, 械褋谢懈 芯薪芯 懈褋褌懈薪薪芯, 褌芯 胁褘锌芯谢薪褟械褌褋褟 褍泻邪蟹邪薪薪褘泄 胁 褎懈谐褍褉薪褘褏 褋泻芯斜泻邪褏 泻芯写 懈 蟹邪锌褉芯褋 芯斜褉邪斜邪褌褘胁邪械褌褋褟 胁 褋芯芯褌胁械褌褋褌胁懈懈 褋 蟹邪写邪薪薪芯泄 褌邪屑 卸械 泻芯薪褎懈谐褍褉邪褑懈械泄. 袣芯薪褎懈谐褍褉邪褑懈褟 胁薪褍褌褉懈 写懈褉械泻褌懈胁褘 if 薪邪褋谢械写褍械褌褋褟 懈蟹 锌褉械写褘写褍褖械谐芯 褍褉芯胁薪褟.
袙 泻邪褔械褋褌胁械 褍褋谢芯胁懈褟 屑芯谐褍褌 斜褘褌褜 蟹邪写邪薪褘:
- 懈屑褟 锌械褉械屑械薪薪芯泄; 谢芯卸薪褘屑懈 蟹薪邪褔械薪懈褟屑懈 锌械褉械屑械薪薪芯泄 褟胁谢褟褞褌褋褟 锌褍褋褌邪褟 褋褌褉芯泻邪 "" 懈谢懈 谢褞斜邪褟 褋褌褉芯泻邪, 薪邪褔懈薪邪褞褖懈械褋褟 薪邪 "0";
- 褋褉邪胁薪械薪懈械 锌械褉械屑械薪薪芯泄 褋芯 褋褌褉芯泻芯泄 褋 锌芯屑芯褖褜褞 芯锌械褉邪褌芯褉芯胁 "=" 懈 "!=";
- 锌褉芯胁械褉泻邪 锌械褉械屑械薪薪芯泄 褋 锌芯屑芯褖褜褞 褉械谐褍谢褟褉薪芯谐芯 胁褘褉邪卸械薪懈褟 斜械蟹 褍褔褢褌邪 褉械谐懈褋褌褉邪 褋懈屑胁芯谢芯胁 鈥 "~*" 懈 褋 褍褔褢褌芯屑 鈥 "~". 袙 褉械谐褍谢褟褉薪褘褏 胁褘褉邪卸械薪懈褟褏 屑芯卸薪芯 懈褋锌芯谢褜蟹芯胁邪褌褜 胁褘写械谢械薪懈褟, 泻芯褌芯褉褘械 蟹邪褌械屑 写芯褋褌褍锌薪褘 胁 胁懈写械 锌械褉械屑械薪薪褘褏 $1 鈥 $9. 孝邪泻卸械 屑芯卸薪芯 懈褋锌芯谢褜蟹芯胁邪褌褜 芯褌褉懈褑邪褌械谢褜薪褘械 芯锌械褉邪褌芯褉褘 "!~" 懈 "!~*".
- 锌褉芯胁械褉泻邪 褋褍褖械褋褌胁芯胁邪薪懈褟 褎邪泄谢邪 褋 锌芯屑芯褖褜褞 芯锌械褉邪褌芯褉芯胁 "-f" 懈 "!-f";
- 锌褉芯胁械褉泻邪 褋褍褖械褋褌胁芯胁邪薪懈褟 泻邪褌邪谢芯谐邪 褋 锌芯屑芯褖褜褞 芯锌械褉邪褌芯褉芯胁 "-d" 懈 "!-d";
- 锌褉芯胁械褉泻邪 褋褍褖械褋褌胁芯胁邪薪懈褟 褎邪泄谢邪, 泻邪褌邪谢芯谐邪 懈谢懈 褋懈屑胁芯谢懈褔械褋泻芯泄 褋褋褘谢泻懈 褋 锌芯屑芯褖褜褞 芯锌械褉邪褌芯褉芯胁 "-e" 懈 "!-e";
- 锌褉芯胁械褉泻邪 懈褋锌芯谢薪褟械屑芯褋褌懈 褎邪泄谢邪 褋 锌芯屑芯褖褜褞 芯锌械褉邪褌芯褉芯胁 "-x" 懈 "!-x".
袩褉懈屑械褉褘 懈褋锌芯谢褜蟹芯胁邪薪懈褟:
: if ($http_user_agent ~ MSIE) { : rewrite ^(.*)$ /msie/$1 break; : } : if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) { : set $id $1; : } : if ($request_method = POST ) { : return 405; : } : if (!-f $request_filename) { : break; : proxy_pass http://127.0.0.1; : } : if ($slow) { : limit_rate 10k; : } : if ($invalid_referer) { : return 403; : }
袟薪邪褔械薪懈械 胁褋褌褉芯械薪薪芯泄 锌械褉械屑械薪薪芯泄 $invalid_referer 蟹邪写邪褢褌褋褟 写懈褉械泻褌懈胁芯泄 valid_referers.
[edit] return
syntax: return 泻芯写
default: 薪械褌
context: server, location, if
袛懈褉械泻褌懈胁邪 return 蟹邪胁械褉褕邪械褌 懈褋锌芯谢薪械薪懈械 泻芯写邪 懈 胁芯蟹胁褉邪褖邪械褌 泻谢懈械薪褌褍 褍泻邪蟹邪薪薪褘泄 泻芯写. 袦芯卸薪芯 懈褋锌芯谢褜蟹芯胁邪褌褜 褋谢械写褍褞褖懈械 蟹薪邪褔械薪懈褟: 204, 400, 402 鈥 406, 408, 410, 411, 413, 416 懈 500 鈥 504. 袣褉芯屑械 褌芯谐芯, 薪械褋褌邪薪写邪褉褌薪褘泄 泻芯写 444 蟹邪泻褉褘胁邪械褌 褋芯械写懈薪械薪懈械 斜械蟹 锌械褉械写邪褔懈 蟹邪谐芯谢芯胁泻邪 芯褌胁械褌邪.
[edit] rewrite
syntax: rewrite regex 蟹邪屑械薪邪 褎谢邪谐
default: 薪械褌
context: server, location, if
袛懈褉械泻褌懈胁邪 rewrite 懈蟹屑械薪褟械褌 URI 胁 褋芯芯褌胁械褌褋褌胁懈懈 褋 褉械谐褍谢褟褉薪褘屑 胁褘褉邪卸械薪懈械屑 懈 褋褌褉芯泻芯泄 蟹邪屑械薪褘. 袛懈褉械泻褌懈胁褘 胁褘锌芯谢薪褟褞褌褋褟 胁 锌芯褉褟写泻械 懈褏 褋谢械写芯胁邪薪懈褟 胁 泻芯薪褎懈谐褍褉邪褑懈芯薪薪芯屑 褎邪泄谢械. 小 锌芯屑芯褖褜褞 褎谢邪谐芯胁 屑芯卸薪芯 写芯褋褉芯褔薪芯 锌褉械泻褉邪褌懈褌褜 懈褋锌芯谢薪械薪懈械 写懈褉械泻褌懈胁. 袝褋谢懈 褋褌褉芯泻邪 蟹邪屑械薪褘 薪邪褔懈薪邪械褌褋褟 褋 "http://", 褌芯 泻谢懈械薪褌褍 斜褍写械褌 胁芯蟹胁褉邪褖褢薪 褉械写懈褉械泻褌 懈 芯斜褉邪斜芯褌泻邪 写懈褉械泻褌懈胁 褌邪泻卸械 蟹邪胁械褉褕邪械褌褋褟.
肖谢邪谐懈 屑芯谐褍褌 斜褘褌褜 褋谢械写褍褞褖懈屑懈:
- last 鈥 蟹邪胁械褉褕邪械褌 芯斜褉邪斜芯褌泻褍 褌械泻褍褖械谐芯 薪邪斜芯褉邪 写懈褉械泻褌懈胁 ngx_http_rewrite_module, 锌芯褋谢械 褔械谐芯 懈褖械褌褋褟 褋芯芯褌胁械褌褋褌胁懈械 URI 懈 location;
- break 鈥 蟹邪胁械褉褕邪械褌 芯斜褉邪斜芯褌泻褍 褌械泻褍褖械谐芯 薪邪斜芯褉邪 写懈褉械泻褌懈胁 ngx_http_rewrite_module;
- redirect 鈥 胁芯蟹胁褉邪褖邪械褌 胁褉械屑械薪薪褘泄 褉械写懈褉械泻褌 褋 泻芯写芯屑 302; 懈褋锌芯谢褜蟹褍械褌褋褟, 械褋谢懈 蟹邪屑械薪褟褞褖邪褟 褋褌褉芯泻邪 薪械 薪邪褔懈薪邪械褌褋褟 褋 "http://";
- permanent 鈥 胁芯蟹胁褉邪褖邪械褌 锌芯褋褌芯褟薪薪褘泄 褉械写懈褉械泻褌 褋 泻芯写芯屑 301.
袩褉懈屑械褉 懈褋锌芯谢褜蟹芯胁邪薪懈褟:
: rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; : rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; : return 403;
袝褋谢懈 卸械 褝褌懈 写懈褉械泻褌懈胁褘 锌芯屑械褋褌懈褌褜 胁 location /download/, 褌芯 薪褍卸薪芯 蟹邪屑械薪懈褌褜 褎谢邪谐 last 薪邪 break, 懈薪邪褔械 nginx 褋写械谢邪械褌 10 褑懈泻谢芯胁 懈 胁械褉薪褢褌 芯褕懈斜泻褍 500:
: location /download/ { : rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; : rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; : return 403; : }
袝褋谢懈 胁 褋褌褉芯泻械 蟹邪屑械薪褘 褍泻邪蟹邪薪褘 邪褉谐褍屑械薪褌褘, 褌芯 锌褉械写褘写褍褖懈械 邪褉谐褍屑械薪褌褘 蟹邪锌褉芯褋邪 写芯斜邪胁谢褟褞褌褋褟 锌芯褋谢械 薪懈褏. 袦芯卸薪芯 芯褌泻邪蟹邪褌褜褋褟 芯褌 褝褌芯谐芯 写芯斜邪胁谢械薪懈褟, 褍泻邪蟹邪胁 胁 泻芯薪褑械 褋褌褉芯泻懈 蟹邪屑械薪褘 蟹薪邪泻 胁芯锌褉芯褋邪:
: rewrite ^/users/(.*)$ /show?user=$1? last;
[edit] set
syntax: set 锌械褉械屑械薪薪邪褟 蟹薪邪褔械薪懈械
default: 薪械褌
context: server, location, if
袛懈褉械泻褌懈胁邪 褍褋褌邪薪邪胁谢懈胁邪械褌 蟹薪邪褔械薪懈械 写谢褟 褍泻邪蟹邪薪薪芯泄 锌械褉械屑械薪薪芯泄. 袙 泻邪褔械褋褌胁械 蟹薪邪褔械薪懈褟 屑芯卸薪芯 懈褋锌芯谢褜蟹芯胁邪褌褜 褌械泻褋褌, 锌械褉械屑械薪薪褘械 懈 懈褏 泻芯屑斜懈薪邪褑懈懈.
[edit] uninitialized_variable_warn
syntax: uninitialized_variable_warn on|off
default: uninitialized_variable_warn on
context: http, server, location, if
袛懈褉械泻褌懈胁邪 芯锌褉械写械谢褟械褌, 薪褍卸薪芯 谢懈 锌懈褋邪褌褜 胁 谢芯谐 锌褉械写褍锌褉械卸写械薪懈械 芯 薪械懈薪懈褑懈邪谢懈蟹懈褉芯胁邪薪薪芯泄 锌械褉械屑械薪薪芯泄. 袙薪褍褌褉械薪薪械械 褍褋褌褉芯泄褋褌胁芯
袛懈褉械泻褌懈胁褘 屑芯写褍谢褟 ngx_http_rewrite_module 泻芯屑锌懈谢懈褉褍械褌褋褟 薪邪 褋褌邪写懈懈 泻芯薪褎懈谐褍褉懈褉芯胁邪薪懈褟 胁芯 胁薪褍褌褉械薪薪懈械 泻芯写褘, 懈褋锌芯谢薪褟械屑褘械 胁芯 胁褉械屑褟 蟹邪锌褉芯褋邪 懈薪褌械褉锌褉械褌邪褌芯褉芯屑. 袠薪褌械褉锌褉械褌邪褌芯褉 锌褉械写褋褌邪胁谢褟械褌 懈蟹 褋械斜褟 锌褉芯褋褌褍褞 褋褌械泻芯胁褍褞 胁懈褉褌褍邪谢褜薪褍褞 屑邪褕懈薪褍.
袧邪锌褉懈屑械褉, 写懈褉械泻褌懈胁褘
: location /download/ { : if ($forbidden) { : return 403; : } : if ($slow) { : limit_rate 10k; : } : rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break; : }
斜褍写械褌 褋泻芯屑锌懈谢懈褉芯胁邪薪褘 胁 褌邪泻懈械 泻芯写褘:
: 锌械褉械屑械薪薪邪褟 $forbidden : 锌褉芯胁械褉泻邪 薪邪 薪芯谢褜 : 胁芯蟹胁褉邪褌 403 : 蟹邪胁械褉褕械薪懈械 胁褋械谐芯 泻芯写邪 : 锌械褉械屑械薪薪邪褟 $slow : 锌褉芯胁械褉泻邪 薪邪 薪芯谢褜 : 锌褉芯胁械褉泻邪 褉械谐褍谢褟褉薪芯谐芯 胁褘褉邪卸械薪懈褟 : 泻芯锌懈褉芯胁邪薪懈械 "/" : 泻芯锌懈褉芯胁邪薪懈械 $1 : 泻芯锌懈褉芯胁邪薪懈械 "/mp3/" : 泻芯锌懈褉芯胁邪薪懈械 $2 : 泻芯锌懈褉芯胁邪薪懈械 ".mp3" : 蟹邪胁械褉褕械薪懈械 褉械谐褍谢褟褉薪芯谐芯 胁褘褉邪卸械薪懈褟 : 蟹邪胁械褉褕械薪懈械 胁褋械谐芯 泻芯写邪
袨斜褉邪褌懈褌械 胁薪懈屑邪薪懈械, 褔褌芯 泻芯写邪 写谢褟 写懈褉械泻褌懈胁褘 limit_rate 薪械褌, 锌芯褋泻芯谢褜泻褍 芯薪邪 薪械 懈屑械械褌 芯褌薪芯褕械薪懈褟 泻 屑芯写褍谢褞 ngx_http_rewrite_module. 袛谢褟 斜谢芯泻邪 if 褋芯蟹写邪褢褌褋褟 褌邪泻邪褟 卸械 泻芯薪褎懈谐褍褉邪褑懈褟, 泻邪泻 懈 写谢褟 斜谢芯泻邪 location. 袝褋谢懈 褍褋谢芯胁懈械 懈褋褌懈薪薪芯, 褌芯 蟹邪锌褉芯褋 锌芯谢褍褔邪械褌 泻芯薪褎懈谐褍褉邪褑懈褞, 褋芯芯褌胁械褌褋褌胁褍褞褖褍褞 斜谢芯泻褍 if, 懈 胁 褝褌芯泄 泻芯薪褎懈谐褍褉邪褑懈懈 limit_rate 褉邪胁械薪 10k.
袛懈褉械泻褌懈胁褍
: rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
屑芯卸薪芯 褋写械谢邪褌褜 薪邪 芯写懈薪 泻芯写 屑械薪褜褕械, 械褋谢懈 胁 褉械谐褍谢褟褉薪芯屑 胁褘褉邪卸械薪懈懈 胁泻谢褞褔懈褌褜 锌械褉胁褘泄 褋谢褝褕 胁 褋泻芯斜泻懈:
: rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
褌芯谐写邪 械褢 泻芯写褘 斜褍写褍褌 胁褘谐谢褟写械褌褜 褌邪泻:
: 锌褉芯胁械褉泻邪 褉械谐褍谢褟褉薪芯谐芯 胁褘褉邪卸械薪懈褟 : 泻芯锌懈褉芯胁邪薪懈械 $1 : 泻芯锌懈褉芯胁邪薪懈械 "/mp3/" : 泻芯锌懈褉芯胁邪薪懈械 $2 : 泻芯锌懈褉芯胁邪薪懈械 ".mp3" : 蟹邪胁械褉褕械薪懈械 褉械谐褍谢褟褉薪芯谐芯 胁褘褉邪卸械薪懈褟 : 蟹邪胁械褉褕械薪懈械 胁褋械谐芯 泻芯写邪