• nginx的proxy_redirect


    proxy_redirect

    语法:proxy_redirect [ default|off|redirect replacement ];
    默认:proxy_redirect default;
    配置块:http、server、location
    当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。

            location /login {
                proxy_pass http://target_servers/login ;
            }
    

    假设当前nginx的访问地址为http://192.168.99.100:8080,如果target_servers又有302到192.168.99.100/xxx
    那么可以添加下redirect,将302的location改为http://192.168.99.100:8080/xxx

            location /login {
                proxy_pass http://target_servers/login ;
                proxy_redirect http://192.168.99.100/ http://192.168.99.100:8080/;
            }
    

    host变量

    如果不想写死ip地址,可以使用nginx的变量

            location /login {
                proxy_pass http://target_servers/login ;
                proxy_redirect http://$host/ http://$http_host/;
            }
    

    其中host不带端口的,也就是nginx部署的主机ip,而$http_host是带端口的

    NGINX的proxy_redirect功能比较强大,其作用是对发送给客户端的URL进行修改。以例子说明: 

       server { 
           listen       80; 
           server_name  test.abc.com; 
           location / { 
                proxy_pass http://10.10.10.1:9080; 
           } 
       }这段配置一般情况下都正常,但偶尔会出错, 错误在什么地方呢? 抓包发现服务器给客户端的跳转指令里加了端口号,如 Location: http://test.abc.com:9080/abc.html 。因为nginx服务器侦听的是80端口,所以这样的URL给了客户端,必然会出错.针对这种情况, 加一条proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的内容替换成“/”再发给客户端,就解决了。 

       server { 
           listen       80; 
           server_name  test.abc.com; 
           proxy_redirect http://test.abc.com:9080/ /; 
           location / { 
                proxy_pass http://10.10.10.1:9080; 
           } 
       } 


    http://nginx.179401.cn/ 
    圣地啊 加红 加粗~!! 


    出处:http://nginx.179401.cn/StandardHTTPModules/HTTPProxy.html 
    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/   /; 
    利用这个指令可以为被代理服务器发出的相对重定向增加主机名:

  • 相关阅读:
    pycharm的常规使用
    python-引用/模块
    6-4 函数
    5-21文件的操作
    5-21python数据类型
    python-基础
    5-7接口测试工具之jmeter的使用
    接口测试基础
    把命令结果作为变量赋值
    shell变量子串
  • 原文地址:https://www.cnblogs.com/zhengchunyuan/p/10636619.html
Copyright © 2020-2023  润新知