非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx [英] non www to www using AWS Elastic Load balancer and Nginx

查看:33
本文介绍了非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app running on example.com and now I wanna redirect all the traffic to the www.example.com 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.com and I am running nginx web server with the following configuration. If I use this configuration ELB is throwing the instance out, now domain.com works fine and www.domain.com fine, but redirecting is not happening.

server {
        listen 80;
        server_name example.com;
#       rewrite ^    http://www.example.com$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

重写 ^ http://www.example.com$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.com 配置文件的末尾),并且 ELB 能够再次找到实例.

Then, I put my redirect configs as the last server{} block (at the end of my domain.com 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.com

less /etc/nginx/sites-enabled/domain.com

server {
    listen 80;
    server_name domain.com;
    .....
}

server {
    listen       80;
    server_name  www.domain.com;
    return       301 http://domain.com$request_uri;
}

这篇关于非 www 到 www 使用 AWS 弹性负载均衡器和 Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆