Elasticbeanstalk – 使用Nginx在Docker容器上强制HTTPs

我有一个使用Nginx在Elasticbeanstalk上运行React环境的单容器Docker。 我将一个子域名指向ELB URL,并且如果您访问该子域名(即,您键入subdomain.domain.com并将其redirect到HTTPS),则需要强制执行HTTPSredirect。

现在,如果我访问默认的ELB URL( something.eu-central-1.elasticbeanstalk.com ),它将被redirect到HTTPS。 但是,我希望我的自定义域名 (这是停放在其他地方,但指向某些东西eu-centralblabla与CNAME) 也被迫使用HTTPS ,但它不会发生。 它允许定期的HTTP请求。

我试了几个指南,并遵循AWS文档,但我似乎无法强制它redirect到我的自定义子域上的HTTPS。

这些是我的文件:

/.ebextensions文件夹

HTTP-instance.config

files: /etc/nginx/conf.d/https.conf: mode: "000644" owner: root group: root content: | # HTTPS Server server { listen 443; server_name localhost; ssl on; ssl_certificate /etc/pki/tls/certs/server.crt; ssl_certificate_key /etc/pki/tls/certs/server.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; ssl_prefer_server_ciphers on; location / { proxy_pass http://docker; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #SSL CRT and KEY below 

HTTPS的实例single.config

 Resources: sslSecurityGroupIngress: Type: AWS::EC2::SecurityGroupIngress Properties: GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]} IpProtocol: tcp ToPort: 443 FromPort: 443 CidrIp: 0.0.0.0/0 

/ nginx文件夹

default.conf

 server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html?/$request_uri; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; error_page 500 504 /500.html; error_page 502 /502.html; error_page 503 /503.html; client_max_body_size 4G; keepalive_timeout 10; location ~ ^/(favicon|static)/ { gzip_static on; expires max; add_header Cache-Control public; # add_header Last-Modified ""; # add_header ETag ""; open_file_cache max=1000 inactive=500s; open_file_cache_valid 600s; open_file_cache_errors on; break; } } 

我究竟做错了什么? 谢谢你的帮助!

你应该能够在你的nginxconfiguration中通过在服务器上下文中添加这个来pipe理它:

 set $redirect_to_https 0; if ($http_x_forwarded_proto != 'https') { set $redirect_to_https 1; } if ($redirect_to_https = 1) { rewrite ^ https://$host$request_uri? permanent; } 

或者是这个效果