温馨提示×

Debian域名如何跳转

小樊
75
2026-07-17 02:01:47
栏目: 云计算

在 Debian 系统里,“域名跳转”通常指通过 Web 服务器(Nginx / Apache)把域名指向另一个域名或路径。下面按常见场景给你说明。


一、最常见:Nginx 域名跳转(推荐)

1️⃣ 安装 Nginx

sudo apt update
sudo apt install nginx

2️⃣ 配置域名跳转(301 永久跳转)

假设:

  • 原域名:old.example.com
  • 新域名:new.example.com

创建或编辑配置文件:

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

内容示例:

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

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

3️⃣ 启用配置

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

✅ 访问 http://old.example.com 会自动跳转到 https://new.example.com


二、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.example.com
    Redirect permanent / https://new.example.com/
</VirtualHost>

4️⃣ 启用并重启

sudo a2ensite redirect.conf
sudo systemctl reload apache2

三、域名指向另一个域名(DNS 层面)

如果只是让域名访问另一个域名,而不是跳转页面:

方法 1:CNAME(推荐)

old.example.com   CNAME   new.example.com

⚠️ 注意:

  • 根域名(example.com)不能直接用 CNAME
  • 一般用于 www、api 等子域名

四、域名跳转到指定路径

Nginx 示例

server {
    listen 80;
    server_name example.com;

    return 301 https://example.com/blog$request_uri;
}

五、HTTPS 域名跳转

先申请 SSL(推荐 Let’s Encrypt):

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx

Certbot 会自动帮你配置 HTTPS → HTTP 跳转。


六、常见错误排查

问题 排查
跳转不生效 nginx -t 检查配置
浏览器缓存 用无痕模式
多个 server_name 确保唯一
端口被占用 ss -lntp

七、你可以直接告诉我

  • 用的是 Nginx 还是 Apache?
  • 是 http → https 还是 域名 A → 域名 B?
  • 是不是 Debian 11 / 12?

我可以给你完全针对你服务器的配置示例。

0 踩