您的位置:宽带测速网 > 编程知识 > Nginx路径重定向的方法是什么

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

2025-07-04 09:09来源:互联网 [ ]

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

    使用return指令:
server {listen 80;server_name example.com;location /old-path {return 301 /new-path;}location /new-path {# 处理新路径的内容}location / {# 其他路径的处理}}
    使用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指令来实现路径重定向。