zoukankan      html  css  js  c++  java
  • Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https【转载】

     

    转自

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https - OPEN 开发经验库
    http://www.open-open.com/lib/view/open1404644133495.html

    1          实现原理与工作流程

    原理:

    客户浏览器使用https协议访问 ApacheServer 。ApacheServer将请求转发到后端的Tomcat。ApacheServer与后端的Tomcat通信使用http协议。所以Tomcat不用开启https协议。把ApacheServer配置支持https协议就可以了。证书也在ApacheServer中配置。自始自终Tomcat都不需要修改任何地方。同理换成JBoss也一样,JBoss不需要修改。

    更进一步,让客户浏览器访问http协议时,自动将http协议转成https协议就更加人性化了。这个技术也是在ApacheServer中配置完成。

    工作流程:

    1.       搭建ApacheServer服务器,支持https协议;(基础工作)

    2.       搭建Tomcat服务器,配置基本的访问页面;(基础工作,整个功能不需要配置Tomcat)

    3.       配置ApacheServer服务器,实现在https协议基础上将URL请求转发给Tomcat服务器;(关键)

    4.       配置ApacheServer,实现访问http协议自动转成https协议。(关键)

    2          搭建ApacheServer服务器

    2.1       使用yum 安装apache

    Apache版本

    # httpd -v

    Server version: Apache/2.2.15 (Unix)

    Server built:   Aug 13 2013 17:29:28

    2.2       检测Apache是否支持加密https协议

    ll /etc/httpd/modules/

    查看目录中是否有mod_ssl模块,若没有使用yum install mod_ssl安装,安装以后变化:

    l  增加了/etc/httpd/modules/mod_ssl.so文件

    l  增加了/etc/httpd/conf.d/ssl.conf配置文件

    2.3       实现URl转发技术需要的模块

    我们需要使用Apache的ProxyPass来转发URL后端,需要下面两个模块

    mod_proxy.so

    mod_proxy_http.so

    我们还需要实现客户端浏览器访问http自动转成https协议,需要下面的模块

    mod_rewrite.so

    2.4       安装openssl提供ssl加密协议

    使用yum 安装openssl

    # yum install openssl

    openssl version

    OpenSSL 1.0.1e-fips 11 Feb 2013

    注意:我们测试环境,不需要创建CA证书,生产环境中需要上传CA证书,并在ssl.conf文件中指定证书路径。

    3          搭建Tomcat服务器

    # tar zxf apache-tomcat-8.0.9.tar.gz

    # mv apache-tomcat-8.0.9 /opt/tomcat

    开启tomcat服务,测试使用tomcat默认页面就可以

    # bin/startup.sh

    访问192.168.1.10:8080出现tomcat默认页面,如下图:

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https

    Tomcat服务器搭建成功

    1          配置Apache在http协议基础上使用ProxyPass转发URL到Tomcat(主要测试基本的ProxyPass的转发功能)

    1)编辑/etc/httpd/conf/httpd.conf

    LoadModule proxy_module modules/mod_proxy.so

    LoadModule proxy_http_module modules/mod_proxy_http.so

    #具备以上2行是说明apache具备将URL转发给Tomcat的能力

    # 测试页面add by sxr

    ProxyPass /examples http://192.168.1.10:8080/examples/

    ProxyPassReverse /examples http://192.168.1.10:8080/examples/

    测试,访问192.168.1.10/examples后,自动跳转到了后端Tomcat的192.168.1.10:8080/examples页面。

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https

    Tomcat提供的192.168.1.10:8080/examples页面

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https

    1          配置Apache在https协议中实现ProxyPass转发URL到Tomcat

    实现原理:客户浏览器访问Apache的htts协议,在Aapache的ssl.conf配置文件中配置ProxyPass转发,将请求转发给后端的Tomcat服务器,这样就实现了在https协议的基础上Apache将URL转发给Tomcat。

    注意:此时,客户端浏览器必须使用https://192.168.1.10/docs访问才能实现URL转发。

    1)编辑/etc/httpd/conf/httpd.conf

    Include conf.d/*.conf

    #加载其他的配置文件,主要是为了使用ssl.conf

    保存。

    2)编辑 /etc/httpd/conf.d/ssl.conf,支持https协议

    LoadModule ssl_module modules/mod_ssl.so

    Listen 443

    ##

    ## SSL Virtual Host Context

    ##

    <VirtualHost _default_:443>

    ErrorLog logs/ssl_error_log

    TransferLog logs/ssl_access_log

    LogLevel warn

    SSLEngine on

    SSLProtocol all -SSLv2

    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW

    SSLCertificateFile /etc/pki/tls/certs/localhost.crt

    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

    <Files ~ ".(cgi|shtml|phtml|php3?)$">

        SSLOptions +StdEnvVars

    </Files>

    <Directory "/var/www/cgi-bin">

        SSLOptions +StdEnvVars

    </Directory>

    SetEnvIf User-Agent ".*MSIE.*"

             nokeepalive ssl-unclean-shutdown

             downgrade-1.0 force-response-1.0

    CustomLog logs/ssl_request_log

              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"

    # 在https协议中实现ProxyPass转发URL,实际就是在ssl.conf中添加ProxyPass语句

    ProxyPass /test http://192.168.1.10:8080/examples

    ProxyPassReverse /test http://192.168.1.10:8080/examples

    ProxyPass /docs http://192.168.1.10:8080/docs    注意,末尾没有/符号,http://192.168.1.10:8080/docs/是错误的

    ProxyPassReverse /docs http://192.168.1.10:8080/docs

    注意:转发到Tomcat的URL中末尾不能加/符号。如果写成ProxyPassReverse /docs http://192.168.1.10:8080/docs/ ,访问的结果如下图:

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https

    写成ProxyPassReverse /docs http://192.168.1.10:8080/docs,访问的结果如下图:

    Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https

    2          配置Apache,实现http自动转换成https协议

    上面,我们已经完成了在https协议上实现将URL转发给Tomcat。接下来,我们只要配置Apache,实现http自动转成https协议,就能够让客户端浏览器输入普通地址自动跳转到加密的https的页面,并且页面还是后端Tomcat服务提供的功能。

    编辑/etc/httpd/conf/httpd.conf配置文件,下面语句实现将整个Apache站点都自动转成https协议

    # add by sxr

    RewriteEngine on

    RewriteCond %{SERVER_PORT} 80

    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

    RewriteLog /var/log/httpd/rewrite.log

    RewriteLogLevel 10

    保存,重启Apache服务生效

    # /etc/init.d/httpd restart

    Stopping httpd:                                            [  OK  ]

    Starting httpd:                                            [  OK  ] 

  • 相关阅读:
    Docker配置samba共享
    阿里云SSL 免费证书申请方法
    linux centos7 关闭防火墙的方法
    PHP 面向对象的特性 抽象类和接口
    详解mixphp的依赖注入控制反转
    swoole mixphp swoolefor热更新使用方法
    nginx ab并发测试 apr_socket_recv: Connection refused (111
    PHP golang java 并发测试
    php.ini 添加的模块没更新的解决方法
    关于golang的未来的道路
  • 原文地址:https://www.cnblogs.com/paul8339/p/5633475.html
Copyright © 2011-2022 走看看