最近在玩微信小程序,手頭有:
一臺(tái)云服務(wù)器:CentOS 7 多個(gè)一級(jí)域名開發(fā)測(cè)試過程中,因?yàn)槟承┰颍胍屖诸^的A、B域名同時(shí)指向云服務(wù)器的443端口,支持HTTPS。
Nginx支持TLS協(xié)議的SNI擴(kuò)展(同一個(gè)IP上可以支持多個(gè)不同證書的域名),只需要重新安裝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),云服務(wù)器的環(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
在購買域名的時(shí)候,如果域名提供商有免費(fèi)的SSL證書,就直接用;如果沒有的話,可以使用 Let's Encript 生成免費(fèi)的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
申請(qǐng)免費(fèi)的CA證書
對(duì)于沒有SSL證書的情況,可以用下面的方法免費(fèi)獲得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在驗(yàn)證服務(wù)器域名的時(shí)候,會(huì)生成一個(gè)隨機(jī)文件,然后CertBot的服務(wù)器會(huì)通過HTTP訪問你的這個(gè)文件,因此要確保你的Nginx配置好,以便可以訪問到這個(gè)文件。
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; }}
新聞熱點(diǎn)
疑難解答
圖片精選