我在beanstalk上有一个经典负载均衡器,并配置了nginx实例。我想把http重定向到https请求。
我设置我的负载均衡器监听器重定向到80端口到它的实例。
我在.ebextensionsnginx_config.config中创建了一个文件,在这个文件中,我设置了重定向,也过滤了健康检查。
请看下面的配置重写。
files:
/etc/nginx/conf.d/proxy.conf:
owner: root
group: root
mode: "000644"
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 80;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log main;
location / {
set $redirect 0;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
if ($http_user_agent ~* "ELB-HealthChecker") {
set $redirect 0;
}
if ($redirect = 1) {
return 301 https://$host$request_uri;
}
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;
}
location /health-check {
access_log off;
default_type text/plain;
return 200 ‘OK’;
}
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
/opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
owner: root
group: root
mode: "000755"
content: |
#!/bin/bash -xe
rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
if [[ -e /etc/init/nginx.conf ]] ; then
echo Using initctl to stop and start nginx
initctl stop nginx || true
initctl start nginx
else
echo Using service to stop and start nginx
service nginx stop
service nginx start
fi
container_commands:
removeconfig:
command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
但似乎什么都没发生,服务器还是没有重定向到https。好像我的配置被忽略了。这种情况下如何重定向到https?
所以按照我上面的建议。创建一个应用程序负载均衡器与2个监听器。
第1个监听器是一个443 HTTPS监听器,直接向你的目标组提供流量。
第2个监听器是一个80 HTTP监听器,它使用一个 重定向规则 的重定向到HTTPS。
这是最好的做法。
你可以让你的负载均衡器用ACM的证书监听443,然后把流量重定向到80端口?但强烈建议使用上面@mokugo-devops说的ALB。希望能帮到你。你也可以看看类似的问题 AWS EB - 将所有流量重定向到https。