NGINX免费配置二级域名及同时开启HTTPS(HTTP强制转HTTPS)nodejs的express后端项目,前端next.js的SSR项目
最近在做web3的創業項目,需要給第三方提供API接口需要配置二級域名:整個過程遇到了一系列問題都一一解決了,記錄下整個過程:
二級域名是可以不開啟HTTPS的,直接用HTTP也行,開啟HTTPS后配置會繁瑣些,但不算太復雜。一個SSL證書只能對應一個域名,所以要為二級域名申請一個證書,有免費的申請網站:FreeSSL首頁 - FreeSSL.cn一個提供免費HTTPS證書申請的網站
1、先在域名服務商哪里配置解析,也就是添加主機記錄(A),IP地址和一級域名的一樣,端口也一樣,我的是在Godaddy上買的,解析配置如下:
?添加完后,域名服務商這邊就好了,接下就去申請證書,申請證書也需要在域名服務商這邊在填寫一遍cname的驗證,這個驗證是SSL證書服務商那邊驗證這個域名是不是你所有,具體教程如下:ACME v2證書自動化快速入門,這個是可以自動續期的。
ACME v2證書自動化快速入門
1.安裝acme.sh
建議切換到root模式,可以減少證書安裝時的問題
sudo su?注意:MacOS 不用切換到root更簡單。
如果上面官方下載地址失敗 或者 太慢,可以選用國內的備用地址
curl https://gitcode.net/cert/cn-acme.sh/-/raw/master/install.sh?inline=false | sh -s email=my@example.com注意:安裝完成后,再重新打開命令行(如果是 SSH,選擇重新連接),以使acme.sh命令生效。
2.對域名進行授權
輸入域名
獲得域名驗證(DCV)授權信息
到您的域名解析服務商添加解析記錄,下面以DNSPod為例:
3.證書申請 & 部署
點擊【配置完成,立即檢測】后獲得證書申請命令
部署到 WebServer
Apache example:
acme.sh --install-cert -d example.com \ --cert-file /path/to/certfile/in/apache/cert.pem \ --key-file /path/to/keyfile/in/apache/key.pem \ --fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \ --reloadcmd "service apache2 force-reload"Nginx example:
acme.sh --install-cert -d example.com \ --key-file /path/to/keyfile/in/nginx/key.pem \ --fullchain-file /path/to/fullchain/nginx/cert.pem \ --reloadcmd "service nginx force-reload"享受自動續期吧!證書進入到30天有效期,acme.sh 會自動完成續期。
下面就可以配置NGINX的二級域名解析了,配置和一級域名的一樣,只是多了代理和SSL。前面只是講解當前一級域名https的請求。根據http二級域名經驗,進行制作二級https二級域名。都是一樣的,只要后面新增一個新的,然后再代理上新的就好了(注意:一定是在第一個server的后面新增server)完整核心的NGINX配置如下:
server {listen 80 default_server;listen [::]:80 default_server;root /var/www/html;#前端項目根目錄# Add index.php to the list if you are using PHPindex index.html index.htm index.nginx-debian.html;server_name _;rewrite ^(.*)$ https://$host$1 permanent; #用于將http頁面重定向到https頁面location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri.html $uri/ /index.html;#這里是next.js的多頁面訪問配置,解決404錯誤}}server {listen 80;server_name metadata.tnafcs.com;#這里很重要二級域名配置rewrite ^(.*)$ https://$host$1 permanent; #用于將http頁面重定向到https頁面location / {proxy_pass http://127.0.0.1:9897;#nodejs后端項目地址端口,開啟HTTP強制轉HTTPS后直接走HTTPS里的proxy_buffer_size 64k; proxy_buffering on; proxy_buffers 4 64k; proxy_busy_buffers_size 64k; proxy_max_temp_file_size 1024m; proxy_ssl_server_name off;} }server {listen 443 ssl http2;#開啟了HTTP2版本,加載速度是HTTP1.1的7倍左右listen [::]:443 ssl http2;#開啟了HTTP2版本,加載速度是HTTP1.1的7倍左右ssl_certificate /etc/nginx/cert/full_chain.pem;ssl_certificate_key /etc/nginx/cert/private.key;ssl_session_timeout 1d;ssl_session_cache shared:MozSSL:10m; # about 40000 sessionsssl_session_tickets off;# curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparamssl_dhparam /etc/nginx/cert/ffdhe2048.txt;# intermediate configurationssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;ssl_prefer_server_ciphers off;# HSTS (ngx_http_headers_module is required) (63072000 seconds)add_header Strict-Transport-Security "max-age=63072000" always;root /var/www/html;#前端項目根目錄location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri.html $uri/ /index.html;#這里是next.js的多頁面訪問配置,解決404錯誤}}server {listen 443 ssl http2;#開啟了HTTP2版本,加載速度是HTTP1.1的7倍左右listen [::]:443 ssl http2;#開啟了HTTP2版本,加載速度是HTTP1.1的7倍左右server_name metadata.etherfair.space;#這里很重要,二級域名配置ssl_certificate /root/.acme.sh/metadata.tnafcs.com/metadata.tnafcs.com.cer;ssl_certificate_key /root/.acme.sh/metadata.tnafcs.com/metadata.tnafcs.com.key;ssl_session_timeout 1d;ssl_session_cache shared:MozSSL:10m; # about 40000 sessionsssl_session_tickets off;# curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam#ssl_dhparam /etc/nginx/cert/ffdhe2048.txt;# intermediate configurationssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;ssl_prefer_server_ciphers off;# HSTS (ngx_http_headers_module is required) (63072000 seconds)add_header Strict-Transport-Security "max-age=63072000" always;location / {proxy_pass http://metadata.tnafcs.com:9897;#這里很重要,#nodejs后端項目地址端口,開啟HTTP強制轉HTTPS后直接走HTTPS里的proxy_buffer_size 64k; proxy_buffering on; proxy_buffers 4 64k; proxy_busy_buffers_size 64k; proxy_max_temp_file_size 1024m; proxy_ssl_server_name on;}}?如果出現502,那是SSL_do_handshake()握手失敗,proxy_pass 代理換成http的就可以了。參考這篇文章nginx反向代理網頁502、SSL_do_handshake()握手失敗_來碗米飯的博客-CSDN博客_ssl_do_handshake
其他的就不啰嗦了,具體可以看以下參考文獻:
1、二級域名https怎么進行?如何配置? - SSL網
2、【搭建后臺環境】nginx https 配置二級域名 - 掘金
3、NGINX反向代理某些請求出現502 BAD GATEWAY_IT_狂奔者的博客-CSDN博客_nginx反向代理出現502
4、nginx反向代理網頁502、SSL_do_handshake()握手失敗_來碗米飯的博客-CSDN博客_ssl_do_handshake
5、FreeSSL首頁 - FreeSSL.cn一個提供免費HTTPS證書申請的網站
6、ACME v2證書自動化快速入門
總結
以上是生活随笔為你收集整理的NGINX免费配置二级域名及同时开启HTTPS(HTTP强制转HTTPS)nodejs的express后端项目,前端next.js的SSR项目的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 广州软博前端实习生面经
- 下一篇: 自然语言生成技术现状调查:核心任务、应用
