前言
应用旧访问地址:http://192.168.1.1:6622/xxxx
SSL证书生成
绑定证书
一、IIS配置证书绑定,启用HTTPS
优点:方便、快捷,删除原先端口绑定的HTTP,添加新的HTTPS协议至原端口。
缺点:如果应用程序在很多地方使用,协议升级后,原地址:http://192.168.1.1:6622
这种访问形式会失效,如果存在大量引用,会导致工作量大、难排查的问题
改进
IIS配置里删除应用的旧绑定端口(6622),新增一个端口(6500)用于Nginx代理目标
tips: IIS里6500端口可以不配置证书,使用Nginx中配置的证书。
通过Nginx监听原先端口,配置SSL证书文件,利用Nginx自带的497状态,将HTTP访问升级至HTTPS访问
配置文件:
nginx.conf1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| server { listen 6622 ssl; server_name 192.168.1.1; # SSL ssl_certificate cert/DEMO.crt; ssl_certificate_key cert/DEMO.key; # 中间证书 ssl_trusted_certificate cert/RootCA.crt; error_page 497 301 =307 https://$host:6622$request_uri; location /{ proxy_pass http://$host:6500; #请求转发至新端口 proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; } }
# 情况 服务器只开放6530 一个https的端口 # nginx 通过监听6530的访问情况,使用497处理http访问异常,重定向至https的6530端口 # https访问后,nginx内部代理访问内网的6529端口【实际服务所在端口】, # 再 使用proxy_redirect 修改6529传来的应答头中的"Location"和"Refresh"字段 # 这样就实现了http和https的兼容,并且不需要修改其它项目中的地址。 server { listen 6530 ssl; server_name localhost; # SSL ssl_certificate cert/124-221-233-39_DEMO.crt; ssl_certificate_key cert/124-221-233-39_DEMO.key; ssl_trusted_certificate cert/124-221-233-39_RootCA.crt; location /{ # proxy_ssl_server_name on; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host:$server_port;; proxy_set_header X-NginX-Proxy true; proxy_pass http://10.0.4.13:6529; proxy_redirect http://10.0.4.13:6529 /; } error_page 497 301 =307 https://$host:$server_port$request_uri; }
|
参考:
https://stackoverflow.com/questions/16669773/nginx-reverse-proxy-to-backend-running-on-localhost
https://blog.csdn.net/faye0412/article/details/75200607
https://blog.csdn.net/jycjyc/article/details/106191981
https://blog.csdn.net/qq_27745471/article/details/124735251
How to redirect on the same port from http to https with nginx reverse proxy
nginx redirect http to https with custom port server
二、使用IIS的URL重写组件【只适用有域名的】
参考:
https://aboutssl.org/iis-redirection-http-to-https/
https://learn.microsoft.com/en-us/answers/questions/762252/iis-10-redirect-http-to-httpsmysitecom.html
IIS中实现HTTPS的自动跳转
URL 重写模块配置引用
URL Rewrite Module 2.0 Configuration Reference
IIS Server Variables