nginx http到https会导致重定向太多

时间:2015-08-31 17:40:57

标签: redirect ssl nginx https ssl-certificate

以下nginx设置适用于https://DNSNAMEhttp://DNSNAME

upstream prod {
    server server_ip_1;
    server server_ip_2;
}

server {
    listen 80;
    listen 443 ssl;

    root /usr/share/nginx/www;
    index index.html index.htm;

    server_name localhost;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    location / {
        proxy_pass        https://prod;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header  Host $host;
    }
}

但我需要始终将域设置为https://并拥有DNSname init。所以当我尝试重定向如下。它导致过多的重定向并且无法加载页面。任何人都请帮助解决这个问题。

server {
  listen 80;
  server_name DNSName;
  return 301 https://$server_name$request_uri;
}

1 个答案:

答案 0 :(得分:0)

我将80和443分为两个单独的服务,类似。

upstream prod {
    server server_ip_1;
    server server_ip_2;
}

server {
    listen    80;
    server_name  localhost;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;

    root /usr/share/nginx/www;
    index index.html index.htm;

    server_name localhost;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    location / {
        proxy_pass        https://prod;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header  Host $host;
    }
}

对我有用。谢谢!