温馨提示×

debian域名重定向怎么设

小樊
52
2026-10-10 22:49:33
栏目: 云计算

在 Debian 上设置“域名重定向”,通常取决于你用的是哪种服务。下面按常见场景给你说明,基本都是 Debian 通用的做法。


一、使用 Nginx 做域名重定向(最常见)

1. 安装 Nginx

sudo apt update
sudo apt install nginx

2. 把旧域名重定向到新域名(301)

编辑站点配置:

sudo nano /etc/nginx/sites-available/redirect.conf

示例(把 old.com 重定向到 new.com):

server {
    listen 80;
    server_name old.com www.old.com;

    return 301 https://new.com$request_uri;
}

启用配置:

sudo ln -s /etc/nginx/sites-available/redirect.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

二、使用 Apache 做域名重定向

1. 安装 Apache

sudo apt install apache2

2. 开启 rewrite 模块

sudo a2enmod rewrite

3. 配置重定向

编辑虚拟主机:

sudo nano /etc/apache2/sites-available/redirect.conf

示例:

<VirtualHost *:80>
    ServerName old.com
    Redirect 301 / https://new.com/
</VirtualHost>

启用并重启:

sudo a2ensite redirect.conf
sudo systemctl restart apache2

三、仅 DNS 层面的“重定向”(注意)

DNS 不能做 HTTP 重定向

  • A / CNAME 只能指向 IP
  • 真正的跳转必须由 Web 服务器完成

如果你只是想:

  • 多域名指向同一网站 → 用 CNAME / A 记录
  • 跳转页面 → 必须用 Nginx / Apache

四、HTTPS 域名重定向(推荐)

如果你有证书(Let’s Encrypt):

server {
    listen 80;
    server_name old.com;
    return 301 https://new.com$request_uri;
}

五、常见需求对照

需求 做法
旧域名 → 新域名 Nginx/Apache 301
www → 非 www server_name + return
http → https 80 端口 return 301
多域名同站 server_name 多个

如果你能告诉我:

  • 用的是 Nginx 还是 Apache
  • 是 http/https
  • 是 整站跳转还是子路径

我可以直接帮你写一份完整配置。

0 踩