[英]Configuring nginx to work with WordPress Multisite - 301 redirect
I am having issues with trying to get a WordPress Multisite (using subdomains) running on nginx. 我在尝试让nginx上运行WordPress多站点(使用子域)时遇到问题。
Here is my ideal set-up: 这是我理想的设置:
domain.com
sub1.domain.com
sub2.domain.com
sub3.domain.com
Any requests to www.domain.com
should be 301'd to domain.com
(my understanding is that WordPress strips out www anyways?) 对
www.domain.com
任何请求都应该对domain.com
进行301处理(我的理解是WordPress还是会剥离www吗?)
I am not wanting to access any of the subdomains by any other means, eg domain2.com
would direct a user to sub2.domain.com
- this isn't what I want. 我不想通过任何其他方式访问任何子域,例如
domain2.com
会将用户sub2.domain.com
到sub2.domain.com
这不是我想要的。
I have followed this DigitalOcean tutorial and everything has worked fine with regular WordPress installs. 我遵循了此DigitalOcean教程 ,并且在常规WordPress安装中一切正常。
My local hosts file is set up as follows: 我的本地主机文件设置如下:
XXX.XXX.XXX.XXX domain.com
XXX.XXX.XXX.XXX sub1.domain.com
etc... 等等...
My issue is that whenever I attempt to visit either domain.com
or sub*.domain.com
, I am redirected to a completely different site on my server, the one which just so happens to be the top of the list in sites-enabled
. 我的问题是,每当我尝试访问
domain.com
或sub*.domain.com
,我都会被重定向到服务器上一个完全不同的站点,而该站点恰好是sites-enabled
的列表的顶部。 On closer inspect, when checking the Network tab in Chrome, I can see that my request for domain.com
or sub*.domain.com
is 301 redirected to the aforementioned unrelated site. 仔细检查后,当检查Chrome中的“网络”标签时,我可以看到我对
domain.com
或sub*.domain.com
请求被重定向到了上述无关站点301。
I have flushed my local Chrome cache, am constantly using Incognito mode and I just cannot shake this redirect. 我已经刷新了本地Chrome缓存,一直使用隐身模式,但无法撼动此重定向。 I'm even using Firefox as I rarely use it so wouldn't expect any caching whatsoever for
domain.com
我什至都在使用Firefox,因为我很少使用它,因此不会为
domain.com
缓存任何内容
I may be completely off the mark here and it could be nothing to do with the browser at all but rather the server and nginx configuration. 我可能在这里完全不合时宜,它与浏览器完全无关,而与服务器和nginx配置无关。 Something is forcing a 301 and I don't know what.
强制301发生故障,我不知道该怎么办。
It seems that this plugin comes up a lot in discussion but as far as I can tell, this is only applicable when wanting to map a regular domain to a subdomain, which isn't what I want. 似乎该插件在讨论中出现了很多问题,但据我所知,这仅适用于要将常规域映射到子域的情况,而这并不是我想要的。
Can anyone please offer any advice/solutions as to how I can configure so everything is routed correctly? 任何人都可以就如何配置以便正确路由所有内容提供任何建议/解决方案吗? My local environment runs a LAMP stack so I have had this working and I have altered the
wp-config.php
file to reflect the new 'live' domain (remember, I have not configured DNS, I have just hacked around in etc/hosts
. 我的本地环境运行一个LAMP堆栈,因此我已经进行了此工作,并且我更改了
wp-config.php
文件以反映新的“活动”域(请记住,我没有配置DNS,我只是在etc/hosts
乱砍。
Any help much appreciated as always :) 任何帮助一如既往地感激:)
The only thing that avoids your request to be served by the "fallback", default vhost (that is, the "first" there) is the server_name
directive. 唯一避免您的请求由“后备”(默认vhost)(即那里的“第一个”)服务的请求是
server_name
指令。
Just by specifying location
blocks in multiple server
blocks with the same (or no) server_name
won't make them magically match. 仅通过在多个具有相同(或不相同)
server_name
server
块中指定location
块,就不会使它们神奇地匹配。
You can however use regular expression server names. 但是,您可以使用正则表达式服务器名称。
For the specific case with only a single domain, you can use 对于只有一个域的特定情况,您可以使用
server_name *.whatever.com;
then match location
with return 301 http://whatever.com;
然后将
location
与return 301 http://whatever.com;
匹配return 301 http://whatever.com;
for subdomains only. 仅适用于子域。 That way you will not need regular expression server names.
这样,您将不需要正则表达式服务器名称。
Note that you should have server_name for the default server, like 请注意,默认服务器应具有server_name,例如
server_name "_";
So in the end I decided to build the multisite from scratch in my nginx environment as opposed to trying to take what worked in Apache and re-configure for nginx. 因此,最终,我决定在我的nginx环境中从头开始构建多站点,而不是尝试采用Apache中有效的方法并为nginx重新配置。 As far as I can tell, I had updated everything that you would expect to have to do so from Apache to nginx as well as updating local domains to live ones in the database and wp-config.
据我所知,我已经完成了从Apache到nginx所需的所有操作,并且将本地域更新为数据库和wp-config中的活动域。
While this approach didn't strictly 'fix' my issue, I do feel like I've learned a valuable lesson here - sometimes starting from scratch is better than trying to make something fit! 尽管这种方法并不能严格“解决”我的问题,但我确实觉得自己在这里学到了宝贵的经验-有时从零开始比尝试做一些合适的事情更好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.