nginx配备中proxy_redirect的作用

http://www.docin.com/p-292511923.html  使用keepalived对nginx负载均衡器热备 负载均衡 多个squid

 

摘要:proxy_pass 语法 :proxy_pass URL 默认值 :no 使用字段 :location, location中的if字段 这个指令设置被代理服务器的地址和被映射的URI,地址可以使用主机名或IP加

 

 

 

proxy_pass

语法:proxy_pass URL 
默认值:no 
使用字段:location, location中的if字段 
这个指令设置被代理服务器的地址和被映射的URI,地址可以使用主机名或IP加端口号的形式,例如:

proxy_pass http://localhost:8000/uri/;

或者一个unix socket:

proxy_pass http://unix:/path/to/backend.socket:/uri/;

路径在unix关键字的后面指定,位于两个冒号之间。
注意:HTTP Host头没有转发,它将设置为基于proxy_pass声明,例如,如果你移动虚拟主机example.com到另外一台机器,然后重新配置正常(监听example.com到一个新的IP),同时在旧机器上手动将新的example.comIP写入/etc/hosts,同时使用proxy_pass重定向到http://example.com, 然后修改DNS到新的IP。
当传递请求时,Nginx将location对应的URI部分替换成proxy_pass指令中所指定的部分,但是有两个例外会使其无法确定如何去替换:

  • location通过正则表达式指定;
  • 在使用代理的location中利用rewrite指令改变URI,使用这个配置可以更加精确的处理请求(break):
location  /name/ {
  rewrite      /name/([^/] +)  /users?name=$1  break;
  proxy_pass   http://127.0.0.1;
}

这些情况下URI并没有被映射传递。
此外,需要标明一些标记以便URI将以和客户端相同的发送形式转发,而不是处理过的形式,在其处理期间:

  • 两个以上的斜杠将被替换为一个: ”//” – ”/”;
  • 删除引用的当前目录:”/./” – ”/”;
  • 删除引用的先前目录:”/dir /../” – ”/“。

如果在服务器上必须以未经任何处理的形式发送URI,那么在proxy_pass指令中必须使用未指定URI的部分:

location  /some/path/ {
  proxy_pass   http://127.0.0.1;
}

在指令中使用变量是一种比较特殊的情况:被请求的URL不会使用并且你必须完全手工标记URL。
这意味着下列的配置并不能让你方便的进入某个你想要的虚拟主机目录,代理总是将它转发到相同的URL(在一个server字段的配置):

location / {
  proxy_pass   http://127.0.0.1:8080/VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot;
}

解决方法是使用rewrite和proxy_pass的组合:

location / {
  rewrite ^(.*)$ /VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot$1 break;
  proxy_pass   http://127.0.0.1:8080;
}

这种情况下请求的URL将被重写, proxy_pass中的拖尾斜杠并没有实际意义。
如果需要通过ssl信任连接到一个上游服务器组,proxy_pass前缀为 https://,并且同时指定ssl的端口,如:

upstream backend-secure {
  server 10.0.0.20:443;
}
 
server {
  listen 10.0.0.1:443;
  location / {
    proxy_pass https://backend-secure;
  }
}

proxy_pass_header

语法:proxy_pass_header the_name 
使用字段:http, server, location 
这个指令允许为应答转发一些隐藏的头部字段。
如:

location / {
  proxy_pass_header X-Accel-Redirect;
}

proxy_pass_request_body

语法:proxy_pass_request_body [ on | off ] ; 
默认值:proxy_pass_request_body on; 
使用字段:http, server, location 
可用版本:0.1.29
不管什么情况都将请求实体通过代理。
通常一直处于on状态。

proxy_pass_request_headers

语法:proxy_pass_request_headers [ on | off ] ; 
默认值:proxy_pass_request_headers on; 
使用字段:http, server, location 
可用版本:0.1.29
不管什么情况都将请求头部通过代理。
通常一直处于on状态。

proxy_redirect

语法proxy_redirect [ default|off|redirect replacement ] 
默认值proxy_redirect default 
使用字段:http, server, location 
如果需要修改从被代理服务器传来的应答头中的”Location”和”Refresh”字段,可以用这个指令设置。
假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
这个指令:

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

将Location字段重写为http://frontend/one/some/uri/。
在代替的字段中可以不写服务器名:

proxy_redirect http://localhost:8000/two/ /;

这样就使用服务器的基本名称和端口,即使它来自非80端口。
如果使用“default”参数,将根据location和proxy_pass参数的设置来决定。
例如下列两个配置等效:

location /one/ {
  proxy_pass       http://upstream:port/two/;
  proxy_redirect   default;
}
 
location /one/ {
  proxy_pass       http://upstream:port/two/;
  proxy_redirect   http://upstream:port/two/   /one/;
}

在指令中可以使用一些变量:

proxy_redirect   http://localhost:8000/    http://$host:$server_port/;

这个指令有时可以重复:

  proxy_redirect   default;
  proxy_redirect   http://localhost:8000/    /;
  proxy_redirect   http://www.example.com/   /;

参数off将在这个字段中禁止所有的proxy_redirect指令:

  proxy_redirect   off;
  proxy_redirect   default;
  proxy_redirect   http://localhost:8000/    /;
  proxy_redirect   http://www.example.com/   /;

利用这个指令可以为被代理服务器发出的相对重定向增加主机名:

proxy_redirect   /   /;

proxy_read_timeout

语法:proxy_read_timeout time 
默认值:proxy_read_timeout 60s 
使用字段:http, server, location 
决定读取后端服务器应答的超时时间,单位为秒,它决定nginx将等待多久时间来取得一个请求的应答。超时时间是指完成了两次握手后并且状态为established的超时时间。
相对于proxy_connect_timeout,这个时间可以扑捉到一台将你的连接放入连接池延迟处理并且没有数据传送的服务器,注意不要将此值设置太低,某些情况下代理服务器将花很长的时间来获得页面应答(例如如当接收一个需要很多计算的报表时),当然你可以在不同的location里面设置不同的值。
可以通过指定时间单位以免引起混乱,支持的时间单位有”s”(秒), “ms”(毫秒), “y”(年), “M”(月), “w”(周), “d”(日), “h”(小时),和 “m”(分钟)。
这个值不能大于597小时。

proxy_redirect

语法proxy_redirect [ default|off|redirect replacement ];
默认值proxy_redirect default;
使用字段:http, server, location
这个指令为被代理服务器应答中必须改变的应答头:”Location”和”Refresh”设置值。
我们假设被代理的服务器返回的应答头字段为:Location: http://localhost:8000/two/some/uri/。
指令:

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

会将其重写为:Location: http://frontend/one/some/uri/。
在重写字段里面可以不使用服务器名:

proxy_redirect http://localhost:8000/two/ /;

这样,默认的服务器名和端口将被设置,端口默认80。
默认的重写可以使用参数default,将使用location和proxy_pass的值。
下面两个配置是等价的:

location /one/ {
  proxy_pass       http://upstream:port/two/;
  proxy_redirect   default;
}
 
location /one/ {
  proxy_pass       http://upstream:port/two/;
  proxy_redirect   http://upstream:port/two/   /one/;
}

同样,在重写字段中可以使用变量:

proxy_redirect   http://localhost:8000/    http://$host:$server_port/;

这个指令可以重复使用:

proxy_redirect   default;
proxy_redirect   http://localhost:8000/    /;
proxy_redirect   http://www.example.com/   /;

参数off在本级中禁用所有的proxy_redirect指令:

proxy_redirect   off;
proxy_redirect   default;
proxy_redirect   http://localhost:8000/    /;
proxy_redirect   http://www.example.com/   /;

这个指令可以很容易的将被代理服务器的服务器名重写为代理服务器的服务器名:

proxy_redirect   /   /;

proxy_redirect_errors

不推荐使用,请使用 proxy_intercept_errors。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值