nginx ,把类似 /page/123_index.json的请求重定向为 /page/index.json请问怎么配置?

location ~ ^/pages/\d+_index.json{
    proxy_pass /pages/index.json;
}

试了一下不行
我对nginx配置不太熟悉,请教了.

阅读 3.6k
2 个回答

你可以使用 rewrite 重写你的请求路径

    location / {
        rewrite ^/pages/\d+_index\.json /pages/index.json break;
        proxy_pass http://127.0.0.1:8080;
    }
    # 测试下 会返回重写后的 url /pages/index.json
    location = /pages/index.json {
        return 200 $request_uri;
    }

我找到了两种解决的方法
rewrite:

location ~ ^/pages/{
     rewrite ^/pages/(\d+)/\d+_index.json   http://$host/pages/$1/index.json break;
}

proxy_pass

location ~ ^/pages/(\d+)/\d+_index.json$ {
    proxy_pass http://$host/pages/$1/index.json;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题