【发布时间】:2019-12-06 23:41:39
【问题描述】:
我在 Elastic Beanstalk 中设置了一个网站,但是当我在浏览器中输入 website.com URL 时,它会自动将我定向到 https//website.com 并且缺少冒号...如果我添加 www.website,它就可以工作。 com 虽然...或者如果我输入https://www.website.com。
.ebextensions/prod01.config 文件的内容
files:
/etc/nginx/conf.d/proxy.conf:
owner: root
group: root
mode: "000644"
content: |
# Elastic Beanstalk Managed
# Elastic Beanstalk managed configuration file
# Some configuration of nginx can be by placing files in /etc/nginx/conf.d
# using Configuration Files.
# http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
server_name website.com;
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 != "https") {
set $redirect 1;
}
if ($http_user_agent ~* "ELB-HealthChecker") {
set $redirect 0;
}
if ($redirect = 1) {
return 301 https://www.heyants.com$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;
}
}
server {
listen 8080;
server_name www.website.com;
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 != "https") {
set $redirect 1;
}
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;
}
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json 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"
更新
我已更新为使用下面提供的 AWS 参考之一;但是重定向的一种情况仍然不起作用;我希望通过这次明确的更新,可以找到真正适用于所有人的规范解决方案。
- website.com 成功重定向到 https://www.website.com
- www.website.com 成功重定向到 https://www.website.com
- https://www.website.com 成功重定向到 https://www.website.com
- http://www.website.com 成功重定向到 https://website.com
- http://website.com 无法重定向到 https://www.website.com
- https://website.com 无法重定向到 https://www.website.com;它会将他们带到 https//www.website.com(缺少冒号)
【问题讨论】:
-
你能进入服务器看看 nginx 文件长什么样吗?您还可以在更新后添加
nginx -T命令,它将转储文件的所有内容以确保它是否正在更新 -
你可以试试这个方法吗? github.com/awsdocs/elastic-beanstalk-samples/blob/master/…。不要使用
=,而是使用!=来检查http标头 -
使用nginx而不是应用负载均衡器是否有原因,是单实例环境吗?
标签: amazon-web-services nginx amazon-elastic-beanstalk