日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

神奇的nginx之https支持

發布時間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 神奇的nginx之https支持 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

隨著技術的方法,http傳輸協議并不能保證數據傳輸的安全性,隨后https技術應運而生,nginx服務器支持https協議,配置的代碼也比較難記,記錄下以防遺忘。


HTTPS數據傳輸過程

  • 客戶端向服務器發送https請求;
  • 服務器上存儲了一套數字證書,其實質為一對公私鑰。數字證書可以自己制作,也可以向組織申請。前者在客戶端訪問時需要驗證才能繼續訪問;后者不會彈出驗證提示;
  • 服務器將公鑰傳輸給客戶端;
  • 客戶端驗證公鑰是否合法:無效(自己制作的)會彈出警告,有效的則生成一串隨機數,用此隨機數加密公鑰;
  • 客戶端將加密后的字符串傳輸給服務器
  • 服務器收到字符串后,先使用私鑰進行解密,獲取加密使用的隨機數,并以此隨機數加密傳輸的數據(對稱機密);
  • 服務器將加密后的數據傳輸給客戶端;
  • 客戶端收到數據后,使用自己的私鑰(即隨機字符串)進行解密。
  • 對稱加密:將數據和私鑰(隨機字符串)通過某種算法混合在一起,除非知道私鑰,否則無法解密。


    前期準備

    nginx支持https所需的ngx-http_ssl_module在編譯時默認是不安裝的,需要二次編譯安裝(一開始就安裝了的就不用再編譯了)。

    查看安裝的nginx是否已安裝ssl模塊

    [root@localhost conf]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx/

    安裝前需要注意一點:重新編譯后可能導致之前做的某些修改重置,例如虛擬主機文件被清除,因此最好對重要配置文件先進行備份。

    # 切換到你之前安裝所使用的nginx軟件包內 [root@localhost conf]# cd /usr/local/src/nginx-1.12.2/ [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module [root@localhost nginx-1.12.2]# make && make install[root@localhost conf]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx/ --with-http_ssl_module

    創建自定義證書文件

    • 創建私鑰key
    [root@localhost ~]# cd /usr/local/nginx/conf # 創建私鑰key文件,必須輸入密碼,否則無法生成key文件 [root@localhost conf]# openssl genrsa -des3 -out tmp.key 2048 Generating RSA private key, 2048 bit long modulus ..............................+++ ...............................................................+++ e is 65537 (0x10001) Enter pass phrase for tmp.key: Verifying - Enter pass phrase for tmp.key:
    • 轉換key,取消密碼
    [root@localhost conf]# openssl rsa -in tmp.key -out test.key Enter pass phrase for tmp.key: writing RSA key[root@localhost conf]# rm -f tmp.key
    • 生成證書
    [root@localhost conf]# openssl req -new -key test.key -out test.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:ZheJiang Locality Name (eg, city) [Default City]:QuZhou Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:# 需要使用csr文件與私鑰一起生成.crt文件 [root@localhost conf]# openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt Signature ok subject=/C=CN/ST=ZheJiang/L=QuZhou/O=Default Company Ltd Getting Private key

    這樣一個自定義創建的數字證書文件就創建成功了


    SSL配置代碼

    • 創建新虛擬主機配置文件
    [root@localhost conf]#vim /usr/local/nginx/conf/vhost/ssl.conf server {listen 443;server_name test.com;index index.html index.php;root /data/www/test.com;ssl on;# 指定自定義的數字證書ssl_certificate test.crt;# 指定對應的key文件ssl_certificate_key test.key;ssl_protocols TLSv1 TLS1.1 TLS1.2; }
    • 創建對應目錄及文件
    [root@localhost conf]# mkdir -p /data/www/test.com [root@localhost conf]# vim /data/www/test.com/index.php ssl test page.
    • 重啟服務
    [root@localhost conf]# /usr/local/nginx/sbin/nginx -t [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload# 查看443端口是否開放 [root@localhost conf]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name ... tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 4953/nginx: master ...

    轉載于:https://blog.51cto.com/castiel/2060000

    總結

    以上是生活随笔為你收集整理的神奇的nginx之https支持的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。