Nginx 域名重定向 [英] NGinx Domain name redirects

查看:63
本文介绍了Nginx 域名重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 xyz.co 的网站,我还有其他具有相同前缀的域名,例如 xyz.com、xyz.it、xyz.co.it

Lets say I have a website named xyz.co, I also have other domain names with the same prefix like xyz.com, xyz.it, xyz.co.it

现在nginx在80端口的nginx.conf中的server_name xyz.co工作正常我希望所有其他域都重定向到xyz.co我也希望上面的www.*版本重定向到xyz.co.我怎样才能得到这个?这是 nginx 网络服务器级别的变化吗?或者我需要在 DNS 中进行此更改?

Right now nginx works fine with server_name xyz.co in nginx.conf in port 80 I would want all the other domains to redirect to xyz.co also I would want www.* versions of the above to redirect to xyz.co. How can I get this? Is this nginx webserver level changes? or I need to make this changes in DNS?

更新:我在 nginx.conf 中试过这个,但没有用...

UPDATE: I tried this in nginx.conf but no use...

server
{
listen 80;
server_name xyz.co xyz.com, xyz.it, xyz.co.it;
rewrite ^/(.*) http://xyz.co permanent;
}

我第一次尝试在 ServerFault 中发布这个问题,但没有回应 - https://serverfault.com/问题/453472/nginx-domain-name-redirects

I first tried posting this question in ServerFault but no response there - https://serverfault.com/questions/453472/nginx-domain-name-redirects

推荐答案

为所有需要重定向的域名添加一个服务器块.像这样:

add one server block for all the domain names that need to be redirected. like this:

server {
    listen 80;
    server_name xyz.com, xyz.it, xyz.co.it;
    rewrite ^ http://xyz.co$request_uri permanent;
}

以及 xyz.co 域的另一个服务器块:

and another server block for the xyz.co domain:

server {
    listen 80;
    server_name xyz.co;

    #other settings
}

这样,当您转到需要重定向的域名之一时,nginx 将简单地重定向到 xyz.co 并移动到另一个服务器块,您可以在其中添加所有设置(根文件夹、位置块等)

this way when you go to one of the domain names that need to be redirected nginx will simply redirect to xyz.co and move into the other server block where you can add all your settings (rootfolder, location blocks, etc)

这篇关于Nginx 域名重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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