简体   繁体   English

如何通过多个站点将非www重定向到www? (Nginx和Varnish)

[英]How to Redirect a non-www to www with Mutiple Sites? (Nginx and Varnish)

First I want to clarify that I searched a lot about this issue, but I haven't found a solution. 首先,我想澄清一下,我对此问题进行了很多搜索,但是还没有找到解决方案。

I have 2 Wordpress sites configured under the same Nginx (port 8080) and Varnish (port 80). 我在相同的Nginx(端口8080)和Varnish(端口80)下配置了2个Wordpress站点。

This is my actual setting: 这是我的实际设置:

map $http_host $blogid {
 default 0;
 www.site1.com 1;
 www.site2.com 2;
}
server {
 listen 8080;
 server_name www.site1.com site1.com;
 root /var/www/site1.com;
 ...
}
server {
 listen 8080;
 server_name www.site2.com site2.com;
 root /var/www/site2.com;
 ...
}

What I want to do, is to configure a redirect for site2 from a non-www to a www. 我想做的是配置site2从非www到www的重定向。 Example: http://site2.com -> http://www.site2.com 范例: http : //site2.com- > http://www.site2.com

I added another ' server ' configuration with the redirect, and let just ' www.site2.com ' in the other ' server_name '. 我在重定向中添加了另一个“ 服务器 ”配置,在另一个“ server_name ”中仅设置了“ www.site2.com ”。

map $http_host $blogid {
 default 0;
 www.site1.com 1;
 www.site2.com 2;
}
server {
 server_name www.site1.com site1.com;
 root /var/www/site1.com;
 ...
}
server {
    listen 8080;
    server_name site2.com;
    return 301 http://www.site2.com$request_uri;
}
server {
 server_name www.site2.com;
 root /var/www/site2.com;
 ...
}

After changed with the configuration above and restarting Nginx, what happened is when accessing " http://site2.com " (without www) it is loading the content from " http://site1.com " (url continues site2.com without www). 在使用上述配置进行更改并重新启动Nginx之后,发生的事情是访问“ http://site2.com ”(不带www)时,它正在从“ http://site1.com ”加载内容(URL继续site2.com而不万维网)。 Acessing " http://www.site2.com " shows the right content. 访问“ http://www.site2.com ”显示正确的内容。

What I'm doing wrong? 我做错了什么?

I think that redirect is not working because I tried to redirect to Google.com, but it didn't redirect. 我认为重定向无法正常工作,因为我尝试重定向到Google.com,但没有重定向。 It keeps loading "site1.com" content inside "site2.com". 它一直将“ site1.com”内容加载到“ site2.com”中。

return 301 http://www.google.com

I tried this code below, but with the same result: 我在下面尝试了此代码,但结果相同:

rewrite ^(.*) http://www.site2.com$1 permanent;

My complete Nginx configuration : http://codepad.org/TfPHS0jH 我完整的Nginx配置http : //codepad.org/TfPHS0jH

Your site isn't being served by Nginx but by Varnish. 您的网站不是由Nginx服务,而是由Varnish服务。

When you navigate to any of http://www.siteX.com or http://siteX.com , you are going to http://www.siteX.com:80 or http://siteX.com:80 and according to your explanation, Varnish listens on Port 80, not Nginx. 当您导航到http://www.siteX.comhttp://siteX.com时 ,您将转到http://www.siteX.com:80http://siteX.com:80,然后根据您的解释,Varnish监听端口80,而不是Nginx。

There is no real reason to ever have to use the two together (despite what you may have read on some websites) and you should get rid or one or the other and focus on the one you decide to keep. 没有真正的理由必须将两者一起使用(尽管您可能在某些网站上已经读过),并且您应该摆脱一个或另一个,而专注于自己决定保留的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM