2

# 我的个人博客,希望大家帮忙踩一下

Nginx 证书配置 强制使用https

  1. 先获取证书,证书可以有很多种, Let's Encrypt 或者阿里云都可以
  2. 这里我用Let's Encrypt 通配符证书 以centos 7为例

获取证书

 #获取证书生成工具
wget https://dl.eff.org/certbot-auto     
#安装软件
chmod a+x certbot-auto                      
#获取证书 -d 可以有多个 *.test.com 只能匹配 子域名和www开头,所以在加一个主域名不带www的 如(test.com)
./certbot-auto certonly  -d *.test.com -d test.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory   

**上面的命令会给出三个的提示仔细看:
是否同意 Let's Encrypt 协议要求
询问是否对域名和机器(IP)进行绑定
输入邮箱,给你发送一封验证邮件
确认同意才能继续。**

Please deploy a DNS TXT record under the name
_acme-challenge.test.com with the following value:

2_8KBE_jXH8nYZ2unEViIbW52LhIqxkg6i9mcwsRvhQ

Before continuing, verify the record is deployed.

Press Enter to Continue
Waiting for verification...
Cleaning up challenges
意思是:求给 _acme-challenge.test.com 配置一条 TXT 记录,在没有确认 TXT 记录生效之前不要回车执行。
这个需要自己配置
配置成功了以后,等30秒在按回车,(保证解析的正确)
确认生效后,回车执行
恭喜您,证书申请成功,证书和密钥保存在下列目录:
/etc/letsencrypt/archive/test.com

下面配置Nginx Nginx 安装我就不教了

打开nginx.conf文件

  
   #  spring boot 项目的  https的配置
    server {
        listen 443 ssl;    # 监听端口
        server_name ifsaid.com www.ifsaid.com;    # 域名配置,可以多个
        
        ssl_certificate /etc/letsencrypt/live/ifsaid.com/fullchain.pem;            # 证书地址
        ssl_certificate_key /etc/letsencrypt/live/ifsaid.com/privkey.pem;       # 证书地址
        
         # 固定写法-------------
        ssl_session_cache   shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers   EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;

       # 这里配置反向代理的项目
        location / {
           
            proxy_pass http://127.0.0.1:8090;     # spring boot 项目的端口号
            
            # 固定写法-------------
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
        }
    }
    
    #   spring boot 项目的 http的配置  强制转发到https端口
    server {
        listen 80;
        server_name ifsaid.com www.ifsaid.com;
        
        #这是老版本的Nginx转发
        #rewrite ^(.*) https://$server_name$1 permanent;
        
        #这是新版本的Nginx转发
        return 301 https://$server_name$request_uri;
        
        # 固定写法-------------
        tcp_nodelay     on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    #  vue 项目的  https的配置
    server {
        listen 443 ssl;
        server_name admin.ifsaid.com;
        
        ssl_certificate /etc/letsencrypt/live/ifsaid.com/fullchain.pem;            #同上  当然也可以知道另一个证书
        ssl_certificate_key /etc/letsencrypt/live/ifsaid.com/privkey.pem;      #同上
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;

        location / {
            root /home/nginx/admin;  # vue.js 打包目录
            
            #固定写法就可以了
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }
    
    #  vue 项目的 http的配置  强制转发到https端口
    server {
        listen 80;
        server_name admin.ifsaid.com;        #除了二级域名可以改变,其他都可以不变
        #rewrite ^(.*) https://$server_name$1 permanent;
        return 301 https://$server_name$request_uri;
        
        tcp_nodelay     on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    # 然后
    Nginx 停止:  
    /usr/local/nginx/sbin/nginx -s quit
    
    启动
    /usr/local/nginx/sbin/nginx    
    

启动在 docker中启动 spring boot

命令

docker run -p 8090:8090 -t springboot/spring-boot-docker

可以看到证书已经生效了

在看子域名的 admin.ifsaid.com

至此 Nginx 域名和二级域名都是https 了 spring boot 和 vue.js 都实现了


旋转的木驴
22 声望2 粉丝