【问题标题】:How to force ssl with Google Cloud Load Balancer and nginx as a web server如何使用 Google Cloud Load Balancer 和 nginx 作为 Web 服务器强制使用 ssl
【发布时间】:2017-11-22 12:51:03
【问题描述】:

我们在 Google Cloud HTTP(S) 负载均衡器后面有 4 台带有 php 应用程序和 nginx 的服务器。 我让服务器同时监听 http 和 https 连接。 问题是 - 我不能强制使用 ssl。 这是nginx配置:

server {
            listen 80;

            server_name domain.com;

            root /var/www/dev/public_html;
            index index.php index.html index.htm;

            port_in_redirect off;

            location / {
                    return 301 https://$server_name$request_uri;
            }

    }
    server {
        listen 443;

        ssl on;
        ssl_certificate /etc/nginx/ssl/ssl.crt;
        ssl_certificate_key /etc/nginx/ssl/ssl.key;
        ssl_prefer_server_ciphers on;

        root /var/www/production/public_html;
        index index.php index.html index.htm;

        server_name domain.com;

        location ~ /help {
            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_set_header X-Forwarded-Proto $http_x_forwarded_proto;
            proxy_redirect off;
            proxy_next_upstream error;
        }

        location / {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param   SCRIPT_FILENAME  $document_root/index.php;
            include fastcgi_params;
            proxy_buffering off;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Referer "";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
        }
}

那里还有几个位置,都具有相似的配置。 这个配置在没有 LB 的情况下在单个服务器上工作。并且不适用于LB。 请指教。

谢谢!

【问题讨论】:

    标签: http nginx https google-cloud-platform load-balancing


    【解决方案1】:

    可能有很多事情需要检查。也许 - 查看 HTTPS 负载均衡器的 SSL 证书 - 文档指出它“负载均衡器需要至少一个签名的 SSL 证书” - 请参阅 Setting Up HTTP(S) Load BalancingSSL Certificates

    【讨论】:

    • 负载均衡器级别存在 SSL。如果我手动输入exemple.com,HTTPS 工作正常。它没有将我从 http 重定向到 https 的问题。
    【解决方案2】:

    在网络服务器上使用此配置:

            if ($http_x_forwarded_proto = "http") { return 301 https://$server_name$request_uri; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-28
      • 2020-08-03
      • 2018-12-19
      • 2021-05-09
      • 2013-03-28
      • 2020-09-20
      • 1970-01-01
      • 2016-12-05
      相关资源
      最近更新 更多