nginx反向代理服务contextpath的问题解决

nginx反向代理服务contextpath的问题解决文章目录问题描述解决方案 sub filter 方案使用重定向单独域名访问问题描述现在的企业服务 往往不是单体的 同时可能涉及中间件的访问如 dubbo solr mq 等 对于中间件的监控页面访问 如果直接暴露在公网 肯定这安全 需发对中间件访问进行安全加固 方法主要有 1 限制使用 IP 白名单访问 2 安全密码访问 不使用简单密码 对于没有密码的 可以使用 basic 认证 强密码访问 3

问题描述

本文要讲的是(3)使用nginx代理,中间可能遇到的问题。

nginx反向代理,可以配置location代理不同的服务,这里就涉及到context-path。如果代理服务本来就不是通过“http://ip:端口/”访问的,而是通过“http://ip:端口/contextpath”访问的,就不会有问题。如果服务不支持“http://ip:端口/contextpath”访问,就涉及“/”路径的问题。

location /activemq/ { proxy_pass http://192.168.17.19:8161/; } 

如上代码,location activemq经反向代理后,访问的实际是8161端口下面的activemq路径,而8161端口”/activemq”下没有内容,就会返回404。当然如果只是代理一个服务是没有问题的(可以用“/”location做反向代理),代理多个服务就需要多个location,必然出现问题。

解决方案

sub_filter方案

使用nginx在jenkins返回的所有资源链接前面都加上/jenkins字段,这样用户接下来产生的GET请求都可以map到location /jenkins区块。要实现这个功能需要用到nginx的ngx_http_sub_module。参考Example,可以写出类似配置:

location /jenkins { sub_filter '<a href="/' '<a href="/jenkins/'; sub_filter '<img src="/' '<img src="/jenkins/'; # ...其他替换规则... sub_filter_once off; # 查找并替换多次 proxy_pass http://localhost:8002; } 

案例因特殊情况,使用了这种配置(公供参考):

 #mq location /activemq/ { sub_filter '<a href="/' '<a href="/activemq/'; sub_filter '<a title="Manage ActiveMQ broker" href="/admin/' '<a href="/activemq/admin/'; sub_filter '<a title="See some Web demos" href="/demo/' '<a href="/activemq/demo/'; sub_filter '<img src="/' '<img src="/activemq/'; sub_filter '<script src="/' '<script src="/activemq/'; sub_filter "type='text/javascript' src='/" "type='text/javascript' src='/activemq/"; sub_filter '<link href="/' '<link href="/activemq/'; sub_filter 'url(/' 'url(/activemq/'; sub_filter "@import url('/" "@import url('/activemq/"; sub_filter_once off; # 查找并替换多次 proxy_pass http://192.168.7.19:8161/; allow 219.143.147.82;#访问控制 allow 220.160.125.218;#访问控制 allow 36.110.62.178; # allow for sslvpn access allow 36.110.218.132; allow 112.48.19.106; deny all; } #solr location /solr/ { proxy_pass http://192.168.17.18:8983/solr/; allow 219.143.147.82;#访问控制 allow 220.160.125.218;#访问控制 allow 36.110.62.178; # allow for sslvpn access allow 36.110.218.132; allow 112.48.19.106; deny all; } #dubbo-admin location /dubbo-admin/ { proxy_pass http://192.168.17.20:8090/dubbo-admin/; allow 219.143.147.82;#访问控制 allow 220.160.125.218;#访问控制 allow 36.110.62.178; # allow for sslvpn access allow 36.110.218.132; allow 112.48.19.106; deny all; } #dubbo-monitor location /dubbo-monitor/ { sub_filter '<a href="/' '<a href="/dubbo-monitor/'; sub_filter '<img src="/' '<img src="/dubbo-monitor/'; #sub_filter_once off; # 查找并替换多次 proxy_pass http://192.168.17.21:8081/; allow 219.143.147.82;#访问控制 allow 220.160.125.218;#访问控制 allow 36.110.62.178; # allow for sslvpn access allow 36.110.218.132; allow 112.48.19.106; deny all; } 

但是这样做效率很低、且filter遗漏和出错可能性大(比如=>其他2)。

使用重定向

location /activemq/ { return 302 http://www.domain.com:8161/; } 

这样做的前提是开通8002的端口映射,还是涉及端口暴露问题。

单独域名访问

如果条件允许的话,应该是分配一个子域名(子域名都是单独的公网IP),比如activemq.domain.com会更合理和方便。nginx同一80端口,支持多域名监听配置。

配置参考

server { listen 443; server_name activemq.domain.com; add_header X-Frame-Options SAMEORIGIN; #rewrite ^(.*)$ http://${server_name}$1 permanent; ssl on; ssl_certificate /etc/nginx/sslkey/_.XXX.com_bundle.crt; ssl_certificate_key /etc/nginx/sslkey/XXX.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL; ssl_prefer_server_ciphers on; add_header X-Via nginx; #static path location / { proxy_pass http://192.168.17.19:8161/; } } 

臭味相投的朋友们,我在这里:
猿in小站:http://www.yuanin.net
csdn博客:https://blog.csdn.net/jiabeis
简书:https://www.jianshu.com/u/4cb7d664ec4b
微信免费订阅号“猿in”
猿in




参考:https://blog.csdn.net/silvita/article/details/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233537.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python3反爬虫原理与绕过实战 网盘_Python 3反爬虫原理与绕过实战「建议收藏」

    第1章 开发环境配置11.1 操作系统的选择11.1.1 Ubuntu简介11.1.2 VirtualBox的安装21.1.3 安装Ubuntu31.1.4 全屏设置81.1.5 Python设置91.2 练习平台Steamboat101.2.1 安装Docker111.2.2 安装Steamboat121.2.3 Steamboat使用说明141.3 第三…

    2022年4月8日
    232
  • django 渲染_源码论坛

    django 渲染_源码论坛前言渲染模块的原理和解析模块是一样,drf默认的渲染有2种方式,一种是json格式,另一种是模板方式。渲染模块源码入口入口:APIView类中dispatch方法中的:self.response

    2022年7月31日
    4
  • Python的使用方法「建议收藏」

    Python的使用方法「建议收藏」1安装turtlePython2安装命令:Python3安装命令:因为turtle库主要是在Python2中使用的,所以安装的时候可能会提示错误:Command"pythons

    2022年7月6日
    26
  • 【转】分区容错性「建议收藏」

    【转】分区容错性「建议收藏」http://book.51cto.com/art/201203/323908.htm1.6.3  分区容错性最为常见的系统部署方案之一就是在一台巨大的中央服务器上安装一个数据库供其他东西访问。这可以让你的系统具有一致性,但是扩展性又如何呢?分区容错性能让你的系统在部分断网的情况下仍然可以完全正常地运转。要实现完全分区容错,系统就必须在任何情况下都能正常运转,除非完全断网。分区容错性几乎…

    2022年7月25日
    15
  • EL表达式详解

    EL表达式详解一、EL表达式简介EL全名为ExpressionLanguage。EL主要作用:1、获取数据EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域中检索java对象、获取数

    2022年7月2日
    21
  • JMH是什么?

    JMH是什么?一、JMH是什么?JMH是Java性能测试工具,主要是对工程中一些方法进行一些基准测试,支持的时间单位为:nano/micro/milli/macro二、JMH使用案例-代码:1P

    2022年7月4日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号