【问题标题】:NGINX force SSL for all but health check file?NGINX对除健康检查文件之外的所有文件强制使用SSL?
【发布时间】:2017-03-28 17:35:26
【问题描述】:

我有一个 Rails 应用程序,在 AWS ELB 后面有一个 NGINX 反向代理。我正在终止 ELB 上的 SSL,并且我已将 NGINX 配置为强制对 HTTP 的任何尝试重写为 HTTPS。此设置工作正常,但我也通过 ECS 为站点提供服务,并且由于 ELB 健康检查在 HTTP 端口 80 上,当它获得重定向并返回 301 时,ELB 健康检查失败并且实例被注销。

如何设置 NGINX 以通过 HTTPS 发送除健康检查文件之外的所有文件?

这是我来自 nginx.conf 的服务器块:

server {
        listen 80;

        server_name localhost;

        root /var/www/html;

        location ~ ^elbcheck\.html$ {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_pass http://rails_app;
          break;
        }

        location / {
          proxy_redirect off;
          proxy_next_upstream error;

          if ($http_x_forwarded_proto != "https") {
            rewrite ^ https://$host$request_uri? permanent;
          }

          try_files $uri $uri/ @proxy;
        }

        location ~* \.(jpg|jpeg|svg|png|gif|ico|css|js|eot|woff|woff2|map)$ {
            proxy_cache APP;
            proxy_cache_valid 200 1d;
            proxy_cache_valid 404 5m;
            proxy_ignore_headers "Cache-Control";
            expires 1d;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            add_header X-Cache-Status $upstream_cache_status;

            proxy_pass http://rails_app;
        }

        location @proxy {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;

            proxy_pass  http://rails_app;
        }
    }

【问题讨论】:

    标签: nginx amazon-elb


    【解决方案1】:

    我遇到了同样的问题,在互联网的某个地方找到了这个答案(不再有来源了,这是不久前的事了)

    server {
        listen 80;
    
        set $redirect_to_https 0;
        if ($http_x_forwarded_proto != 'https') {
            set $redirect_to_https 1;
        }
    
        if ($request_uri = '/status') {
            set $redirect_to_https 0;
        }
    
        if ($redirect_to_https = 1) {
            return 301 https://$host$request_uri;
        }
    
        ...
    }
    

    【讨论】:

    • 我只会添加 access_log off;以避免记录来自负载均衡器的每个调用。
    【解决方案2】:

    this post 找到了一个非常有效的简单答案。这是@ceejayoz 的建议:

    server {
      location /elb-status {
        access_log off;
        return 200;
      }
    }
    

    似乎工作正常——ECS 没有因为健康检查失败而终止我的服务。

    【讨论】:

    • 这种方法的问题是 nginx 将响应健康检查,而不是 rails 应用程序,因此它绕过了健康检查的目标之一:删除 Rails 服务器不运行的不健康实例回应。您可能会遇到 nginx 响应但 rails 应用程序没有响应的情况,您的用户仍将被重定向到此错误实例。
    猜你喜欢
    • 1970-01-01
    • 2010-11-14
    • 2018-06-15
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多