Nginx自建SSL證書部署HTTPS網站


一、創建SSL相關證書

1.安裝Nginx(這里為了測試使用yum安裝,實際看具體情況)

[root@localhost ~]# yum install nginx -y  #默認yum安裝已經支持SSL,如果是自己編譯的可以通過 nginx -V 查看當前Nginx是否支持SSL
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# mkdir ssl
[root@localhost nginx]# cd ssl/

2.生成一個RSA密鑰

[root@localhost ssl]#  openssl genrsa -des3 -out nginx.key 1024 #實際使用中看服務器性能,如果足夠好也可以使用4096位秘鑰 Generating RSA private key, 1024 bit long modulus .......++++++ ...++++++ e is 65537 (0x10001) Enter pass phrase for nginx.key: #輸入密碼,自定義,不少於4個字符 Verifying - Enter pass phrase for nginx.key:     #確認密碼

3.生成一個證書請求

[root@localhost ssl]# openssl req -new -key nginx.key -out nginx.csr Enter pass phrase for nginx.key: #輸入剛剛創建的秘密碼 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) []:ShangHai #省 Locality Name (eg, city) [Default City]:ShangHai #市 Organization Name (eg, company) [Default Company Ltd]:ACBC #公司 Organizational Unit Name (eg, section) []:Tech #部門 Common Name (eg, your name or your server's hostname) []:*.mydomain.com #注意,此處應當填寫你要部署的域名,如果是單個則直接添加即可,如果不確定,使用*,表示可以對所有mydomain.com的子域名做認證
Email Address []:admin@mydomain.com #以域名結尾即可 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: #是否設置密碼,可以不寫直接回車 An optional company name []: #其他公司名稱 可不寫

4.創建不需要輸入密碼的RSA證書,否則每次reload、restart都需要輸入密碼

[root@localhost ssl]# openssl rsa -in nginx.key -out nginx_nopass.key Enter pass phrase for nginx.key: #之前RSA秘鑰創建時的密碼 writing RSA key

5.簽發證書(由於是測試自己簽發,實際應該將自己生成的csr文件提交給SSL認證機構認證)

[root@localhost ssl]# openssl x509 -req -days 3650 -in nginx.csr  -signkey nginx.key -out nginx.crt Signature ok subject=/C=CN/ST=ShangHai/L=ShangHai/O=ACBC/OU=Tech/CN=*.mydomain.com/emailAddress=admin@mydomain.com Getting Private key Enter pass phrase for nginx.key:          #RSA創建時的密碼

二、配置Nginx

1、修改配置文件(注意域名,特別是443)

# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
 user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name test.mydomain.com; #配置自己的域名 注意要以自己申請證書時填寫的域名一致 root /usr/share/nginx/html; # Load configuration files for the default server block. 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 { } } # Settings for a TLS enabled server. server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; server_name test.mydomain.com; #與申請時的域名保持一致,否則會報錯 root /usr/share/nginx/html; ssl_certificate "/etc/nginx/ssl/nginx.crt"; # ssl_certificate_key "/etc/nginx/ssl/nginx_nopass.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. 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 { } } }

#配置簡介

ssl_certificate證書其實是個公鑰,它會被發送到連接服務器的每個客戶端,ssl_certificate_key私鑰是用來解密的,所以它的權限要得到保護但nginx的主進程能夠讀取。當然私鑰和證書可以放在一個證書文件中,這種方式也只有公鑰證書才發送到client。 ssl_session_timeout 客戶端可以重用會話緩存中ssl參數的過期時間,內網系統默認5分鍾太短了,可以設成30m即30分鍾甚至4h。 ssl_protocols指令用於啟動特定的加密協議,nginx在1.1.13和1.0.12版本后默認是ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1與TLSv1.2要確保OpenSSL >= 1.0.1 ,SSLv3 現在還有很多地方在用但有不少被攻擊的漏洞。 ssl_ciphers選擇加密套件,不同的瀏覽器所支持的套件(和順序)可能會不同。這里指定的是OpenSSL庫能夠識別的寫法,你可以通過 openssl -v cipher ‘RC4:HIGH:!aNULL:!MD5’(后面是你所指定的套件加密算法) 來看所支持算法。 ssl_prefer_server_ciphers on設置協商加密算法時,優先使用我們服務端的加密套件,而不是客戶端瀏覽器的加密套件。

2.啟動服務

[root@localhost nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost nginx]# echo "This test page" > /usr/share/nginx/html/test.html #創建一個測試頁面 [root@localhost nginx]# systemctl restart nginx

3.測試

綁定 test.mydomain.com域名對應的IP到客戶端

 使用HTTP協議訪問

 使用HTTPS協議訪問

 

有個警告,正常,應用我們的證書沒有經過認證,點擊高級,點擊繼續前往

 

 

 從結果看,HTTPS配置成功

4.有時候我們希望無論是HTTP或者HTTPS協議都通過HTTPS訪問,可以做如下配置

 server { listen 80 default_server; listen [::]:80 default_server; server_name test.mydomain.com; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; return 301 https://$server_name$request_uri;  #在80監聽端口 配置跳轉

在使用HTTP協議訪問,為避免緩存的影響,我們可以換一個瀏覽器

 輸入URL后回車,跳轉到HTTPS了, 點擊高級-->接受風險並繼續

 結果為:

 三、配置Nginx使用HTTPS代理后端Tomcat服務

1.下載Tomcat,並測試是否訪問正常

[root@localhost nginx]# cd /mnt/ [root@localhost mnt]# tar xf apache-tomcat-7.0.96.tar.gz [root@localhost mnt]# cd apache-tomcat-7.0.96/bin/ [root@localhost bin]# ./startup.sh [root@localhost bin]# curl -I http://172.16.150.132:8080 #如果在他為200 OK則正常

2.配置Nginx代理到Tomcat

Nginx代理可以分為兩種情況

1.全代理

2.只代理動態請求,靜態請求本地或者代理到其他靜態服務器上,即動靜分離

我們先配置第一種情況:全代理

[root@localhost bin]# cd /etc/nginx/ [root@localhost nginx]# vim nginx.conf server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; server_name test.mydomain.com; root /usr/share/nginx/html; ssl_certificate "/etc/nginx/ssl/nginx.crt"; ssl_certificate_key "/etc/nginx/ssl/nginx_nopass.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://localhost:8080;  #使用proxy_pass 直接代理 } [root@localhost bin]# cd /etc/nginx/ [root@localhost nginx]# vim nginx.conf [root@localhost nginx]# systemctl restart nginx

訪問測試一下,直接使用HTTP協議

 

 沒有問題

動靜分離配置

[root@localhost nginx]# vim nginx.conf location / { #刪除之前的8080代理 index index.html index.htm; } location ~ \.(jsp|jspx|do|action)(\/.*)?$ { proxy_set_header real_ip $remote_addr; proxy_pass http://localhost:8080;
 } [root@localhost nginx]# nginx -t [root@localhost nginx]# systemctl restart nginx

訪問HTML文件

訪問JSP文件

3.生產其他常見配置

proxy_set_header Host $http_host; #避免http請求中丟失Host頭部的情況下Host不被重寫的失誤 proxy_set_header X-Forwarded-For $http_x_forwarded_for; #代表客戶端,也就是HTTP的請求端真實的IP proxy_set_header X-Real-IP $remote_addr;                #將$remote_addr的值放進變量X-Real-IP中,此變量名可變,$remote_addr的值為客戶端的ip add_header Access-Control-Allow-Origin *; #表示允許訪問的外域請求 add_header Access-Control-Allow-Headers X-Requested-With; #首部字段用於預檢請求的響應。其指明了實際請求中允許攜帶的首部字段。 add_header Access-Control-Allow-Methods GET,POST,OPTIONS; #首部字段用於預檢請求的響應。其指明了實際請求所允許使用的 HTTP 方法。 add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload"; #防止中間人攻擊 add_header X-Frame-Options:ALLOW-FROM http://www.growingio.com; # 避免點擊劫持 (clickjacking) 的攻擊
add_header X-Content-Type-Options nosniff; #不允許瀏覽器任何猜測的行為 ssi on; #開啟SSI ssi_silent_errors on; ssi_types *; proxy_redirect http:// $scheme://; #修改從被代理服務器傳來的應答頭中的"Location"和"Refresh"字段,將http修改為https
port_in_redirect on;                 #告訴nginx,遇到跳轉的時候,不要加上自己的端口號

#可配置參數由openssl 的ciphers定義  openssl ciphers -v #查看支持的配置參數
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';

  ssl_stapling on; #啟用或禁用 服務器對OCSP響應的裝訂 #檢驗證書合法性的在線查詢服務
  ssl_stapling_verify on; #啟用或禁用服務器對OCSP響應的驗證

#如果啟用了ssl_stapling,則以file PEM格式 指定具有受信任CA證書的證書,用於驗證客戶端證書和OCSP響應。 順序為 站點證書、中間證書(1張或多張)、根證書
ssl_trusted_certificate /path/to/file;

#使用Diffie-Hellman方法讓訪問者的瀏覽器和服務器安全的交換密鑰 
#使用 openssl dhparam -out /usr/ssl/dhparam.pem 2048 命令生成需要的pem文件 其中默認為1024位
ssl_dhparam /etc/ssl/certs/dhparam.pem;


網站SSL配置檢查:
https://www.ssllabs.com/ssltest/
https://myssl.com/myssl.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM