亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Nginx 配置 https (Let's Encrypt)

標簽:
Git

初衷

由于IOS在极力封杀http请求,所以抽空先把刚刚部署好的Web服务加上https支持。

使用90天免费并且可无限续签的 Let's Encrypt

Let's Encrypt是一个良心的CA,因为普通商业CA的价格对个人来说还是难以接受的。但它提供了90天的免费证书。

获取证书的方式也很简单,因为它提供了完全自动化的解决方案:

## 放置路径mkdir /var/www/letsencrypt
sudo apt-get install certbot
sudo certbot certonly --webroot --agree-tos --no-eff-email --email [email protected] -w /var/www/letsencrypt -d app.airoubo.com

申请ok了。

配置Nginx

创建challenge目录:

sudo mkdir -p /var/www/letsencrypt/.well-known/acme-challenge

创建letsencrypt.conf文件并添加:/etc/nginx/snippets/letsencrypt.conf

location ^~ /.well-known/acme-challenge/ {        default_type "text/plain";        root /var/www/letsencrypt;
}

创建ssl.conf文件并添加:/etc/nginx/snippets/ssl.conf

ssl_session_timeout 1d;ssl_session_cache shared:SSL:50m;ssl_session_tickets off;ssl_protocols TLSv1.2;ssl_ciphers EECDH+AESGCM:EECDH+AES;ssl_ecdh_curve secp384r1;ssl_prefer_server_ciphers on;ssl_stapling on;ssl_stapling_verify on;add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";add_header X-Frame-Options DENY;add_header X-Content-Type-Options nosniff;

修改主配置文件:

# the upstream component nginx needs to connect toupstream django {    server unix:///data/django/rouboApi/rouboapi.scok; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver {    # the port your site will be served on
    listen      80;    # the domain name it will serve for
    server_name app.airoubo.com; # substitute your machine's IP address or FQDN
    include /etc/nginx/snippets/letsencrypt.conf;    charset     utf-8;    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    #location /media  {
    #    alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    #}

    location /static {        alias /data/django/rouboApi/static; # your Django project's static files - amend as required
    }    # Finally, send all non-media requests to the Django server.
    location /roubo {        uwsgi_pass  django;        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}## httpsserver {    # the port your site will be served on
    listen      443 ssl http2;    listen [::]:443 ssl http2;    # the domain name it will serve for
    server_name app.airoubo.com; # substitute your machine's IP address or FQDN
    include /etc/nginx/snippets/letsencrypt.conf;    charset     utf-8;    ssl_certificate /etc/letsencrypt/live/app.airoubo.com/fullchain.pem;    ssl_certificate_key /etc/letsencrypt/live/app.airoubo.com/privkey.pem;    ssl_trusted_certificate /etc/letsencrypt/live/app.airoubo.com/fullchain.pem;    include /etc/nginx/snippets/ssl.conf;    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    #location /media  {
    #    alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    #}

    location /static {        alias /data/django/rouboApi/static; # your Django project's static files - amend as required
    }    # Finally, send all non-media requests to the Django server.
    location /roubo {        uwsgi_pass  django;        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

重启nginx后,就可以使用https访问服务了。

自动续签

虽然有90天的期限,但是支持无限续签。所以我们只要定时续签就可以了。

使用上面的certbot工具,可以看下man certbot,它下面有一个renew参数用于更新证书。因为证书更新之后,我们需要重启nginx服务,刚好,它还有一个--renew-hook的参数,支持renew成功之后hook执行我指定的脚本。

我们在/etc/letsencrypt/renewhook.sh脚本中加入重启nginx的动作:

#!/bin/bashservice nginx restart

在root下增加crontab:

sudo crontab -e

设置每月的1号的8点钟执行更新:

00 8 1 * * certbot renew --noninteractive --renew-hook /etc/letsencrypt/renewhook.sh



作者:萝卜日志
链接:https://www.jianshu.com/p/46b2c2798abe


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消