前后端分离项目域名配置Https

2019-01-10  本文已影响0人  linjiajiam

在之前的文章中,介绍了前后端分离项目nginx配置域名访问,这篇文章将介绍如何通过Certbot获取免费https证书,为网站配置https访问。

1.安装certbot
wget https://dl.eff.org/certbot-auto
image.png
chmod a+x certbot-auto
2.获取证书
systemctl stop nginx
./certbot-auto certonly --standalone -d  www.demoProject.com   # www.demoProject.com为你想要配置https的域名
ls /etc/letsencrypt/live/
3.配置前端项目https证书
server {

    listen       80;
    server_name  www.demoProject.com;    #该域名为阿里云绑定服务器的域名

    location / {
        proxy_set_header Host $host;
        root /root/server/vue/demo;             #项目路径
        index /index.html;                        
        try_files $uri $uri/ /index.html;        #匹配不到任何静态资源,跳到同一个index.html
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
server {

    listen 443 ssl;
 
    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.demoProject.com/fullchain.pem;   #注意域名填写正确
    ssl_certificate_key /etc/letsencrypt/live/www.demoProject.com/privkey.pem; #注意域名填写正确
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;


    listen       80;
    server_name  www.demoProject.com;

    location / {
        client_max_body_size 100M;
        proxy_set_header Host $host;
        root /root/server/vue/vbp/;             #项目路径
        index /index.html;                        
        try_files $uri $uri/ /index.html;        #匹配不到任何静态资源,跳到同一个index.html
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
server {

    listen 443 ssl;
    server_name  www.demoProject.com;
 
    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.demoProject.com/fullchain.pem;   #注意域名填写正确
    ssl_certificate_key /etc/letsencrypt/live/www.demoProject.com/privkey.pem; #注意域名填写正确
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;


    location / {
        client_max_body_size 100M;
        proxy_set_header Host $host;
        root /root/server/vue/vbp/;             #项目路径
        index /index.html;                        
        try_files $uri $uri/ /index.html;        #匹配不到任何静态资源,跳到同一个index.html
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}


server {
    listen       80;
    server_name  www.demoProject.com;
    rewrite ^/(.*) https://$server_name$request_uri? permanent;
}
4.配置后端项目https证书
systemctl stop nginx  #停止nginx
./certbot-auto certonly --standalone --email xxxmail@qq.com --agree-tos -d xxx.xxx.com
#email后面填写邮箱,-d后面填写想要增加的域名

成功后会显示如下:


7802645-21703d31487e7e12.png

查看生成的正式域名:

ls /etc/letsencrypt/live/
upstream api.demoProject.com{
    server 192.168.1.110:8090 weight=1;    #此处ip为服务器内网IP,端口号为tomcat端口号
}


server {

    listen       80;
    server_name  api.demoProject.com;

    location / {
        client_max_body_size 100M;
        proxy_set_header Host $host;
        proxy_pass http://api.demoProject.com;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
upstream api.demoProject.com{
    server 192.168.1.110:8090 weight=1;
}


server {

    listen 443 ssl;
 
    ssl on;
    ssl_certificate /etc/letsencrypt/live/api.demoProject.com/fullchain.pem;   #注意域名填写正确
    ssl_certificate_key /etc/letsencrypt/live/api.demoProject.com/privkey.pem; #注意域名填写正确
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;

    listen       80;
    server_name  api.demoProject.com;

    location / {
        client_max_body_size 100M;
        proxy_set_header Host $host;
        proxy_pass api.demoProject.com;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
5.证书自动定时更新
crontab -e    #编辑crontab

加入以下内容

30 2 * * 1 /root/certbot-auto renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx" >> /var/log/le-renew.log 2>&1 &
root/certbot-auto renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

执行后,出现以下内容,则代表成功执行命令了。


image.png
上一篇 下一篇

猜你喜欢

热点阅读