【发布时间】:2017-10-06 19:09:27
【问题描述】:
我正在努力使用 NGINX 并设置我的 v-hosts。我正在尝试设置一个虚拟主机,将 HTTP 请求重定向到 HTTPS,然后重定向到我的应用程序(当它是 443 时)
我的操作系统是 Ubuntu 16.04,我使用的是 NGINX 1.10.3。
nginx.conf 看起来像这样(它大部分是默认的):
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
我的 ServerBlocks / VHosts 看起来像这样:
server {
listen 443 ssl;
server_name xxx.com;
# Prevent MITM
add_header Strict-Transport-Security "max-age=31536000";
ssl_certificate "/etc/nginx/ssl/xxx.com.pem";
ssl_certificate_key "/etc/nginx/ssl/xxx.com.key";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://localhost:2237;
}
}
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
现在的问题是,如果我使用 HTTP 或 HTTPS,它会尝试将我重定向到 HTTPS,因此我陷入了无休止的重定向循环。
我完全不知道我的错误在哪里。
每个虚拟主机都在一个文件中。端口 2237 上的应用程序是 nodeJS Express 服务器。我也在使用 Cloudflare(我从他们那里获得了我的 SSL 证书)
编辑:
curl -I 的输出是:
$ curl -I https://example.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 06 Oct 2017 19:42:19 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d827df762e20a4e321b92b34bd15546621507318939; expires=Sat, 06-Oct-18 19:42:19 GMT; path=/; domain=.example.com; HttpOnly
Location: https://example.com/
Server: cloudflare-nginx
CF-RAY: 3a9b1a6a4e4564d5-FRA
【问题讨论】:
-
运行
nginx -T并验证没有正在加载的其他配置 -
@TarunLalwani 不,它只是那个配置
-
尝试在您的
proxy_pass块中添加proxy_redirect http://localhost:2237/ https://$host/;。我认为您的 nodejs 应用程序正在执行重定向,这可能会导致这些重定向 -
@TarunLalwani 我将该行粘贴在
proxy_pass部分下,但仍在位置内容中,但它不起作用:( -
你能把
curl -v https://<url>的输出贴出来
标签: node.js loops ssl nginx cloudflare