如何在Nginx中配置多個(gè)HTTPS域名-創(chuàng)新互聯(lián)

本篇文章為大家展示了如何在Nginx中配置多個(gè)HTTPS域名,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比青白江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式青白江網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋青白江地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴(lài)。

環(huán)境:

  1. CentOS 7

  2. 多個(gè)一級(jí)域名

開(kāi)發(fā)測(cè)試過(guò)程中,因?yàn)槟承┰?,想要讓手頭的A、B域名同時(shí)指向云服務(wù)器的443端口,支持HTTPS。

Nginx支持TLS協(xié)議的SNI擴(kuò)展(同一個(gè)IP上可以支持多個(gè)不同證書(shū)的域名),只需要重新安裝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"

備注:在安裝的過(guò)程中發(fā)現(xiàn),云服務(wù)器的環(huán)境中缺少一些庫(kù),下載后,重新執(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

在購(gòu)買(mǎi)域名的時(shí)候,如果域名提供商有免費(fèi)的SSL證書(shū),就直接用;如果沒(méi)有的話,可以使用 Let's Encript 生成免費(fèi)的CA證書(shū)。

打開(kāi)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證書(shū)

對(duì)于沒(méi)有SSL證書(shū)的情況,可以用下面的方法免費(fèi)獲得CA證書(shū)——Let's Encript。

步驟1: 安裝 Let's Encrypt 官方客戶(hù)端——CetBot

[root]# yum install -y epel-releasesudo 
[root]# yum install -y certbot

步驟2: 配置Nginx的配置文件,在 Server 模塊(監(jiān)聽(tīng)80端口的)添加下面配置:

CertBot在驗(yàn)證服務(wù)器域名的時(shí)候,會(huì)生成一個(gè)隨機(jī)文件,然后CertBot的服務(wù)器會(huì)通過(guò)HTTP訪問(wèn)你的這個(gè)文件,因此要確保你的Nginx配置好,以便可以訪問(wèn)到這個(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;
  }
}

重新加載Nginx: nginx -s reload

步驟3: 申請(qǐng)SSL證書(shū)

[root]# certbot certonly --webroot -w /usr/share/nginx/html/ -d your.domain.com

安裝過(guò)程中,會(huì)提示輸入郵箱,用于更新CA證書(shū)的。

安裝成功后,默認(rèn)會(huì)在 /etc/letsencrypt/live/your.domain.com/ 會(huì)生成CA證書(shū)。

|-- 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: 自動(dòng)更新證書(shū)

在命令行先進(jìn)行模擬更新證書(shū)

certbot renew --dry-run

如果模擬更新成功,則 使用 crontab -e 命令來(lái)啟用自動(dòng)更新任務(wù):

[root]# crontab -e
30 2 * * 1 /usr/bin/certbot renew >> /var/log/le-renew.log

上述內(nèi)容就是如何在Nginx中配置多個(gè)HTTPS域名,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)標(biāo)題:如何在Nginx中配置多個(gè)HTTPS域名-創(chuàng)新互聯(lián)
鏈接地址:http://muchs.cn/article38/cososp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站收錄、ChatGPT建站公司、微信小程序網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)