经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
Nginx之proxy_redirect使用详解
来源:jb51  时间:2018/12/17 9:23:21  对本文有异议

今天在做nginx反向代理apache的时候出了一点点问题,原来后端apache用的端口是8080通过反向代理后,使用wireshark抓包发现location头域数值为http://192.168.1.154:8080/wuman/  如果把这个返回给客户端肯定是不可以的,看起来别扭而且还暴露了apache的具体信息

所以在这里用到了nginx的proxy_redirect指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值

以下是截取nginx的一小段配置文档

  1. server {
  2. listen 80;
  3. server_name www.boke.com;
  4. location / {
  5. proxy_pass http://192.168.1.154:8080;
  6. proxy_redirect off;
  7. }
  8. }
  9.  

此时我们通过curl查看结果得出

  1. [root@localhost nginx]# curl -I http://www.boke.com/wuman
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx
  4. Date: Thu, 24 Dec 2015 12:02:00 GMT
  5. Content-Type: text/html; charset=iso-8859-1
  6. Connection: keep-alive
  7. Location: http://192.168.1.154:8080/wuman/
  8.  

这里location为带有后端服务器实际地址跟端口的响应头信息这样在实际线上是不允许的所以这里我们打算通过proxy_redirect将被代理服务器的响应头中的location字段进行修改后返回给客户端

  1. server {
  2. listen 80;
  3. server_name www.boke.com;
  4. location / {
  5. proxy_pass http://192.168.1.154:8080;
  6. proxy_redirect http://192.168.1.154:8080/wuman/ http://www.boke.com/wuman/;
  7. }
  8.  
  9. server {
  10. listen 80;
  11. server_name www.boke.com;
  12. location / {
  13. proxy_pass http://192.168.1.154:8080;
  14. proxy_redirect ~^http://192.168.1.154:8080(.*) http://www.boke.com$1;
  15. }
  16.  

则curl查看返回结果

  1. [root@localhost nginx]# curl -I http://www.boke.com/wuman
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx
  4. Date: Thu, 24 Dec 2015 12:08:34 GMT
  5. Content-Type: text/html; charset=iso-8859-1
  6. Connection: keep-alive
  7. Location: http://www.boke.com/wuman/
  8.  

此时查看location已经变成了我们想要的结果了。 此时通过replacement 301重定向到了我们新的页面

出处:

proxy_redirect

语法:proxy_redirect [ default|off|redirect replacement ] 

默认值:proxy_redirect default 

使用字段:http, server, location 

如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置。

假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/

这个指令: 

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

将Location字段重写为http://frontend/one/some/uri/。

在代替的字段中可以不写服务器名:

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

这样就使用服务器的基本名称和端口,即使它来自非80端口。

如果使用“default”参数,将根据location和proxy_pass参数的设置来决定。

例如下列两个配置等效:

  1. location / one / {
  2. proxy_pass http: //upstream:port/two/;
  3. proxy_redirect default;
  4. }
  5. location / one / {
  6. proxy_pass http: //upstream:port/two/;
  7. proxy_redirect http: //upstream:port/two/ /one/;
  8. }

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

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

这个指令有时可以重复:

  1. proxy_redirect default;
  2. proxy_redirect http://localhost:8000//; proxy_redirect ; /;

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

  1. proxy_redirect off;
  2. proxy_redirect default;
  3. proxy_redirect http://localhost:8000/ /; proxy_redirect ; /;

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号