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

小億
206
2024-05-17 19:06:15

在Nginx中,可以通過配置重定向指令來(lái)實(shí)現(xiàn)路徑重定向。以下是兩種常用的重定向方法:

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

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

    location /new-path {
        # 處理新路徑的內(nèi)容
    }

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

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

    location /new-path {
        # 處理新路徑的內(nèi)容
    }

    location / {
        # 其他路徑的處理
    }
}

上述示例中,當(dāng)用戶訪問/example.com/old-path時(shí),會(huì)被重定向到/example.com/new-path路徑上??梢愿鶕?jù)實(shí)際需求選擇使用return或者rewrite指令來(lái)實(shí)現(xiàn)路徑重定向。

0