咻咻ing 2020-01-11
背景:
使用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";
}
}
}1 | location [=|~|~*|^~] /uri/ { … } |
示例1:
location / { }匹配任意请求
示例2:
location ~* .(gif|jpg|jpeg)$ {
rewrite .(gif|jpg|jpeg)$ /logo.png;
}不区分大小写匹配任何以gif、jpg、jpeg结尾的请求,并将该请求重定向到 /logo.png请求
示例3:
location ~ ^.+\.txt$ {
root /usr/local/nginx/html/;
}区分大小写匹配以.txt结尾的请求,并设置此location的路径是/usr/local/nginx/html/。也就是以.txt结尾的请求将访问/usr/local/nginx/html/ 路径下的txt文件
示例如下:
location ^~ /sta/ {
alias /usr/local/nginx/html/static/;
}location ^~ /tea/ {
root /usr/local/nginx/html/;
}(1)last 和 break 当出现在location 之外时,两者的作用是一致的没有任何差异
(2)last 和 break 当出现在location 内部时:
将符合某个正则表达式的URL重定向到一个固定页面
比如:我们需要将符合“/test/(\d+)/[\w-\.]+” 这个正则表达式的URL重定向到一个固定的页面。符合这个正则表达式的页面可能是:http://test.com/test/12345/abc122.html、http://test.com/test/456/11111cccc.js等
从上面的介绍可以看出,这里可以使用rewrite重定向或者alias关键字来达到我们的目的。因此,这里可以这样做:
(1)使用rewrite关键字:
location ~ ^.+\.txt$ {
root /usr/local/nginx/html/;
}
location ~* ^/test/(\d+)/[\w-\.]+$ {
rewrite ^/test/(\d+)/[\w-\.]+$ /testpage.txt last;
}这里将所有符合条件的URL(PS:不区分大小写)都重定向到/testpage.txt请求,也就是 /usr/local/nginx/html/testpage.txt 文件
(2)使用alias关键字:
location ~* ^/test/(\d+)/[\w-\.]+$ {
alias /usr/local/nginx/html/static/sta1.html;
}这里将所有符合条件的URL(PS:不区分大小写)都重定向到/usr/local/nginx/html/static/sta1.html 文件
示范:
server {
listen 80;
listen 443 ssl;
server_name ~^((cloud)|(demo-cloud)|(demo2-cloud)|(demo3-cloud)|(approval))((\.italent\.link)|(\.italent-inc\.cn)|(\.beisen\.cn))$;
ssl on;
ssl_certificate ./ssl/ssl.crt;
ssl_certificate_key ./ssl/ssl.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
ssl_prefer_server_ciphers on;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_session_timeout 5m;
location ~* .*mrest.* {
proxy_pass https://10.129.8.77:443;
proxy_http_version 1.1;
proxy_connect_timeout 9990;
proxy_send_timeout 9990;
proxy_read_timeout 9990;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header x-ssl-enabled true;
proxy_set_header X-Nginx-Proxy true;
}
location / {
index index.html index.htm;
proxy_pass http://127.0.0.1:8088;
proxy_http_version 1.1;
proxy_connect_timeout 9990;
proxy_send_timeout 9990;
proxy_read_timeout 9990;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header x-ssl-enabled true;
proxy_set_header X-Nginx-Proxy true;
}
}
参考: