Nginx 非 WWW 重定向到 WWW 记录问题

Nginx 非 WWW 重定向到 WWW 记录问题

我正在通过 AWS ElasticBeanstalk 应用程序运行 Nginx,并通过负载均衡器启用 Https,并强制对 www 进行 Https 重定向,但似乎我的 Route53 记录或 Nginx 服务器块存在问题,因为当我尝试访问根域(非 www)时,它会显示 godaddy.com 停放页面,而不是重定向到https://www.GoDaddy 是原始 DNS,但我将记录迁移到了 Route53。我的服务器块配置看起来正确,但这个错误很可能是由于记录引起的,还是与我的服务器配置有关?

Elastic Beanstalk Nginx 修改:

files:
  "/etc/nginx/conf.d/02_www_redirect.conf":
    owner: root
    group: root
    mode: "000644"
    content: |
      server {
        listen 80;   #listen for all the HTTP requests
        server_name example.com www.example.com;
        return 301 https://www.example.com$request_uri;
      }

container_commands:
    01_reload_nginx:
        command: "sudo service nginx reload"

Route53 记录集:

example.com. A ALIAS www.example.com. (Id's)
www.example.com. A ALIAS example-eb-application.us-east-1.elasticbeanstalk.com. (Id's)

答案1

请查看旧帖子https://stackoverflow.com/questions/41669583/redirect-non-www-to-www-with-aws-elastic-beanstalk

或者进行以下更改

if ($http_x_forwarded_proto != 'https') {
  rewrite ^(.*) https://$host$1 redirect;
}

相关内容