Windows平臺Nginx部署https網站的過程
背景
同事打電話告知有人說Windows平臺的nginx不支援https
我當時就懵逼了
怎麼可能.
然後說開會時說的. 我當時認為不可能..
不會這麼弱, 除非官方nginx沒代ssl模組
想著趁中午休息時間捯飭一下.
結果發現部不午休的確容易犯低階錯誤
遂總結一下.
下載
https://nginx.org/en/download.html
具體的連結地址為:
https://nginx.org/download/nginx-1.26.0.zip
需要說明:
偶數的版本是穩定版
奇數的版本是開發版
建議使用偶數版本.
檢視編譯包含的元件
C:\nginx-1.26.0>nginx.exe -V
nginx version: nginx/1.26.0
built by cl 16.00.30319.01 for 80x86
built with OpenSSL 3.0.13 30 Jan 2024
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug
--prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log
--error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp
--http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp
--http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp
--with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre2-10.39 --with-zlib=objs.msvc8/lib/zlib-1.3.1
--with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module
--with-http_dav_module --with-http_stub_status_module --with-http_flv_module
--with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module
--with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module
--with-http_slice_module --with-mail --with-stream --with-stream_realip_module
--with-stream_ssl_preread_module --with-openssl=objs.msvc8/lib/openssl-3.0.13
--with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501'
--with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module
確認編譯是帶ssl模組的 不可能不支援.
建立證書
使用之前指令碼建立的自簽名證書
mkdir -p /cert
cd /cert
openssl genrsa -out ca.key 4096
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/C=CN/ST=SD/L=JN/O=JNXLH/OU=JNXLH"
# 建立好 ca 之後 需要建立 服務端證書
openssl req -new -nodes -keyout server.key -out server.csr -subj "/C=CN/ST=SD/L=JN/O=JNXLH/OU=JNXLH/CN=10.110.139.121,10.110.139.122"
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 36000 -extensions v3_req
# 檢視證書
openssl x509 -text -in server.crt
# 生成pfx
openssl pkcs12 -password pass:Testxxxxxxxx -export -out server.pfx -inkey server.key -in server.crt
然後將 server.key 和 server.crt 放到 conf/cert 目錄下面
配置檔案
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
access_log off;
client_max_body_size 20m;
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
client_body_buffer_size 100m;
gzip_buffers 16 8k;
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
keepalive_timeout 6000;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
proxy_connect_timeout 600s;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
server_tokens off;
upstream myapp{
ip_hash;
server 172.24.110.201:5200 ;
}
server {
listen 80;
server_name your.site.com ;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name your.site.com ;
error_page 497 https://$http_host$request_uri;
ssl_certificate cert/server.crt;
ssl_certificate_key cert/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
valid_referers none blocked server_names;
if ($invalid_referer = "1") {
return 403;
}
location / {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' "true";
proxy_pass http://myapp ;
}
location ^~ /api/runtime/sys/v1.0/messagecenter {
proxy_pass http://myapp/api/runtime/sys/v1.0/messagecenter;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
啟動nginx
在 cmd 開啟
cd c:\nginx-1.26.0
nginx.exe
挖坑
發現nginx 使用cmd方式啟動之後, 如果關了 cmd 程序其實不消失
關閉服務的方法可以使用:
taskkill /im nginx.exe /F
然後雙擊nginx
視窗雖然小時, 但是工作管理員 詳細資訊裡面可以看到多個nginx的程序
所以建議需要每次關閉一下nginx, 再啟動
避免程序殘留 出現誤判
我這邊就浪費了 十分鐘.