问题:
1、 配置切记要仔细,每个配置项最后都应以_ ; _
结尾。报错如下:
2022/07/11 12:51:29 [emerg] 3284#4856: "worker_processes" directive is not allowed here in F:\nginx/conf/conf.d/nginx(1).conf:3
2、配置文件中配置文件名与实际路径下文件名不一致。报错如下:
2022/07/11 12:52:49 [emerg] 4504#2412: cannot load certificate "F:\nginx/conf/ssl/mpatrol.cn_bundle.pem": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('F:\nginx/conf/ssl/mpatrol.cn_bundle.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
问题修复过程:
1、刚开始以为是winServer不支持绝对路径配置,然后修改为绝对路径,问题依然存在。
2、将证书相关两文件放置conf文件目录下测试相对路径与绝对路径,问题依然存在。
3、最终发现拷贝文件与配置中文件名不一致,修改一致后,问题修复。
3、nginx版本不同,配置项有所改动:(非问题,作为记录防止遗忘)
1.15及以后版本都不在支持ssl on;
老版本nginx配置https使用:ssl on; 开启ssl属性。
新版本nginx配置https使用:listen 443 ssl; 即可开启ssl属性。
4、400 Bad Request: The plain HTTP request was sent to HTTPS port
从报错的字面意思上来看,是因为HTTP请求被发送到HTTPS端口,这种报错多出现在Nginx既处理HTTP请求又处理HTTPS请求的情况。
该项目nginx中配置了http server模块和https server模块,具体业务流向为:
https -> http: 访问登陆页后跳转详情页
由于配置不完善导致登陆成功后跳转详情页时,未能正常将所有的HTTP请求重定向到HTTPS导致页面异常。
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://0.0.0.0:8082;
proxy_redirect http:// https://; # 将http协议转为https协议
解读:
1)proxy_pass执行前,先设置了request head host 为https外网访问的域名+端口
2)proxy_pass执行后,tomcat结果返回response
3)proxy_redirect修改response中的location中的协议http为https外网访问的协议。
java redirect重定向主要是通过访问tomcat服务的请求head项来决定的,默认是http协议,域名是通过读取host地址,默认host中不包括访问端口,因此使用非443端口时,访问web地址时需添加端口。
当前项目主要配置项:在https server模块下添加http重定向https。
server {
listen 81;
listen 8087 ssl;
server_name www.XXX.cn XXX.cn;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate ssl/XX.cn_bundle.crt;
ssl_certificate_key ssl/XX.cn.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 15m;
ssl_prefer_server_ciphers on;
location / {
root html/mpis;
index index.html;
}
location /cc/web/ {
proxy_pass http://0.0.0.0:10002/mpis/web/;
}
location /cc/base/ {
proxy_pass http://0.0.0.0:10001/mpis/base/;
}
location /hxdlhd {
proxy_pass http://www.XXXX.cn:81/hxdlhd/login/goLogin;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
}
location /hxdlhd/ {
proxy_pass http://0.0.0.0:8080/hxdlhd/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# 配置此属性即可完成 http重定向至https
proxy_redirect http:// https://;
}
location ~* ^/WW_verify_h3vxzGabbQ7SLsWm.txt {
default_type text/html;
alias F://nginx-1.21.6/html/WW_verify_h3vxzGabbQ7SLsWm.txt;
}
}