我有一个节点应用程序,它带有两个服务器进程。一个提供我自己的应用程序,另一个提供API。我使用Nginx作为反向代理,通过443(和80)提供两者,并从人工端点向API发出代理请求。
我有一个nginx Docker容器的以下设置:
一般配置:
user nginx;
worker_processes 5;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
HTTP和HTTPS的特定服务器部分:
server {
listen 80;
server_name mydomain.de;
access_log /var/log/nginx/nodejs_project.log;
charset utf-8;
rewrite_log on;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.de;
access_log /var/log/nginx/nodejs_project.log;
charset utf-8;
rewrite_log on;
ssl_certificate /etc/nginx/bundle.pem;
ssl_certificate_key /etc/nginx/webserver.key;
location / {
proxy_pass http://mydomain:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /api {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://mydomain:3030;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
目标是自动将所有HTTP请求重定向到HTTPS。这就是当我卷曲页面时会发生什么:
→ curl -IL mydomain.de
HTTP/1.1 301 Moved Permanently
Server: nginx/1.9.15
Date: Sat, 21 May 2016 12:26:12 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://mydomain.de/
HTTP/1.1 302 Found
Server: nginx/1.9.15
Date: Sat, 21 May 2016 12:26:13 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 30
Connection: keep-alive
X-Powered-By: Express
Location: /steps/1
Vary: Accept, Accept-Encoding
HTTP/1.1 200 OK
Server: nginx/1.9.15
Date: Sat, 21 May 2016 12:26:13 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 39705
Connection: keep-alive
X-Powered-By: Express
ETag: W/"9b19-576TRcOW5QU6nLaobx28iw"
Vary: Accept-Encoding
这对我来说很好看。首先是从HTTP到HTTPS的301,然后是由应用程序本身触发的302。
这适用于大多数浏览器,但有些失败。当我尝试加载:mydomain.de
甚至http://mydomain.de
时,尤其是Windows上的Firefox,iOS上的Chrome以及其他一些操作系统根本无法执行任何操作。
证书很好。它不是自签名的,而是由RapidSSL发布的。捆绑包包括CA的根证书,中间证书以及按照文档顺序的我的服务器证书。正确重定向的浏览器不会抱怨它。当我通过https://mydomain.de
直接加载页面时,失败的浏览器不会抱怨证书。
我甚至尝试过添加:
add_header Strict-Transport-Security "max-age=63072000;
includeSubdomains;
preload";
配置,但它更糟糕。谁能告诉我,我的错误在哪里?