未分类
nginx通过传参做动态代理
nginx通过传参做动态代理:可以访问http://172.20.47.38/console/?host=172.20.47.11, 来直接代理到gotty的172.20.47.11:50000 的websocket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
map $http_cookie $c_cookie { default ""; ~*host=([^\;]*) $1; } map $args $r_host { default $c_cookie; ~*host=(.*) $1; } #------------- server { listen 80; location ~ /console/ { if ($uri !~ console/ws){ proxy_pass http://$r_host:50000; rewrite /console/(.*)$ /$1 break; add_header Set-Cookie "host=$r_host"; } if ($uri ~ console/ws){ #proxy_pass http://$c_cookie:50000/ws; rewrite /(.*)$ /consolews last; } #proxy_http_version 1.1; #proxy_set_header Upgrade $http_upgrade; #proxy_set_header Connection $connection_upgrade; #proxy_set_header Host $http_host; #proxy_set_header X-Real-IP $remote_addr; } location ~ /consolews { proxy_pass http://$c_cookie:50000/ws; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } } #------------ |
可以将参数进行base64加密, 如:
1 2 3 |
[root@dciuap2 nginx]# printf 172.20.47.38|base64|tr -d \\n MTcyLjIwLjQ3LjM4 #注意base64加密会自动在字符后面添加空格, 要把空格删除 |
然后通过访问http://172.20.47.38/console/?host=MTcyLjIwLjQ3LjM4 来访问gotty: 参考: http://man.hubwiz.com/docset/OpenResty.docset/Contents/Resources/Documents/set-misc-nginx-module.html#set_unescape_uri nginx要添加set-misc-nginx-module模块, 具体安装方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
wget 'http://nginx.org/download/nginx-1.11.2.tar.gz' tar -xzvf nginx-1.11.2.tar.gz cd nginx-1.11.2/ # Here we assume you would install you nginx under /opt/nginx/. ./configure --prefix=/opt/nginx \ --with-http_ssl_module \ --add-module=/path/to/ngx_devel_kit \ --add-module=/path/to/set-misc-nginx-module make -j2 make install |
配置文件如下, 逻辑为: 第一次访问时, $uri是/console/ 则进行set_decode_base64解码,设置cookie 然后再访问几个js文件, 匹配.js, js再调用ws
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
map $http_cookie $c_cookie { default ""; ~*host=([^\;]*) $1; } map $args $r_host { default $c_cookie; ~*host=(.*) $1; } server { listen 80; server_name 172.20.47.38; location ~ /console/ { if ($uri = /console/ ){ set $hostip "$r_host"; set_decode_base64 $hostip; proxy_pass http://$hostip:50000; rewrite /console/(.*)$ /$1 break; add_header Set-Cookie "host=$hostip"; } if ($uri ~ .js){ proxy_pass http://$c_cookie:50000; rewrite /console/(.*)$ /$1 break; } if ($uri ~ console/ws){ #proxy_pass http://$c_cookie:50000/ws; rewrite /(.*)$ /consolews last; } } location ~ /consolews { proxy_pass http://$c_cookie:50000/ws; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } } |
—- 旧的请求根目录版:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
map $http_upgrade $connection_upgrade { default upgrade; '' close; } map $args $r_host { default localhost; ~*host=(.*) $1; } map $http_cookie $c_cookie { default ""; ~*host=(.*) $1; } server { listen 8000; location / { proxy_pass http://$r_host:50000/; add_header Set-Cookie "host=$r_host"; } location /ws { proxy_pass http://$c_cookie:50000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } location ~ \.(gif|jpg|png|js|css)$ { proxy_pass http://$c_cookie:50000; } } |