2017-10-06 141 views
0

我在努力與NGINX並設置我的v主機。我試圖設置一個虛擬主機重定向HTTP請求到HTTPS,然後到我的應用程序(當它是443)NGINX SSL過於頻繁地重定向

我的操作系統是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 /虛擬主機看起來像:

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 Server。我也使用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 
+0

運行'nginx -T'並驗證沒有額外的配置正在加載 –

+0

@TarunLalwani不,它只是配置 – dunklesToast

+0

嘗試在'proxy_pass'塊中添加'proxy_redirect http:// localhost:2237/https:// $ host /;'。我認爲你的nodejs應用程序正在做一個重定向,這可能導致這些重定向 –

回答

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,從而創建無限循環