Nginx https 配置 申請免費SSL證書,搭建https網站

什麼要用HTTPS

Nginx https 配置 申請免費SSL證書,搭建https網站

https

網站沒有使用HTTPS的時候,瀏覽器一般會報不安全,而且在別人訪問這個網站的時候,很有可能會被運營商劫持,然後在網站裡顯示一些莫名其妙的廣告。

有HTTPS的時候,通俗地講所有的數據傳輸都會被加密,你和網站之間的數據交流也就更加安全。

相關簡介

Let’s Encrypt

如果要啟用HTTPS,我們就需要從證書授權機構處獲取一個證書,Let’s Encrypt 就是一個證書授權機構。我們可以從 Let’s Encrypt 獲得網站域名的免費的證書。

獲取Let's Encrypt免費SSL證書

首先,我們需要從 https://github.com/letsencrypt/letsencrypt 上下載 letsencrypt-auto 腳本,使用它能夠很方便地完成域名驗證和SSL證書的獲取。

使用git下載腳本

$ git clone https://github.com/letsencrypt/letsencrypt

進入到腳本所在目錄

$ cd letsencrypt

查看 letsencrypt-auto 工具的用法

$ ./letsencrypt-auto --help

腳本下載好了,來看看怎麼使用吧。letsencrypt-auto工具的用法如下:

root@localhost:~/letsencrypt# ./letsencrypt-auto --help
-------------------------------------------------------------------------------
letsencrypt-auto [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...
Certbot can obtain and install HTTPS/TLS/SSL certificates. By default,
it will attempt to use a webserver both for obtaining and installing the
certificate. The most common SUBCOMMANDS and flags are:
obtain, install, and renew certificates:
(default) run Obtain & install a certificate in your current webserver
certonly Obtain or renew a certificate, but do not install it
renew Renew all previously obtained certificates that are near expiry
-d DOMAINS Comma-separated list of domains to obtain a certificate for
--apache Use the Apache plugin for authentication & installation
--standalone Run a standalone webserver for authentication
--nginx Use the Nginx plugin for authentication & installation
--webroot Place files in a server's webroot folder for authentication
--manual Obtain certificates interactively, or using shell/> -n Run non-interactively
--test-cert Obtain a test certificate from a staging server
--dry-run Test "renew" or "certonly" without saving any certificates to disk
manage certificates:
certificates Display information about certificates you have from Certbot
revoke Revoke a certificate (supply --cert-path)
delete Delete a certificate
manage your account with Let's Encrypt:
register Create a Let's Encrypt ACME account
--agree-tos Agree to the ACME server's Subscriber Agreement
-m EMAIL Email address for important account notifications
More detailed help:
-h, --help [TOPIC] print this message, or detailed help on a topic;
the available TOPICS are:
all, automation, commands, paths, security, testing, or any of the
subcommands or plugins (certonly, renew, install, register, nginx,
apache, standalone, webroot, etc.)

這裡只對幾個重要的命令參數進行說明:

run:獲取並安裝證書到當前的Web服務器

certonly:獲取或續期證書,但是不安裝

renew:在證書快過期時,續期之前獲取的所有證書

-d DOMAINS:一個證書支持多個域名,用逗號分隔

--apache:使用 Apache 插件來認證和安裝證書

--standalone:運行獨立的 web server 來驗證

--nginx:使用 Nginx 插件來認證和安裝證書

--webroot:如果目標服務器已經有 web server 運行且不能關閉,可以通過往服務器的網站根目錄放置文件的方式來驗證

--manual:通過交互式方式,或 Shell 腳本手動獲取證書

關於域名驗證和證書的獲取安裝,上面提到了5種方式:--apache, --standalone, --nginx, --webroot 和 --manual,請根據實際情況選擇其一。筆者選擇採用 --standalone 方式進行域名驗證和證書獲取。

獲取證書

$ ./letsencrypt-auto certonly --standalone -d musicfind.fun

注意將上面的郵箱和域名替換成自己的。上面命令中的 certonly 表示只獲取證書,不安裝;

-d ,表示將要獲取的SSL證書綁定一個域名,可以多個-d 綁定。

上面的命令在執行過程中,會有兩次確認。命令執行完成後,如果看到提示信息"Congratulations! Your certificate and chain..."就說明證書創建成功了.

Let's Encrypt 證書續期

出於安全原因,Let's Encrypt 頒發的 SSL 證書有效期為90天,我們可以通過自動續期來解決。如果到期沒有更新證書,Let's Encrypt 會向申請證書時提交的email發送提醒郵件。

進入到 letsencrypt-auto 腳本所在目錄,執行下面的命令即可完成 SSL 證書的續期。

./letsencrypt-auto renew

默認情況下,在證書即將到期之前才能執行續期操作,否則會提示“Cert not yet due for renewal”,即證書尚未到期。如果需要強制執行續期操作,可以加上參數 --force-renew ,命令如下:

./letsencrypt-auto renew --force-renew

以下是筆者強制執行證書續期的操作結果:

root@localhost:~/letsencrypt# ./letsencrypt-auto renew --force-renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/musicfind.fun.conf
-------------------------------------------------------------------------------
Plugins selected: Authenticator standalone, Installer None
Renewing an existing certificate
Performing the following challenges:
tls-sni-01 challenge for musicfind.fun

Waiting for verification...
Cleaning up challenges
-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/musicfind.fun/fullchain.pem
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/musicfind.fun/fullchain.pem (succes

#Nginx安裝http_ssl_module模塊

Nginx如果未開啟SSL模塊,配置Https時提示錯誤。

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx

nginx缺少http_ssl_module模塊,編譯安裝的時候帶上--with-http_ssl_module配置就行了。

本場景是服務器已經安裝過nginx,但是未安裝http_ssl_module。

1.進入到源碼包,如:

cd /usr/local/src/nginx-1.13.2

2.configure:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

3.make:

make

4.不需要執行make install,否則就覆蓋安裝了。

5.備份原有的nginx,如:

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

6.然後將剛剛編譯好的nginx覆蓋掉原有的nginx(nginx需要停止)

cp ./objs/nginx /usr/local/nginx/sbin/

7.查看安裝情況:

/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.13.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#nginx配置https

貼部分配置信息:

 server {

listen 80;
server_name www.yourdomain.com;
rewrite ^(.*) https://$server_name$1 permanent; #http 跳轉 https
}
server {
listen 443;
server_name www.musicfind.fun;
ssl on;
root html;
index index.html index.htm;
## 這裡放從lets encrypt中申請好的證書pem文件和key文件,注意文件實際路徑
ssl_certificate cert/214981350170042.pem;
ssl_certificate_key cert/214981350170042.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#禁止在header中出現服務器版本,防止黑客利用版本漏洞攻擊
server_tokens off;
#如果是全站 HTTPS 並且不考慮 HTTP 的話,可以加入 HSTS 告訴你的瀏覽器本網站全 站加密,並且強制用 HTTPS 訪問

fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;
}

先檢驗配置的對不對:

/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重啟nginx:

/usr/local/nginx/sbin/nginx -s reload


分享到:


相關文章: