背景:
使用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"; } } }
一 Nginx的location語法
1
|
location [=|~|~*|^~] /uri/ { … }
|
- = 嚴格匹配。如果請求匹配這個location,那么將停止搜索並立即處理此請求
- ~ 區分大小寫匹配(可用正則表達式)
- ~* 不區分大小寫匹配(可用正則表達式)
- !~ 區分大小寫不匹配
- !~* 不區分大小寫不匹配
- ^~ 如果把這個前綴用於一個常規字符串,那么告訴nginx 如果路徑匹配那么不測試正則表達式
示例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文件
二 alias與root的區別
- root 實際訪問文件路徑會拼接URL中的路徑
- alias 實際訪問文件路徑不會拼接URL中的路徑
示例如下:
location ^~ /sta/ { alias /usr/local/nginx/html/static/; }
- 請求:http://test.com/sta/sta1.html
- 實際訪問:/usr/local/nginx/html/static/sta1.html 文件
location ^~ /tea/ { root /usr/local/nginx/html/; }
- 請求:http://test.com/tea/tea1.html
- 實際訪問:/usr/local/nginx/html/tea/tea1.html 文件
三 last 和 break關鍵字的區別
(1)last 和 break 當出現在location 之外時,兩者的作用是一致的沒有任何差異
(2)last 和 break 當出現在location 內部時:
- last 使用了last 指令,rewrite 后會跳出location 作用域,重新開始再走一次剛才的行為
- break 使用了break 指令,rewrite后不會跳出location 作用域,它的生命也在這個location中終結
四 permanent 和 redirect關鍵字的區別
- rewrite … permanent 永久性重定向,請求日志中的狀態碼為301
- rewrite … redirect 臨時重定向,請求日志中的狀態碼為302
五 綜合實例
將符合某個正則表達式的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;
}
}
參考:
- http://www.php100.com/html/program/nginx/2013/0905/5535.html
- http://unixman.blog.51cto.com/10163040/1711943
- https://my.oschina.net/aiguozhe/blog/115510
- https://zhuanlan.zhihu.com/p/24524057
- https://segmentfault.com/a/1190000002797606
- https://www.cnblogs.com/duhuo/p/8323812.html
- https://www.cnblogs.com/aligege/p/10542591.html