非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx

编程入门 行业动态 更新时间:2024-10-27 22:23:33
本文介绍了非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个在 example 上运行的应用程序,现在我想将所有流量重定向到 www.example,因为我们正在与 Akamai 的 CDN 合作开发您的网站.我的域停在 Route53 中,添加了 Elastic Load Balancer 的 CNAME 的 CNAME 指向 *.example,我正在使用以下配置运行 nginx Web 服务器.如果我使用此配置,ELB 会抛出实例,现在 domain 工作正常,www.domain 正常,但没有发生重定向.

I have an app running on example and now I wanna redirect all the traffic to the www.example since we are collaborating with Akamai's CDN for your website. My domain is parked in Route53, added the CNAME of Elastic Load Balancer's CNAME to pointing to *.example and I am running nginx web server with the following configuration. If I use this configuration ELB is throwing the instance out, now domain works fine and www.domain fine, but redirecting is not happening.

server { listen 80; server_name example; # rewrite ^ www.example$request_uri? permanent; root /home/ubuntu/apps/st/public; passenger_enabled on; }

但我无法实现重定向.

还尝试添加值为负载均衡器cname的PTR.

Also tried to add the PTR whose value is the cname of the load balancer.

尝试更改nginx配置,如您所见

Tried to change the nginx configuration, as you can see

重写 ^ www.example$request_uri?永久;

即使这样也行不通.

一旦我添加了上面的行,应用程序实例就会消失,ELB 会从堆栈中抛出.

As soon as I add the above line, the app instance goes and ELB throws out of the stack.

解决办法是什么,我从3-4天开始就一直在努力实现这一目标,现在它变成了噩梦,请帮助我.我还在 aws 论坛中发布了帖子和主题,但没有任何帮助.

What is the solution, I have been trying to achieve this since 3-4 days, its become a night mare now, kindly help me up. I have also put posts and threads in aws forums, nothing is helping as such.

推荐答案

我在重定向时遇到了同样的问题(使用与此处显示的相同的 nginx conf 代码).

I had the same issue with the redirect (using same nginx conf code as shown here).

然后,我将我的重定向配置作为最后一个 server{} 块(在我的 domain 配置文件的末尾),并且 ELB 能够再次找到实例.

Then, I put my redirect configs as the last server{} block (at the end of my domain config file), and the ELB was able to find the instances again.

我没有深入研究它,但似乎 vhost 处理是按顺序完成的,所以当 ELB 命中我服务器的 IP 地址时,它落在第一个定义的 server{} 块上,在我的情况下是重定向.Nginx 抛出 301 返回码,ELB 吓坏了,因为它不是 200.

I have not looked more into it, but it seems that vhost processing is done in order, so when ELB hits the IP address of my servers, it lands on the first defined server{} block, which in my case was the redirect. Nginx throws a 301 return code, and ELB freaks out since its not 200.

不过只是猜测.

所以,我的配置(真正的虚拟主机在上面,重定向在最后):

So, my config (real vhost on top, redirect is last):

少/etc/nginx/sites-enabled/domain

less /etc/nginx/sites-enabled/domain

server { listen 80; server_name domain; ..... } server { listen 80; server_name www.domain; return 301 domain$request_uri; }

更多推荐

非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx

本文发布于:2023-11-01 20:46:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:均衡器   负载   弹性   www   AWS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!