syntax: rewrite regex replacement [flag] Default: — Context: server, location, if
rewrite ^/users/(.*)$ /show?user=$1? last;=
Nginx配置proxy_pass轉發的/路徑問題html
在nginx中配置proxy_pass時,若是是按照^~匹配路徑時,要注意proxy_pass後的url最後的/,當加上了/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;若是沒有/,則會把匹配的路徑部分也給代理走。nginx
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}正則表達式
如上面的配置,若是請求的url是http://servername/static_js/test.html
會被代理成http://js.test.com/test.html後端
而若是這麼配置瀏覽器
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}url
則會被代理到http://js.test.com/static_js/test.htmspa
固然,咱們能夠用以下的rewrite來實現/的功能代理
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)$ /$1 break;
proxy_pass http://js.test.com;
} unix
Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except
proxy_pass http://localhost:8000/uri/;
upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://backend; } }
proxy_pass http://$host$uri;
location /name/ { proxy_pass http://127.0.0.1/remote/; } 請求http://127.0.0.1/name/test.html 會被代理到http://example.com/remote/test.html
location /name/ { proxy_pass http://127.0.0.1; } 請求http://127.0.0.1/name/test.html 會被代理到http://127.0.0.1/name/test.html
location /name/ { rewrite /name/([^/]+) /users?name=$1 break; proxy_pass http://127.0.0.1; }