個人網站配置HTTPS

讓NGINX支持SSL

需要編譯時支持ssl,可以sbin/nginx -V 來查看confiture參數,如果當時沒有支持,那麼需要重新編譯安裝。 編譯參數前面已經有一篇文章了。nginx編譯參數,也不用全加,用--with-http_ssl_module 就可以了。

生成證書

主要參考這個letsencrypty,可以生成免費證書。 生成方式也很簡單,讀上面的文章基本就能明白。ubuntu+nginx.

大致步驟如下:

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto
./path/to/certbot-auto certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.is -d m.thing.is

執行完之後系統中會生成這些文件。系統中生成的文件

This command will obtain a single cert for example.com, www.example.com, thing.is, and m.thing.is; it will place files below /var/www/example to prove control of the first two domains, and under /var/www/thing for the second pair.

Automating renewal

上面生成的證書,有效期好像是1個月,所以需要到期自己重新renewal一下。方法如下:

./path/to/certbot-auto renew --dry-run
./path/to/certbot-auto renew --quiet --no-self-upgrade

配置NGINX

配置就不多說了.首先需要配置2個server,監聽2個端口。這樣可以強制將80端口的請求重定向至443端口。https本身監聽的是443端口。最主要的是ssl中間那3行。將步驟2中生成的對應key寫在nginx的配置文件裏。注意改成你的具體路徑。

server {

    listen       443;
    server_name  blog.nofile.cc;

    ssl                  on;
    ssl_certificate      /xxxx/letsencrypt/live/yoursite/fullchain.pem;
    ssl_certificate_key  /xxxx/letsencrypt/live/yoursite/privkey.pem;

     location / {
        #這個地方指定被訪問的文件夾位置
        root   /your/webroot/;
        index  index.html;
    }
}

server {
    listen      80;
    server_name blog.nofile.cc;
    return 301 https://blog.nofile.cc$request_uri;  
}

配置好之後,重啓nginx,應該就可以看到綠色的鎖了。
我的個人網站https://blog.nofile.cc,歡迎來踩。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章