新的解决办法:
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";
}
}
}
参考: