Nginx配置多個HTTPS域名的方法
最近在玩微信小程序,手頭有:
- 一臺云服務器:CentOS 7
- 多個一級域名
開發(fā)測試過程中,因為某些原因,想要讓手頭的A、B域名同時指向云服務器的443端口,支持HTTPS。
Nginx支持TLS協(xié)議的SNI擴展(同一個IP上可以支持多個不同證書的域名),只需要重新安裝Nginx,使其支持TLS即可。
安裝Nginx
[root]# wget http://nginx.org/download/nginx-1.12.0.tar.gz [root]# tar zxvf nginx-1.12.0.tar.gz [root]# cd nginx-1.12.0 [root]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module \ --with-openssl=./openssl-1.0.1e \ --with-openssl-opt="enable-tlsext"
備注:在安裝的過程中發(fā)現(xiàn),云服務器的環(huán)境中缺少一些庫,下載后,重新執(zhí)行Nginx的./configure
指令,具體操作如下:
[root]# wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz [root]# tar zxvf pcre-8.35 [root]# yum -y install gcc [root]# yum -y install gcc-c++ [root]# yum install -y zlib-devel [root]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module \ --with-openssl=./openssl-1.0.1e \ --with-openssl-opt="enable-tlsext" \ --with-pcre=./pcre-8.35
配置Nginx
在購買域名的時候,如果域名提供商有免費的SSL證書,就直接用;如果沒有的話,可以使用 Let's Encript 生成免費的CA證書。
打開Nginx的配置:vi /etc/nginx/nginx.conf
... server { listen 443 ssl; listen [::]:443 ssl; server_name abc.com; root /usr/share/nginx/html; ssl_certificate "/root/keys/abc.com.pem"; ssl_certificate_key "/root/keys/abc.com.private.pem"; include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 443 ssl; listen [::]:443 ssl; server_name def.com; root /usr/share/nginx/html; ssl_certificate "/root/keys/def.com.pem"; ssl_certificate_key "/root/keys/def.com.private.pem"; include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
配置完成后,重新加載Ngixn:nginx -s reload
申請免費的CA證書
對于沒有SSL證書的情況,可以用下面的方法免費獲得CA證書——Let's Encript。
步驟1: 安裝 Let's Encrypt 官方客戶端——CetBot
[root]# yum install -y epel-releasesudo [root]# yum install -y certbot
步驟2: 配置Nginx的配置文件,在 Server 模塊(監(jiān)聽80端口的)添加下面配置:
CertBot在驗證服務器域名的時候,會生成一個隨機文件,然后CertBot的服務器會通過HTTP訪問你的這個文件,因此要確保你的Nginx配置好,以便可以訪問到這個文件。
server { listen 80 default_server; ... location ^~ /.well-known/acme-challenge/ { default_type "text/plain"; root /usr/share/nginx/html; } location = /.well-known/acme-challenge/ { return 404; } }
重新加載Nginx: nginx -s reload
步驟3: 申請SSL證書
[root]# certbot certonly --webroot -w /usr/share/nginx/html/ -d your.domain.com
安裝過程中,會提示輸入郵箱,用于更新CA證書的。
安裝成功后,默認會在 /etc/letsencrypt/live/your.domain.com/
會生成CA證書。
|-- fullchain.pem |-- privkey.pem
步驟4: 配置Nginx
server { listen 443 ssl; listen [::]:443 ssl; server_name def.com; root /usr/share/nginx/html; ssl_certificate "/etc/letsencrypt/live/your.domain.com/fullchain.pem"; ssl_certificate_key "/etc/letsencrypt/live/your.domain.com/privkey.pem"; include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
配置完,重新加載Nginx
步驟5: 自動更新證書
在命令行先進行模擬更新證書
certbot renew --dry-run
如果模擬更新成功,則 使用 crontab -e 命令來啟用自動更新任務:
[root]# crontab -e 30 2 * * 1 /usr/bin/certbot renew >> /var/log/le-renew.log
相關參考
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Ubuntu16.04上為Nginx創(chuàng)建自簽名SSL證書
SSL證書是實現(xiàn)HTTPS的關鍵組成部分,本文主要介紹了Ubuntu16.04上為Nginx創(chuàng)建自簽名SSL證書,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-05-05深入解析nginx路由location匹配規(guī)則及其優(yōu)先級
Nginx是一款高性能的Web服務器和反向代理服務器,它的路由功能是通過location指令來實現(xiàn)的,location指令用于匹配請求的URL,并將請求轉(zhuǎn)發(fā)到相應的處理程序或靜態(tài)文件,需要的朋友可以參考下2023-10-10Ubuntu下Nginx1.28.0源碼編譯安裝與systemd管理方式解讀
這篇文章主要介紹了Ubuntu下Nginx1.28.0源碼編譯安裝與systemd管理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-05-05Kubernetes中Nginx服務啟動失敗排查流程分析(Error:?ImagePullBackOff)
這篇文章主要介紹了Kubernetes中Nginx服務啟動失敗排查流程(Error:?ImagePullBackOff),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離
這篇文章主要介紹了使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。2017-01-01