如何使用 Nginx 重定向到不同的域?

发布于 2024-11-08 16:17:33 字数 136 浏览 0 评论 0原文

如何使用 Nginx 将 mydomain.example 和任何子域 *.mydomain.example 重定向到 www.a differentdomain.example

How can I redirect mydomain.example and any subdomain *.mydomain.example to www.adifferentdomain.example using Nginx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一口甜 2024-11-15 16:17:33

server_name 支持使用 .mydomain.example 语法进行后缀匹配:

server {
  server_name .mydomain.example;
  rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}

或在任何 0.9 版本上.1 或更高:

server {
  server_name .mydomain.example;
  return 301 http://www.adifferentdomain.example$request_uri;
}

server_name supports suffix matches using .mydomain.example syntax:

server {
  server_name .mydomain.example;
  rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}

or on any version 0.9.1 or higher:

server {
  server_name .mydomain.example;
  return 301 http://www.adifferentdomain.example$request_uri;
}
孤星 2024-11-15 16:17:33
server {
    server_name .mydomain.example;
    return 301 http://www.adifferentdomain.example$request_uri;
}

http://wiki.nginx.org/HttpRewriteModule#return

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

server {
    server_name .mydomain.example;
    return 301 http://www.adifferentdomain.example$request_uri;
}

http://wiki.nginx.org/HttpRewriteModule#return

and

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

余生共白头 2024-11-15 16:17:33

我正在为我的网站使用此代码

server {
        listen 80;
        listen 443;
        server_name  .domain.example;

        return 301 $scheme://newdomain.example$request_uri;
}

I'm using this code for my sites

server {
        listen 80;
        listen 443;
        server_name  .domain.example;

        return 301 $scheme://newdomain.example$request_uri;
}
美胚控场 2024-11-15 16:17:33

如果可以return,为什么还要使用重写模块呢?从技术上讲,return 是重写模块的一部分,您可以在此处阅读 但恕我直言,这段代码更容易阅读。

server {
    server_name  .domain.com;

    return 302 $scheme://forwarded-domain.com;
}

您还可以给它一个 301 重定向。

Why use the rewrite module if you can do return? Technically speaking, return is part of the rewrite module as you can read here but this snippet is easier to read imho.

server {
    server_name  .domain.com;

    return 302 $scheme://forwarded-domain.com;
}

You can also give it a 301 redirect.

一笔一画续写前缘 2024-11-15 16:17:33

这应该通过 HTTPRewriteModule 工作。

www.example.com 重写为 example.com 的示例:

server {
    server_name www.example.com;
    rewrite ^ http://example.com$request_uri? permanent;
}

That should work via HTTPRewriteModule.

Example rewrite from www.example.com to example.com:

server {
    server_name www.example.com;
    rewrite ^ http://example.com$request_uri? permanent;
}
蒲公英的约定 2024-11-15 16:17:33

如果您想将 domain1.example 的请求重定向到 domain2.example,您可以创建一个如下所示的服务器块:

server {
    listen 80;
    server_name domain1.example;
    return 301 $scheme://domain2.example$request_uri;
}

If you would like to redirect requests for domain1.example to domain2.example, you could create a server block that looks like this:

server {
    listen 80;
    server_name domain1.example;
    return 301 $scheme://domain2.example$request_uri;
}
忘羡 2024-11-15 16:17:33

您可以简单地在 server {} 块中编写 if 条件:

server {

    if ($host = mydomain.example) {
        return 301 http://www.adifferentdomain.example;
    }
}

You can simply write a if condition inside server {} block:

server {

    if ($host = mydomain.example) {
        return 301 http://www.adifferentdomain.example;
    }
}
时光磨忆 2024-11-15 16:17:33

临时重定向

rewrite ^ http://www.RedirectToThisDomain.example$request_uri? redirect;

永久重定向

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

在特定站点的 Nginx 配置文件中:

server {
    server_name www.example.com;
    rewrite ^ http://www.RedictToThisDomain.example$request_uri? redirect;

}

Temporary redirect

rewrite ^ http://www.RedirectToThisDomain.example$request_uri? redirect;

Permanent redirect

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

In Nginx configuration file for specific site:

server {
    server_name www.example.com;
    rewrite ^ http://www.RedictToThisDomain.example$request_uri? redirect;

}
2024-11-15 16:17:33

刚刚开始使用 wordops,并且有一个用于电子邮件地址的短域名,我需要将其重定向到网站的主要长域名。我的两个域的 MX DNS 条目都指向交换服务器,但 Web 服务位于 DigitalOcean 上。我最终做的是创建一个基本的 wordops html 站点,然后编辑 nginx conf 以专门重定向到长域名的 http 地址,然后重定向到 https。这解决了我在 Firefox 等中的“警告”问题。这最终对我有用:

server {
    listen 80;
    listen 443;
    server_name  .shortname.com;
    rewrite ^ http://stupidlongnamedomainname.com$request_uri? permanent;
}

Just started using wordops and had a short domain name I use for email addresses that I needed to redirect to the primary long domain name for the website. My MX DNS entries for both domains pointed to an exchange server but the web service was on DigitalOcean. What I ended up doing was creating a basic wordops html site then editing the nginx conf to redirect specifically to the http address of the long domain name which THEN redirects to https. This solved my "warning" issue in firefox etc. This is what finally worked for me:

server {
    listen 80;
    listen 443;
    server_name  .shortname.com;
    rewrite ^ http://stupidlongnamedomainname.com$request_uri? permanent;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文