Nginx 域名重定向

编程入门 行业动态 更新时间:2024-10-26 16:27:44
本文介绍了Nginx 域名重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个名为 xyz.co 的网站,我还有其他具有相同前缀的域名,例如 xyz、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, 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, xyz.it, xyz.co.it; rewrite ^/(.*) xyz.co permanent; }

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

I first tried posting this question in ServerFault but no response there - serverfault/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, xyz.it, xyz.co.it; rewrite ^ 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 域名重定向

本文发布于:2023-11-01 19:49:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   域名   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!