Nginx的location匹配规则-根据url 路径重定向到不同的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";
        } 
    }
    
}

一 Nginx的location语法

1
location [=|~|~*|^~] /uri/ { … }
  • =         严格匹配。如果请求匹配这个location,那么将停止搜索并立即处理此请求
  • ~         区分大小写匹配(可用正则表达式)
  • ~*       不区分大小写匹配(可用正则表达式)
  • !~       区分大小写不匹配
  • !~*     不区分大小写不匹配
  • ^~      如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式

示例1:

背景:

使用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";
        } 
    }
    
}

猜你喜欢

转载自www.cnblogs.com/micro-chen/p/12179817.html