本文介绍了使用AWS Elastic Load Balancer和Nginx的非www到www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在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;
}

但是我无法实现重定向.

but I am not able to achieve the redirect.

还尝试添加其值为负载均衡器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

rewrite ^ http://www.example.com$request_uri? permanent;

即使那不起作用.

一旦添加以上行,该应用程序实例就会消失,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.

我没有进一步研究,但似乎虚拟主机处理是按顺序进行的,因此当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):

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

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

这篇关于使用AWS Elastic Load Balancer和Nginx的非www到www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:21