现在的位置: 首页 > 综合 > 正文

Nginx + Tomcat + Openssl 配置https的web服务

2019年07月23日 ⁄ 综合 ⁄ 共 1613字 ⁄ 字号 评论关闭

linux 地址 192.168.221.128


一:系统环境

apache-tomcat-8.0.15

jdk1.8.0_05

Tengine version: Tengine/2.1.0 (nginx/1.6.2)

二:使用openssl生成证书

1:首先要生成服务器端的私钥(key文件):
openssl genrsa -des3 -out server.key 1024

2:运行时会提示输入密码,此密码用于加密key文件(参数des3便是指加密算法,当然也可以选用其他你认为安全的算法.),以后每当需读取此文件(通过openssl提供的命令或API)都需输入口令.如果觉得不方便,也可以去除这个口令,但一定要采取其他的保护措施!
去除key文件口令的命令:
openssl rsa -in server.key -out server.key

openssl req -new -key server.key -out server.csr

3:生成证书

openssl req -new -x509 -nodes -out server.crt -keyout server.key


三:配置tomcat

编辑 conf/server.xml文件,添加

1:把原来的

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

改成

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" proxyPort="443" />

2:添加

<Host name="love.tangshiyi.com" autoDeploy="false">
        <Valve className="org.apache.catalina.valves.RemoteIpValve"
                  remoteIpHeader="x-forwarded-for"
                  remoteIpProxiesHeader="x-forwarded-by"
                  protocolHeader="x-forwarded-proto"
            />
        <Context path="" docBase="/home/hadoop/vs/tangshiyi" />
</Host>

四:配置Nginx

在http里面加入

upstream tomcat {
        server 127.0.0.1:8080;
}

server {
        listen       443 ssl;
        server_name  love.tangshiyi.com ;

        ssl_certificate     /home/hadoop/key/server.crt;
        ssl_certificate_key  /home/hadoop/key/server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-Proto https;
                proxy_pass http://tomcat;
        }

    }

注意,这里面的server_name,必须和tomcat那里的Host保持一致

proxy_set_header        Host $http_host; 这一句必须有,否则代理不了

四:配置客户端Hosts文件

在hosts里面加入

192.168.221.128 love.tangshiyi.com

在浏览器中,输入https://love.tangshiyi.com即可访问


抱歉!评论已关闭.