当前位置: 代码网 > 服务器>服务器>Linux > Nginx实现前端重定向的三种方法介绍

Nginx实现前端重定向的三种方法介绍

2025年09月30日 Linux 我要评论
需求:http://127.0.0.1/test-chat/orig_docs/80a4e16a-6945-4fa0-bc8a-2b9c9c6eacb7使用 nginx 转为下面的请求http://1

需求:

http://127.0.0.1/test-chat/orig_docs/80a4e16a-6945-4fa0-bc8a-2b9c9c6eacb7

使用 nginx 转为下面的请求

http://127.0.0.1/orig_docs/80a4e16a-6945-4fa0-bc8a-2b9c9c6eacb7

可以使用 nginx 的rewritelocation指令来实现这个 url 转换。以下是几种配置方式:

方法 1:使用 rewrite 指令

server {
    listen 80;
    server_name 127.0.0.1;

    # 方法1.1:在server级别重写
    rewrite ^/test-chat/orig_docs/(.*)$ /orig_docs/$1 permanent;

    # 或者方法1.2:在location中重写
    location /test-chat/orig_docs/ {
        rewrite ^/test-chat/orig_docs/(.*)$ /orig_docs/$1 permanent;
    }
}

方法 2:使用 location 代理

server {
    listen 80;
    server_name 127.0.0.1;

    location /test-chat/orig_docs/ {
        # 移除test-chat前缀
        rewrite ^/test-chat(/orig_docs/.*)$ $1 break;

        # 代理到实际服务
        proxy_pass http://your_backend_server;
        proxy_set_header host $host;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header x-forwarded-proto $scheme;
    }

    location /orig_docs/ {
        # 处理原始/orig_docs请求
        proxy_pass http://your_backend_server;
        proxy_set_header host $host;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header x-forwarded-proto $scheme;
    }
}

方法 3:精确的路径重写

server {
    listen 80;
    server_name 127.0.0.1;

    location ~ ^/test-chat/orig_docs/(.+)$ {
        return 301 /orig_docs/$1;
    }
}

完整的 nginx 配置示例

server {
    listen 80;
    server_name 127.0.0.1;

    # 访问日志
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # 重写规则:移除test-chat前缀
    rewrite ^/test-chat/orig_docs/(.*)$ /orig_docs/$1 permanent;

    # 处理/orig_docs请求
    location /orig_docs/ {
        # 这里配置您的后端服务
        proxy_pass http://backend_server;
        proxy_set_header host $host;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header x-forwarded-proto $scheme;

        # 可选:设置超时时间
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
    }

    # 其他location配置...
    location / {
        # 默认处理
        root /usr/share/nginx/html;
        index index.html;
    }
}

配置说明

permanent (301 重定向):浏览器会记住重定向,适合永久性 url 变更

redirect (302 重定向):临时重定向,每次都会请求原 url

break:在 server 内部重写,不对外部重定向

测试配置

# 检查nginx配置语法
nginx -t

# 重新加载配置
nginx -s reload

选择哪种方法取决于您的具体需求:

  • 如果希望用户浏览器地址栏显示新的 url,使用permanentredirect
  • 如果只是内部重写,使用break
  • 如果需要代理到其他服务,使用方法 2 的代理配置

到此这篇关于nginx实现前端重定向的三种方法介绍的文章就介绍到这了,更多相关nginx前端重定向内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com