nginx + tomcat + https 配置

本文详细介绍了如何在Nginx和Tomcat服务器上配置HTTPS,包括生成私钥、证书请求文件、自签名证书以及配置服务器启用SSL/TLS的过程。适用于希望提高网站安全性,进行HTTPS加密传输的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:nginx 配置https

(1)#生成私钥

#openssl genrsa -des3 -out server.key 1024

(2)#生成证书请求文件

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

输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

(3)在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

 #cp server.key server.key.org

 # openssl rsa -in server.key.org -out server.key

(4)最后标记证书使用上述私钥和CSR:

#openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

-new: 生成新证书签署请求
-x509: 专用于CA生成自签证书
-key: 生成请求时用到的私钥文件
-days n:证书的有效期限,单位是day,默认是365天
-out /PATH/TO/SOMECERTFILE: 证书的保存路径

(5)配置nginx

server {

      listen       443;   #指定ssl监听端口

      server_name  www.example.com;

      ssl on;    #开启ssl支持

      ssl_certificate      /etc/nginx/server.pem;    #指定服务器证书路径

      ssl_certificate_key  /etc/nginx/server.key;    #指定私钥证书路径

      ssl_session_timeout  5m;

      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;     #指定SSL服务器端支持的协议版本

      ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;    #指定加密算法

      ssl_prefer_server_ciphers   on;    #在使用SSLv3和TLS协议时指定服务器的加密算法要优先于客户端的加密算法

# 

    location / {

        #配置忽略

      }

}

二:tomcat

  (1)创建秘钥库

     # keytool -genkey -alias tomcat -keyalg RSA -keystore   /home/liuge/cert/mykey.keystore

nter keystore password: 输入秘钥库口令
Re-enter new password: 再次输入新口令
What is your first and last name? 您的姓氏是什么
[Unknown]: Tomcat
What is the name of your organizational unit? 您的单位名称
[Unknown]: Apache
What is the name of your organization? 您的组织名称
[Unknown]: Apache
What is the name of your City or Locality?省份
[Unknown]: Beijing
What is the name of your State or Province?城市
[Unknown]: Beijing
What is the two-letter country code for this unit? 国家代码
 [Unknown]: CN
Is CN=Tomcat, OU=Apache, O=Apache, L=Beijing, ST=Beijing, C=CN correct? 信息是否正确
[no]: y
  
Enter key password for <tomcat>                输入Tomcat的秘钥口令
(RETURN if same as keystore password): 如果和秘钥库口令相同,按回车
 Re-enter new password:

2 配置server.xml文件

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
              maxThreads="150" scheme="https" secure="true" SSLEnabled="true">
    <SSLHostConfig>
      <Certificate certificateKeystoreFile="conf/mykey.keystore"
              certificateKeystorePassword="123456" ##秘钥库口令
              type="RSA" />
    </SSLHostConfig>
</Connector>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值