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; }