nginx開啟ssl並把http重定向到https的兩種方式


1 簡介

Nginx是一個非常強大和流行的高性能Web服務器。本文講解Nginx如何整合https並將http重定向到https

https相關文章如下:

(1)Springboot整合https原來這么簡單

(2)HTTPS之密鑰知識與密鑰工具Keytool和Keystore-Explorer

(3)Springboot以Tomcat為容器實現http重定向到https的兩種方式

(4)Springboot以Jetty為容器實現http重定向到https

NGINX

Nginx的特點:

(1)熱啟動:例如當修改配置文件后,不需要停止與啟動就可以讓配置生效,命令如下:

nginx -s reload

(2)高並發連接:頂住10萬以上連接是沒有問題的。

(3)低內存消耗:在高性能的同時,保持很低的內存消耗;

(4)響應請求快;

(5)高可靠性。

Nginx可以做哪些事呢?最常用的功能為下面三個:

(1)靜態HTTP服務器,實現動靜態分離

(2)反向代理

(3)負載均衡

2 安裝與使用

CentOS使用下面命令進行安裝與使用:

# 添加 Nginx 源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # 安裝 Nginx yum install -y nginx # 啟動 Nginx systemctl start nginx.service # 停止 Nginx systemctl stop nginx.service # 設置開機自啟 Nginx systemctl enable nginx.service # 重新加載 nginx -s reload 

Mac使用下面命令進行安裝和使用:

# 查看是否有安裝 brew info nginx # 安裝 brew install nginx # 啟動,默認端口為8080 nginx # 停止 nginx -s stop # 重新加載 nginx -s reload 

安裝完會有提供說明:
Docroot is: /usr/local/var/www

nginx will load all files in /usr/local/etc/nginx/servers/

就知道該在哪放網站資源和配置文件了。

3 整合https

3.1 生成密鑰文件

先通過keytool生成PKCS12格式的密鑰,然后通過openssl取出certkey,具體命令如下:

# 生成PKCS12格式的密鑰文件 keytool -genkey -alias localhost -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -storetype PKCS12 -keystore localhost.p12 -dname CN=localhost,OU=Test,O=pkslow,L=Guangzhou,C=CN -validity 731 -storepass changeit -keypass changeit # 導出pem(certificate) openssl pkcs12 -nokeys -in ./localhost.p12 -out localhost.pem # 導出key openssl pkcs12 -nocerts -nodes -in ./localhost.p12 -out localhost.key 

3.2 配置nginx.conf

新建一個nginx.conf文件,把它放在配置加載目錄上。要把密鑰文件路徑配置上去,具體配置如下:

server { listen 443 ssl; server_name localhost; ssl_certificate /key-path/localhost.pem; ssl_certificate_key /key-path/localhost.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8000/; } } 

記得要替換key-path為具體的密鑰文件的路徑。

ssl_certificate:這個配置的是cert文件。

ssl_certificate_key:這個配置的是private key文件。

proxy_pass http://127.0.0.1:8000/:這個作用是把請求反向代理到這個地址上。

4 開啟http並重定向到https

4.1 開啟http

開啟http很簡單,直接把listen 80;加到listen 443 ssl;上面去就可以了。或者新加一個server配置,如下:

server { listen 443 ssl; server_name localhost; ssl_certificate /key-path/localhost.pem; ssl_certificate_key /key-path/localhost.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8000/; } } server { listen 80; server_name localhost; location / { proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8000/; } } 

4.2 重定向到https的兩種方式

要把http重定向到https也很簡單,具體可以使用兩種配置來實現。

第一種方式使用return 301如下:

server { listen 80; server_name localhost; return 301 https://127.0.0.1$request_uri; } 

第二種方式使用rewrite如下:

server { listen 80; server_name localhost; rewrite ^(.*)$ https://$host$1 permanent; } 

對於returnrewrite的區別,可以閱讀這篇文章:Creating NGINX Rewrite Rules

5 總結

最后,介紹一個工具,可以快速方便獲得nginx的配置:Nginx Config


免責聲明!

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



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