我知道以前有人问过这个问题,但似乎没有什么对我有用。我尝试了多种不同的方法,例如这些问题中描述的答案:
How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS?
Redirecting EC2 elb from http to https
它们似乎都不起作用。我是 aws noob,所以我不完全确定编辑配置文件的工作原理 - 或者我是否做错了什么。
我的设置如下:
我当前的 .ebextensions 文件夹中的 nginx.config 文件(从 this article 获得):
files:
"/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" :
mode: "000755"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
set $fixedWWW '';
set $needRedir 0;
# nginx does not allow nested if statements
# check and decide on adding www prefix
if ($host !~* ^www(.*)) {
set $fixedWWW 'www.';
set $needRedir 1;
}
# what about that https? the traffic is all http right now
# but elastic load balancer tells us about the original scheme
# using $http_x_forwarded_proto variable
if ($http_x_forwarded_proto != 'https') {
set $needRedir 1;
}
# ok, so whats the verdict, do we need to redirect?
if ($needRedir = 1) {
rewrite ^(.*) https://$fixedWWW$host$1 redirect;
}
location / {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
gzip on;
}
但这似乎没有任何作用。我已经没有想法了。我不确定我是否错过了一个步骤或什么,但我不知道该怎么做。作为一种解决方法,我让我的 angularjs 前端重定向非 https 请求,但这太 hacky 并且一些 DOM 在重定向之前呈现,我想在负载均衡器处重定向 - 它应该重定向的地方。
最佳答案
直接更新/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf"非常困难。我发现了这个: https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/security-configuration/https-redirect/nodejs/https-redirect-nodejs.config ,它可以让你设置重定向,但这会改变我的其他配置文件太多。最好的方法是在 .ebextensions 文件夹中创建一个 redirect.config 文件:
container_commands:
https_redirect:
command: |
sed -i '/location \/ {/i \
set $redirect 0;\
if ($http_x_forwarded_proto != "https") {\
set $redirect 1;\
}\
if ($http_user_agent ~* "ELB-HealthChecker") {\
set $redirect 0;\
}\
if ($redirect = 1) {\
return 301 https://$host$request_uri;\
}\
' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf```
关于amazon-web-services - Elastic Beanstalk Http 重定向到 Https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817362/