NGINX SSL过于频繁地重定向

时间:2017-10-06 19:09:27

标签: node.js loops ssl nginx cloudflare

我正在努力使用NGINX并设置我的v-hosts。我正在尝试设置一个vhost,将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,因此我陷入了无休止的重定向循环。

我完全不知道我的错误在哪里。

每个VHost都在一个文件中。端口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

1 个答案:

答案 0 :(得分:1)

您需要使用以下配置

server {
listen              80;
server_name         example.com;
add_header          Strict-Transport-Security "max-age=31536000";
  location / {
    proxy_pass      http://localhost:2237;
    proxy_redirect  http://localhost:2237/ https://$host/;
  }
}

您正在使用cloudflare SSL并在cloudflare终止SSL。因此,您应该只是在端口80上侦听。您之前的配置是将端口80重定向回HTTPS并将请求发送到Cloudflare,然后发送到您的nginx端口80,从而创建无限循环