一聚教程网:一个值得你收藏的教程网站

热门教程

Tomcat配置SSL nginx+tomcat配置https实例及优化过程

时间:2022-06-30 18:30:46 编辑:袖梨 来源:一聚教程网

Tomcat配置SSL,使用openssl制作证书

制作证书以及Tomcat配置

    搭建openssl环境,下载openssl并设置环境变量方便命令行的使用;
    修改openssl配置文件,设置dir目录,如设置dir=e:/temp/openssl_ca,然后根据配置文件分别建立子目录:certs、crl、newcerts、private分别用来存放签发的证书、吊销的证书、证书申请、私钥;

        cd /d e:tempopenssl_ca
        mkdir certs
        mkdir crl
        mkdir newcerts
        mkdir private


    根据配置文件,需要建立索引文件、序列号文件、随机数文件:

        cd /d e:tempopenssl_ca
        echo 0 > index.txt
        echo 01 > serial
        openssl rand -out private/.rand 1000


    构建根证书密钥

        openssl genrsa -des3 -out private/ca.key.pem 2048

    密码是:oseye15b
    构建根证书申请

        openssl req -new -key private/ca.key.pem -out newcerts/ca.csr -subj "/C=CN/ST=GD/L=SZ/O=oseye/OU=oseye/CN=*.oseye.net"

    签发根证书

        openssl x509 -req -days 100000 -sha1 -signkey private/ca.key.pem -in newcerts/ca.csr -out certs/ca.cer

    构建服务器密钥

        openssl genrsa -des3 -out private/mobile.key.pem 2048

    密码是:oseye15bmobile
    构建服务器证书申请

        openssl req -new -key private/mobile.key.pem -out newcerts/mobile.csr -subj "/C=CN/ST=GD/L=SZ/O=oseye/OU=oseye15b/CN=mobile.oseye.net"

    签发服务器证书

        openssl x509 -req -days 100000 -sha1 -CA certs/ca.cer -CAkey private/ca.key.pem -CAserial ca.srl -CAcreateserial -in newcerts/mobile.csr -out certs/mobile.cer

    此处为了安全可言采用sha512来签名,更多签名算法参见这里。

        openssl x509 -req -days 100000 -sha512 -CA certs/ca.cer -CAkey private/ca.key.pem -CAserial ca.srl -CAcreateserial -in newcerts/mobile.csr -out certs/mobile.cer

    转换成java可用的格式

        openssl pkcs12 -export -clcerts -inkey private/mobile.key.pem -in certs/mobile.cer -out certs/mobile.p12

    Export密码:oseye15bp12
    使用keytool查看

        keytool -list -keystore certs/mobile.p12 -storetype pkcs12 -v -storepass oseye15bp12

    修改tomcat的配置文件server.xml

        ps" secure="true"
                       clientAuth="false" sslProtocol="TLS" keystoreFile="/var/oseye/certs/mobile.p12" keystorePass="oseye15bp12" keystoreType="PKCS12" />


    通过./catalina.sh configtest来检测配置结果。
    站点http自动到https的配置
    在web.xml中

           
                SSL 
                /* 
            
         
             
                CONFIDENTIAL 
             
        


Tomcat在debian中的一点点PS

    安装命令:

        apt-get install tomcat7

    tomcat7配置文件:/etc/tomcat7/
    tomcat7日志文件:/var/log/tomcat7
    tomcat7 web默认webapps:/var/lib/tomcat7/webapps
    修改使用sun jdk,需要在/etc/default/tomcat7添加修改变量:JAVA_HOME=/usr/oseye/lib/jvm/jdk1.7.0_67


    
nginx + tomcat 配置https实例

上面讲了Tomcat的https配置,现在打算用nginx作负载前端,后端用Tomcat,使用Nginx均衡且负责https的协议处理。Nginx的密钥不能使用密码,因此必须导出无密码的密钥。

为了方便制作证书,我写了bat文件,最后需要的密钥和证书放在了upload目录里,bat代码如下:

    @echo off
    echo 准备工作...
    set current=%cd%
    echo 清理...
    rd /s /q %current%ssl
    mkdir %current%ssl
    cd /d %current%ssl
     
    echo 创建辅助目录...
    mkdir certs
    mkdir crl
    mkdir newcerts
    mkdir private
    mkdir upload
     
    echo 创建辅助文件...
    echo 0 > index.txt
    echo 01 > serial
    openssl rand -out private/.rand 1000
     
    echo 构建根证书密钥...
    openssl genrsa -des3 -passout pass:oseye15b -out private/ca.key.pem 2048
     
    echo 构建根证书申请...
    openssl req -passin pass:oseye15b -new -key private/ca.key.pem -out newcerts/ca.csr -subj "/C=CN/ST=GD/L=SZ/O=oseye/OU=oseye/CN=*.oseye.net"
     
    echo 根证书自签发...
    openssl x509  -passin pass:oseye15b -req -days 100000 -sha1 -signkey private/ca.key.pem -in newcerts/ca.csr -out certs/ca.crt
     
     
    echo 构建无密码mobile证书密钥...
    openssl genrsa -des3 -passout pass:oseye15bmobile -out private/mobile.key.pem 2048
     
    echo 导出mobile证书密钥...
    openssl rsa -passin pass:oseye15bmobile -in private/mobile.key.pem -out upload/mobile_nopwd.key
     
    echo 构建mobile证书申请...
    openssl req -passin pass:oseye15bmobile -new -key private/mobile.key.pem -out newcerts/mobile.csr -subj "/C=CN/ST=GD/L=SZ/O=oseye/OU=oseye15b/CN=mobile.oseye.net"
     
    echo 签发服务器证书
    openssl x509 -passin pass:oseye15b -req -days 100000 -sha1 -CA certs/ca.crt -CAkey private/ca.key.pem -CAserial ca.srl -CAcreateserial -in newcerts/mobile.csr -out upload/mobile.crt
    cd ..
     
    echo.
    echo success!
    echo 请上传 %current%sslupload 目录中的文件上传


Nginx的配置如下:

    upstream mobile_https_proxy{
            server 168.9.2.45:8080 weight=4 max_fails=2 fail_timeout=30s;
    }
     
     
    server {
           listen 443;
           server_name localhost;
     
           root html;
           index index.html index.htm;
     
           ssl on;
           ssl_certificate /home/oseyei/certs/mobile/mobile.crt;
           ssl_certificate_key /home/oseyei/certs/mobile/mobile_nopwd.key;
     
           ssl_session_timeout 5m;
     
           ssl_protocols SSLv3 TLSv1;
           ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
           ssl_prefer_server_ciphers on;
     
           location / {
                    proxy_pass http://mobile_https_proxy;
                    proxy_set_header X-Forwarded-For $remote_addr;
     
           }
    }

     

其中“168.9.2.45:8080”是Tomcat部署的website.

关于nginx反向代理和负载均衡可参考这里。

如何让浏览器不在显示证书不信任呢,只要把ca.crt作为根证书导入系统即可。



Nginx+Tomcat的https优化过程

nginx + tomcat 配置https实例成功之后,经过压力测试,惨不忍睹,含有业务的每秒并发只有150左右,于是走上了优化之路。

回过头总结,从以下方面开始确认和优化:

    后端Tomcat的处理能力;
    后端Tomcat含业务的处理能力;
    前端Nginx的接入能力;

后端Tomcat的处理能力

由于使用的是Spring MVC框架,因此使用Spring MVC做了一个最简单的Test,压力测试发现每秒的并发只有400左右,于是优化Tomcat的配置:

 

   
    
     
    
    javascript,text/css,text/plain"
        disableUploadTimeout="true"
    />


当然这其中要处理掉 Tomcat报异常:Too many open files 的解决之路 的异常,而且启用了Tomcat的NIO模式,经过这个设置后并发提高到800左右,但仍不理想!很想启用APR模式,但一直没能编译成功!

这样的速度让我对Spring MVC失去了信心,因为如果直接使用JSP的话并发4000左右,网上做了Struts和Spring MVC的比较,都说现在的Spring MVC性能并不比Struts差,所以肯定是自己用得不当。最后逐步定位,定位到了日志系统,因为上面使用简单的测试,为了调试方便还是启用了Log4J,在web.xml的配置如

    
        log4jConfigLocation
        /WEB-INF/log4j.properties
    
         
        log4jRefreshInterval     
        6000     
      
    
        org.springframework.web.util.Log4jConfigListener
    


把配置注释掉,并发就能达到3500左右,一度让我怀疑Spring的这个listener是否有问题,打算自己写listener,但还是觉得Spring应该不会这么弱,最后定位到了log4j的配置,原来为了调试方便,Log4j的配置文件其中有

    log4j.logger.org.springframework=debug, SysFile

把springframework的日志关闭,即

    log4j.logger.org.springframework=off

则并发数就上来了,可见一个小动作可能酿成不可预估的后果。


后端Tomcat含业务的处理能力

经过上面的优化,对含有业务的应用做压力测试,悲惨的是并发只有200左右!这种情况和业务的优化有关了,属于另一个话题,暂且按下不表!


前端Nginx的接入能力

只测试Nginx的https接入能力,并发在450左右,关键的配置

    worker_processes 2;
    worker_cpu_affinity 01 10;
    worker_rlimit_nofile 65535;
     
    events {
        worker_connections 65535;
        multi_accept off;
        use epoll;
    }
     
    server {
        #........
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        #........
    }


后来参考 对 Nginx SSL 的性能进行调整 改变了加密算法

    ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;

性能又有所提升,貌似是800左右,记不得了!

但是每次压力时我都发现cpu利用率都能达到90%-96%,应该是cpu处理不来,于是把cpu由2核升级到8核,并把nginx从1.2升级到了1.8,优化到此,并发能达到每秒1500左右!由于现在用的是虚拟机,如果采用物理机,并发应该更强一些。

热门栏目