文章詳情頁
CentOS環境下Nginx配置SSL證書實現https請求詳解
瀏覽:90日期:2023-06-15 15:23:22
目錄
- 一、證書申請
- 二、配置SSL
- 2.1 證書上傳
- 2.2 Server配置
- 2.3 配置轉發
- 三、配置問題
- 3.1 ngx_http_ssl_module
- 3.2 ERR_SSL_PROTOCOL_ERROR
- 四、配置示例
- 4.1 SSL完整配置
一、證書申請
- 申請SSL證書,申請之后會有兩個文件提供下載(注意下載nginx版本),阿里云有免費的SSL證書申請
- xxx.key
- xxx.pem
- nginx安裝版本使用的是1.16.1
二、配置SSL
2.1 證書上傳
- 在nginx的安裝目錄下創建cert(別的名字也可以)
- 將下載的SSL證書文件上傳到cert下
2.2 Server配置
- 進入到nginx下的conf文件夾下打開nginx.conf文件
- 取消https server的注釋
# HTTPS serverserver { 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; }}
- 需要配置一下說明的內容
# HTTPS serverserver { # 注意這里就是443 ssl, 不要把ssl刪除了 listen 443 ssl; # 把localhost替換為SSL綁定的域名, 如www.codecoord.com # server_name localhost; server_name www.codecoord.com; # 添加默認主目錄和首頁, 根據自己的路徑修改 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端口需要在開啟外網訪問(比如阿里云服務器需要在控制臺配置安全組, 不過默認是打開的)
2.3 配置轉發
- 這一步是配置對外訪問端口和將http請求強制轉為https
- 刪除多余配置,只需要留下以下配置
server { # 監聽端口 listen 80; # 改為自己的域名 server_name www.codecoord.com; # 將http請求強制轉為https # rewrite:重寫指令,$host$:請求地址,$1:請求參數,permanent:永久訪問 rewrite ^(.*)$ https://$host$1 permanent;}
上述兩步配置完成后測試一下是否配置正確,在sbin目錄下運行測試命令
- ./nginx -t
# 配置成功信息[root@TianXin sbin]# ./nginx -tnginx: the configuration file /opt/Nginx/conf/nginx.conf syntax is oknginx: 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之前版本, 配置內容會有有所變化,請參考別的版本配置
- 如果運行./nginx -t時出現以下錯誤,標識nginx沒有安裝SSL模塊
[root@tianxin conf]# nginx -tnginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/conf/nginx.conf:112nginx: configuration file /opt/nginx/conf/nginx.conf test failed
- 解決方法是重新配置nginx,重新編譯帶上--with-http_stub_status_module --with-http_ssl_module
- 可以重新安裝nginx(建議, 可以避免很多問題)也可以不用重新安裝, 不用重新安裝只需要執行下面的兩個命令即可
# 清除編譯文件make clean# 配置./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module# 編譯make
- 不要執行make install 否則會覆蓋原來的文件
- 關閉nginx
- nginx -s stop
- 拷貝目錄下的objs/nginx替換之前的nginx啟動文件
- cp objs/nginx /opt/nginx/sbin/
- 最后啟動nginx即可
3.2 ERR_SSL_PROTOCOL_ERROR
- 此問題在該版本中出現是因為listen配置的時候把443 后面的ssl刪除了導致這個錯誤
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;#pidlogs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfileon; 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.comserver_name www.codecoord.com;# 添加默認主目錄和首頁, 根據自己的路徑修改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實戰-配置SSL證書(CentOS環境),實現https請求的詳細內容,更多關于Nginx配置SSL實現https請求的資料請關注其它相關文章!
標簽:
Nginx
排行榜
