Nginx路径重定向的方法是什么

1173
2024/5/17 19:06:15
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Nginx中,可以通过配置重定向指令来实现路径重定向。以下是两种常用的重定向方法:

  1. 使用return指令:
server {
    listen 80;
    server_name example.com;

    location /old-path {
        return 301 /new-path;
    }

    location /new-path {
        # 处理新路径的内容
    }

    location / {
        # 其他路径的处理
    }
}
  1. 使用rewrite指令:
server {
    listen 80;
    server_name example.com;

    rewrite ^/old-path$ /new-path permanent;

    location /new-path {
        # 处理新路径的内容
    }

    location / {
        # 其他路径的处理
    }
}

上述示例中,当用户访问/example.com/old-path时,会被重定向到/example.com/new-path路径上。可以根据实际需求选择使用return或者rewrite指令来实现路径重定向。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: nginx设置301跳转后有时候居然无法跳转