记录一次nginx做https,tomcat也做https,遇到的证书问题如:No subject alternative names present

需求,访问支持https的方式。

 

于是,我装了一个nginx,准备改造,生成证书等文件。下面是遇到的一些坑:

1.nginx配置https模块

 

没有安装openssl的话,要安装
yum install -y openssl openssl-devel

nginx:添加配置http_ssl_module模块 (在解压目录下)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make

这里如果正常,括号内就不用看了。

(

这里要注意,如果提示找不到openssl的地址,请按照提示,设置openssl的源目录即可。  本人由于openssl安装后,一直提示要关联openssl的地址,

但是由于yum安装后,我找不到那个地址,于是,自己去官网下载了openssl,然后对openssl 进行升级,直到输入openssl version得到正确的就行
  如下图:

  如果后面跟着什么奇怪的东西,比如说版本是1.1.0x,源是1.1.12,那就是没安装好,要重新修改源地址。

 )


--可能需要覆盖nginx之前的二进制文件(这里进入的是安装包的目录)
cp objs/nginx /usr/local/nginx/sbin/
-- 如果提示“cp:cannot create regular file `/usr/local/nginx/sbin/nginx’: Text file busy”,系统不允许覆盖,执行
cp -rfp objs/nginx /usr/local/nginx/sbin/nginx

 

 

 

上面的操作执行完后,去nginx的安装后的目录,一般是/usr/local/nginx/sbin下,输入./nginx -V

如果出现的是类似于这样,nginx的https的准备工作就完成了,可以配置了。

 

 

 

2.依然是准备工作,生成证书 (前提是安装好了jdk,并且可以使用keytool)

 1 这一步骤,请自己找一个放证书的目录操作:
 2 
 3 生成证书文件(这里-ext后面如果不写,tomcat用这个证书的时候,调用服务或者接口如果有验证证书,会报错,如:No subject alternative names present,unable to find valid certification path to requested target )
 4 keytool -genkey -alias test -keypass 123456 -keyalg RSA -keysize 1024 -validity 3650 -keystore /home/jks/test.keystore -storepass 123456 -ext SAN=dns:test.abc.com,ip:192.168.x.x
 5 
 6 导出证书
 7 keytool -export -alias test -keystore /home/jks/test.keystore  -storepass 123456 -rfc -file test.cer
 8 
 9 导入信任证书(SSL客户端使用)
10 keytool -importcert -file test.cer -alias test -keyalg client_trusk.jks -storepass 123456 -keypass 123456
11 
12 将.jks文件转为.p12(PKCS12格式证书库) jks文件==keystore文件
13 keytool -importkeystore -srckeystore sgpms.keystore -destkeystore sgpms.p12 -deststoretype PKCS12
14 
15 提取私钥
16 openssl pkcs12 -nocerts -nodes -in sgpms.p12 -out sgpms.key
17 
18 现在,拥有了
19 .cer文件和.key文件,剩下的就是配置Nginx了。

 

以上生成的证书相关文件,用于nginx的https足够了,但是我这项目特殊,进入项目后,会自动跳转一个登录地址,登录成功再返回。导致一直出如下图的错误,后面有同事说,给nginx要dialing的tomcat搞成https可以解决。

 

 

 注意,这里-keystore后面如果你不是在jdk/jre/lib/security/的这个目录下,请填写详细路径。

 

---导入证书到java的cacerts(jre/lib/security/) 口令,默认是changeit
keytool -import -alias tomcat -file test.cer -keystore cacerts -trustcacerts

把你的证书,导入到这里,就不会引发上面的错误了。如果不行,可以换做个证书,试着多这样操作几遍看看。

 

 

3.nginx里的https配置

server {
        listen       29001 ssl;
        server_name  localhost;

        ssl_certificate      /home/jks/test.cer; 
        ssl_certificate_key  /home/jks/test.key;

        ssl_session_cache    shared:SSL:1m; 
        ssl_session_timeout  5m;        
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #ssl_prefer_server_ciphers  on;
        #if ($scheme = http) {
        #  return 301 https://$host$request_uri;
        #}

        location / {
       #没有特殊需求,代理到http路径就可以了  #proxy_pass http://192.168.11.11:9401;         proxy_pass https://192.168.11.54:19443; proxy_set_header x-forwarded-for $remote_addr; proxy_set_header Host $host:$server_port; client_max_body_size 1024m; #下面是让http升级为https #add_header Content-Security-Policy upgrade-insecure-requests; add_header Cache-Control no-cache; }


   #对使用的一些接口进行代理,如果有使用websokets,需要升级协议,让以wsss来访问 

    location /robot {
        proxy_pass https://192.168.2.1:187/robot;
        client_max_body_size 1024m;
        add_header Cache-Control no-cache;
        proxy_set_header Upgrade $http_upgrade; #升级协议头
        proxy_set_header Connection upgrade;
      }



}

 

4.可能还有的tomcat配置

<Connector port="19443" protocol="HTTP/1.1" SSLEnabled="true"
    maxThreads="150" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS"
    keystoreFile="/home/jks/test.keystore"
    keystorePass="123456"    
    />

 

上面所有都成功的情况下,应该就正常了。

以上所有问题,花了将近一周时间。

 

posted @ 2022-05-25 17:29  不加班不熬夜的男子  阅读(517)  评论(0编辑  收藏  举报