【问题标题】:how to redirect http to https in nginx docker elastic beanstalk如何在 nginx docker elastic beanstalk中将http重定向到https
【发布时间】:2017-02-04 14:53:36
【问题描述】:

我在 docker elastic beanstalk 中托管了 django 应用程序,它使用 nginx。对于 SSL,我使用的是 aws 证书。 要将 http 重定向到 https,我尝试在 docker 容器内使用 nginx 使用“x_forwarded_proto”,但出现 502 错误。这是 nginx 配置:

server {

listen      80 default_server;

server_name www.example.com; 

access_log /home/docker/logs/nginx-access.log;
error_log /home/docker/logs/nginx-error.log;


if ($host !~* ^(www.example.com|example.com)$ ) {
    return 444;
}

if ( $http_x_forwarded_proto != 'https' ) {
return 301 https://$host$request_uri;
}

location / {
    uwsgi_pass unix:/var/sockets/api.sock;
    include    /home/docker/server/uwsgi_params; #
  }  
}

谁能提出更好的解决方案。

【问题讨论】:

    标签: docker nginx https


    【解决方案1】:

    找到了解决办法,加一下

    if ( $http_x_forwarded_proto != 'https' ) {
    return 301 https://$host$request_uri;
    }
    

    到eb实例的nginx配置。

    【讨论】:

      【解决方案2】:

      这确实是一个 nginx 问题(添加正确的标签)。

      您的配置似乎很复杂。而是从这个开始。这就是我用来将 http 端口 80 流量重定向到 TLS/SSL 端口 443 流量的方法。

      access_log /home/docker/logs/nginx-access.log;
      error_log /home/docker/logs/nginx-error.log;
      
      server {
          listen 80;
          server_name www.example.com;
      
          return 301 https://$host$request_uri;
      }
      
      server {
          listen      443 ssl;
          server_name www.example.com;
      
          location / {
              root   /usr/share/nginx/html;
              index  index.html;
          }
      }
      

      【讨论】:

      • 通常我们可以使用它来将http重定向到https。但我使用的是来自 AWS Certificate Manager 的 ssl 证书。所以我无法在 nignx 配置中定义 ssl 证书位置,这会导致错误。我关注了这个博客uvd.co.uk/blog/using-aws-certificate-manager-nginx
      猜你喜欢
      • 2018-01-07
      • 2019-01-24
      • 2016-06-19
      • 2015-07-09
      • 2018-09-30
      • 2023-03-10
      • 2013-12-08
      • 2018-01-01
      • 2021-02-23
      相关资源
      最近更新 更多