nginx配置https

使用linux實用工具certbot來生成https證書php

這個工具是生成Let's Encrypt證書,
Let's Encrypt數字證書認證機構,Let’s Encrypt 是由互聯網安全研究小組(ISRG,一個公益組織)提供的服務
提供免費的SSL/TLS證書html

2015年12月3日,該服務進入公測階段,正式面向公衆。
2016年4月12日,該項目正式離開Beta階段。
到2016年9月9日,Let’s Encrypt 已經發放 1000 萬張證書。
所以對於大部分中小型網站來講,是一個值得考慮的選擇。python

 

https配置的步驟linux

1打開 https://certbot.eff.org/ 選擇對應操做系統與 Web 服務器nginx

這裏我選擇nginx服務器,CentOS7服務器上

2執行命令,並根據須要修改相應域名參數。web

certbot要經過yum安裝,certbot被打包到epel源中,
    因此安裝啓動epel庫,安裝epel源查看連接
    https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F
    啓動epel源,可使用手動本身啓動epel,也能夠藉助yum-config-manager命令來啓動

    安裝yum-config-manager
    yum -y install yum-utils
    啓動epel
    yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

3安裝certbotcentos

sudo yum install certbot python2-certbot-nginx

 

獲取證書的兩種方式:身份驗證器和安裝程序安全

使用webRoot插件進行安裝,這個要求你的服務器80端口可以正常被訪問到(這個域名是屬於你的)服務器

webRoot插件經過certonly和--webroot(或者-w)在命令行上執行命令

certbot certonly -w /var/www/example -d www.example.com

certbot certonly -w 能夠被http訪問到的webroot目錄 -d 要配置https的域名名稱

上面的 /var/www/example表示的是在nginx配置文件中root根節點所指向的根路徑dom

webroot插件的工做原理是爲每一個請求的域建立一個臨時文件${webroot-path}/.well-known/acme-challenge。
而後,Let的加密驗證服務器發出HTTP請求,以驗證每一個請求的域的DNS是否解析爲運行certbot的服務器。

訪問請求以下

66.133.109.36 - - [05/Jan/2016:20:11:24 -0500] "GET /.well-known/acme-challenge/HGr8U1IeTW4kY_Z6UIyaakzOkyQgPr_7ArlLgtZE8SX HTTP/1.1" 200 87 "-" "Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)"

因此咱們服務器須要放通.well-known/acme-challenge這個訪問路徑

例如,

server
    {
        listen 80;
        server_name www.example.com; 
        index index.html ;
        root  /var/www/example;
    
        。。。
    
        location ~ /.well-known {
            allow all;
        }
    }

具體的http配置文件

server
    {
        listen 80;
        server_name www.example.com; 
        index index.html ;
        root  /var/www/www.example.com;


        location / {
            proxy_redirect off;
            proxy_pass  http://localhost:8080;
            proxy_set_header  Host $host;
            proxy_set_header   X-real-ip $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page   404   /404.html;

        location /nginx_status
        {
            #stub_status on;
            #access_log   off;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }


        access_log /data/log/nginx//var/www/www.example.com/-access.log;
        error_log  /data/log/nginx//var/www/www.example.com/-error.log;
}

 

執行完命令後,https證書就會生成在/etc/letsencrypt/live目錄下

certbot certonly -w /var/www/example -d www.example.com

好比上面的命令會生成證書/etc/letsencrypt/live/www.example.com/fullchain.pem

生成證書密鑰文件/etc/letsencrypt/live/www.example.com/privkey.pem

 

而後咱們只須要爲該域名加上https配置,咱們nginx就配置完成https

https對應443端口

具體https配置文件

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name www.example.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /var/www/www.example.com/;
        
        ssl on;
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
        
     location / {
            proxy_redirect off;
            proxy_pass  http://localhost:8080;
            proxy_set_header  Host $host;
            proxy_set_header   X-real-ip $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page   404   /404.html;

        include enable-php-pathinfo.conf;

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log /data/log/nginx/www.example.com-ssl-access.log;
        error_log  /data/log/nginx/www.example.com-ssl-error.logs;    
}

查看生產的證書
tree /etc/letsencrypt/live/

證書續簽
Let’s Encrypt 生成的免費證書爲3個月時間,可是咱們能夠無限次續簽證書
certbot renew

使用定時器來自動從新生成證書
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew

 

centos6使用
1獲取certbot客戶端
    wget https://dl.eff.org/certbot-auto
    chmod a+x certbot-auto
    
2中止nginx
    service nginx stop
3生成證書
    ./certbot-auto certonly --standalone --email `你的郵箱地址` -d `你的域名地址`
    
當前網站有多個域名時需在後面增長,例如
./certbot-auto certonly --standalone --email `你的郵箱地址` -d `你的域名1` -d `你的域名2`
相關文章
相關標籤/搜索