我有以下设置,但当我 curl 负载均衡器时,会看到 302 Moved Temporarily。在浏览器中,它报告“重定向次数过多”:
- 1 个带 SSL 的负载均衡器
- 2 个 Web 服务器
我要求https://www.domain.com然后我得到了一个 302,其中包含相同的 URL(请参阅下面的编辑)。以下是服务器设置:
服务器 1 - IP 例如 1.1.1.1 Nginx 配置为负载均衡器,处理 SSL 并将 http 重定向到 https:
server_tokens off; # for security-by-obscurity: stop displaying nginx version
upstream www_backend {
ip_hash;
server 2.2.2.2:80;
server 3.3.3.3:80;
}
# this section is needed to proxy web-socket connections
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# HTTP
server {
listen 80;
server_name www.domain.com; # the domain on which we want to host the application.
# redirect non-SSL to SSL
location / {
rewrite ^ https://$server_name$request_uri? permanent;
}
}
server {
listen 443 ssl spdy;
server_name www.domain.com;
ssl on;
ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_certificate /etc/nginx/ssl/www.domain.com/server.pem; # full path to SSL certificate and CA certificate concatenated together
ssl_certificate_key /etc/nginx/ssl/www.domain.com/server.key; # full path to SSL key
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-R blah blah';
# config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
# to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000";
ssl_dhparam /etc/nginx/ssl/dhparams.pem;
location / {
proxy_pass http://www_backend;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_headers_hash_bucket_size 128;
}
}
Web 服务器 1 - IP 例如 2.2.2.2 Nginx 配置为代理,监听 80 端口并传递到 8080 端口:
server_tokens off; # for security-by-obscurity: stop displaying nginx version
# this section is needed to proxy web-socket connections
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# HTTP
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name www.domain.com;
root /var/www/nginx/html;
location /200.html {
rewrite ^ /200.html break;
}
# pass all requests to Meteor
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
proxy_set_header X-Forwarded-Proto $scheme;
proxy_headers_hash_bucket_size 128;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # allow websockets
proxy_set_header Connection $connection_upgrade;
# this setting allows the browser to cache the application in a way compatible with Meteor
# on every application update the name of CSS and JS file is different, so they can be cache infinitely (here: 30 days)
# the root path (/) MUST NOT be cached
if ($uri != '/') {
expires 30d;
}
}
error_page 500 502 503 504 /50x.html;
location /50x.html{
root /var/www/nginx/html;
}
}
我已经添加了一些静态 HTML 页面来测试是否已访问 Web 服务器。
Meteor 应用程序,监听本地端口 8080:
export PORT=8080
# this allows Meteor to figure out correct IP address of visitors
export HTTP_FORWARDED_COUNT=1
# The domain name as configured previously as server_name in nginx
export ROOT_URL=http://www.domain.com
#Start Meteor
exec node /var/www/blah/main.js >> /var/log/blah/meteor.log
Web 服务器 2 的配置类似。
如果我 ssh 进入 web1 并 curl meteo,它会按预期响应:curl http:127.0.0.1:8080
如果我从我的开发机器上 curl 测试 HTML 页面,它会按预期响应:curlhttps://www.domain.com/200.html
但是,如果我从我的开发中 curl 根,它什么也不会响应: curlhttps://www.domain.com
打https://www.domain.comChrome 中显示“重定向次数过多”错误。由于我使用的是 https,而不是 http,因此我不明白这些重定向是如何导致的。
我获得了 Qualsys 的 SSL A+ 评级。我已在所有服务器上运行 sudo nginx -t,并且 nginx 文件正确。
我已经从 /etc/nginx/sites-enabled/www.domain.com -> /etc/nginx/sites-available/www.domain.com 创建了一个符号链接
我已经重新加载了 Nginx:sudo nginx -s reload
编辑 顺便说一句 - 我之前按照另一位 ServerFault 用户的建议删除了 /etc/nginx/sites-enabled/default 和 /etc/nginx/sites-available/default。
编辑 以下是 curl 的详细响应:
* About to connect() to www.domain.com port 443 (#0)
* Trying 1.1.1.1...
* connected
* Connected to www.domain.com (1.1.1.1) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using DHE-RSA-AES128-SHA
* Server certificate:
* subject: CN=www.domain.com
* start date: 2016-03-14 00:00:00 GMT
* expire date: 2019-03-14 23:59:59 GMT
* subjectAltName: www.domain.com matched
* issuer: C=US; O=GeoTrust Inc.; CN=Blah Blah
* SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.domain.com
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx
< Date: Mon, 28 Mar 2016 05:52:35 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: https://www.domain.com/
< Strict-Transport-Security: max-age=31536000
<
* Connection #0 to host www.domain.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
我检查了 Web 服务器上的 Nginx 访问日志,似乎可以确认是 Web 服务器发送了 302,而不是负载均衡器:
[28/Mar/2016:01:56:28 -0500] "GET / HTTP/1.1" 302 5 "-" "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5"
因此,总而言之,负载均衡器似乎成功地将 SSL 调用从 443 传递到 Web 服务器 80。Meteor 服务在 Web 1&2 上运行,并成功响应本地端口 8080 请求。Web 服务器代理似乎没有将端口 80 请求传递到端口 8080。
答案1
在 Web 服务器上,我更改了方案行以强制使用 https:
proxy_set_header X-Forwarded-Proto https;
sudo reboot
并且它成功了。