【Nginx】修改響應頭,根據不同請求IP重定向到不同IP


新的解決辦法:

iptables -t nat -I OUTPUT -d 218.94.x.x -p tcp --dport 8476 -j DNAT --to-destination 10.1.x.x:8088

service iptables save

該方法用於將外網IP和端口轉成內網IP和端口

218.94.x.x 和 8476 分別為外網IP和端口
10.1.x.x:8088 為內網IP和端口

 

 

 

 

 

背景:

使用CAS登錄的過程中會涉及到三次重定向,如果在同一個局域網內,是沒有任何問題的,但如果涉及到跨網訪問就有問題了。

解決思路:

通過Nginx對要訪問的系統進行代理,把響應頭中的重定向Location的地址改成外網能訪問到的IP,實現跨網訪問。

 

 

 

 

 

 

實現步驟:

1、安裝Nginx,安裝ngx_headers_more模塊(下載路徑:https://github.com/openresty/headers-more-nginx-module/tags

安裝方式:進入nginx的tar包解壓目錄,執行./configure --prefix==/usr/local/nginx  --add-module=/home/nginx/ngx_headers_more解壓后的目錄  --add-module=其他模塊如echo模塊

上述命令執行完成后,執行make,make install 重新安裝nginx

2、配置nginx如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    
    #高版本的Nginx用這種方式     注意:有的版本中,通過$upstream_http_Location會一直取不到值,可以使用$sent_http_location來代替,$sent_http_location是不帶IP的請求路徑
    map $sent_http_location $location{
        ~/xxx-cas([\S]+$) http://130.13.11.24:8888/xxx-cas$1;
        ~/xxx-auth([\S]*$) http://130.13.11.24:8888/xxx-auth$1;
        ~/zhcx([\S]*$) http://130.13.11.24:8888/zhcx$1;
        ~/sjpz([\S]*$) http://130.13.11.24:8888/sjpz$1;
        default abcd$sent_http_location;
    }
    
    #低版本的Nginx用這種方式     注意:有的版本中,通過$upstream_http_Location會一直取不到值,可以使用$sent_http_location來代替,$sent_http_location是不帶IP的請求路徑
    map $upstream_http_Location $location{
        ~http://192.168.0.10:8088/xxx-cas([\S]+$) http://130.13.11.24:8888/xxx-cas$1;
        ~http://192.168.0.10:8088/xxx-auth([\S]*$) http://130.13.11.24:8888/xxx-auth$1;
        ~http://192.168.0.10:8081/zhcx([\S]*$) http://130.13.11.24:8888/zhcx$1;
        ~http://192.168.0.10:8082/sjpz([\S]*$) http://130.13.11.24:8888/sjpz$1;
        default abcd$upstream_http_Location;
    }

    server {
        listen       8080;
        server_name  localhost;
        location /xxx-auth {
            proxy_pass http://192.168.0.10:8088;
            more_set_headers -s '302' "Location $location";
        }
        
        location /xxx-cas {
            proxy_pass http://192.168.0.10:8088;
            more_set_headers -s '302' "Location $location";
        }
        
        location /zhcx {
            proxy_pass http://192.168.0.10:8081;
            more_set_headers -s '302' "Location $location";
        }  
        
        location /sjpz {
            proxy_pass http://192.168.0.10:8082;
            more_set_headers -s '302' "Location $location";
        } 
    }
    
}

 

參考:

https://www.oschina.net/question/2265795_2217890


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM