CentOS環(huán)境下Nginx配置SSL證書實現(xiàn)https請求詳解
更新時間:2023年05月08日 10:18:44 作者:TianXinCoord
這篇文章主要為大家介紹了Nginx實戰(zhàn)-配置SSL證書(CentOS環(huán)境),實現(xiàn)https請求過程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
一、證書申請
- 申請SSL證書,申請之后會有兩個文件提供下載(注意下載nginx版本),阿里云有免費的SSL證書申請
- xxx.key
- xxx.pem
- nginx安裝版本使用的是1.16.1
二、配置SSL
2.1 證書上傳
- 在nginx的安裝目錄下創(chuàng)建cert(別的名字也可以)
- 將下載的SSL證書文件上傳到cert下
2.2 Server配置
- 進(jìn)入到nginx下的conf文件夾下打開nginx.conf文件
- 取消https server的注釋
# HTTPS server server { listen 443 ssl; server_name localhost; ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }
- 需要配置一下說明的內(nèi)容
# HTTPS server server { # 注意這里就是443 ssl, 不要把ssl刪除了 listen 443 ssl; # 把localhost替換為SSL綁定的域名, 如www.codecoord.com # server_name localhost; server_name www.codecoord.com; # 添加默認(rèn)主目錄和首頁, 根據(jù)自己的路徑修改 root /opt/nginx/html; index index.html; # cert.pem和cert.key替換為上傳文件的路徑(最好使用完整路徑) # ssl_certificate cert.pem; # ssl_certificate_key cert.key; ssl_certificate /opt/nginx/cert/cert.pem; ssl_certificate_key /opt/nginx/cert/cert.key; # 下面的不用動 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }
- 注意443端口需要在開啟外網(wǎng)訪問(比如阿里云服務(wù)器需要在控制臺配置安全組, 不過默認(rèn)是打開的)
2.3 配置轉(zhuǎn)發(fā)
- 這一步是配置對外訪問端口和將http請求強(qiáng)制轉(zhuǎn)為https
- 刪除多余配置,只需要留下以下配置
server { # 監(jiān)聽端口 listen 80; # 改為自己的域名 server_name www.codecoord.com; # 將http請求強(qiáng)制轉(zhuǎn)為https # rewrite:重寫指令,$host$:請求地址,$1:請求參數(shù),permanent:永久訪問 rewrite ^(.*)$ https://$host$1 permanent; }
上述兩步配置完成后測試一下是否配置正確,在sbin目錄下運行測試命令
- ./nginx -t
# 配置成功信息 [root@TianXin sbin]# ./nginx -t nginx: the configuration file /opt/Nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/Nginx/conf/nginx.conf test is successful
- 如果測試成功則重啟nginx,使配置生效
[root@TianXin sbin]# ./nginx -s reload
- 完整配置參考第四點配置示例
- 配置完成后訪問域名,即可顯示https信息
三、配置問題
3.1 ngx_http_ssl_module
- 注意如果是nginx 1.16.1之前版本, 配置內(nèi)容會有有所變化,請參考別的版本配置
- 如果運行./nginx -t時出現(xiàn)以下錯誤,標(biāo)識nginx沒有安裝SSL模塊
[root@tianxin conf]# nginx -t nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/conf/nginx.conf:112 nginx: configuration file /opt/nginx/conf/nginx.conf test failed
- 解決方法是重新配置nginx,重新編譯帶上--with-http_stub_status_module --with-http_ssl_module
- 可以重新安裝nginx(建議, 可以避免很多問題)也可以不用重新安裝, 不用重新安裝只需要執(zhí)行下面的兩個命令即可
# 清除編譯文件 make clean # 配置 ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module # 編譯 make
- 不要執(zhí)行make install 否則會覆蓋原來的文件
- 關(guān)閉nginx
- nginx -s stop
- 拷貝目錄下的objs/nginx替換之前的nginx啟動文件
- cp objs/nginx /opt/nginx/sbin/
- 最后啟動nginx即可
3.2 ERR_SSL_PROTOCOL_ERROR
- 此問題在該版本中出現(xiàn)是因為listen配置的時候把443 后面的ssl刪除了導(dǎo)致這個錯誤
server { # 注意這里就是443 ssl, 不要把ssl刪除了,之前的版本 listen 443 ssl; ... }
- 解決方法就是不要把443后面的ssl漏了,注意中間有空格
四、配置示例
4.1 SSL完整配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.codecoord.com codecoord.com; rewrite ^(.*)$ https://$host$1 permanent; } # https server { # 注意這里就是443 ssl, 不要把ssl刪除 listen 443 ssl; # 替換為SSL綁定的域名, 如www.codecoord.com server_name www.codecoord.com; # 添加默認(rèn)主目錄和首頁, 根據(jù)自己的路徑修改 root /opt/nginx/html; index index.html; # cert.pem和cert.key替換為上傳文件的路徑 ssl_certificate /opt/nginx/cert/www.codecoord.com.pem; ssl_certificate_key /opt/nginx/cert/www.codecoord.com.key; # 下面的不用動 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; try_files $uri $uri/ /index.html; # 解決vue頁面刷新404問題 } } }
以上就是Nginx實戰(zhàn)-配置SSL證書(CentOS環(huán)境),實現(xiàn)https請求的詳細(xì)內(nèi)容,更多關(guān)于Nginx配置SSL實現(xiàn)https請求的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx上對同一IP訪問的特定URL進(jìn)行限流實現(xiàn)
要在Nginx上對同一IP訪問的特定URL進(jìn)行限流,您可以使用ngx_http_limit_req_module模塊,本文就來介紹一下如何使用,具有一定的參考價值,感興趣的餓2024-01-01