V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ropon
V2EX  ›  问与答

nginx 重写规则疑问

  •  
  •   ropon · 2019-04-04 17:05:03 +08:00 · 1052 次点击
    这是一个创建于 1841 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:访问 xxx.com/* 重写到 xxx.com/testdir/*

    测试

    if ($request_uri !~* "/testdir/?") {
    	rewrite ^(.*) /testdir$1 last;
    }
    
    1. xxx.com 访问提示"File not Found"
    2. xxx.com/x.php 重写到 xxx.com/testdir/x.php 访问正常,测试 xxx.com/index.php 也正常

    开启重写日志进一步分析,但没看懂。

    "/testdir/?" does not match "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    "^(.*)" matches "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    rewritten data: "/testdir/", args: "", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    "/testdir/?" does not match "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    "^(.*)" matches "/testdir/index.php", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    rewritten data: "/testdir/testdir/index.php", args: "", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
    

    换成以下规则测试 OK

    if ($document_uri !~* "/testdir/?") {
    	rewrite ^(.*) /testdir$1 last;
    }
    
    4 条回复    2019-04-05 20:55:40 +08:00
    kiddult
        1
    kiddult  
       2019-04-04 18:54:20 +08:00
    /testdir/ 路径没有对应?

    这两个对于 xxx.com 这个请求应该是一致的,都会重写到 xxx.com/testdir/
    ropon
        2
    ropon  
    OP
       2019-04-04 20:01:05 +08:00
    @kiddult 没看明白呢 您指的是 xxx.com xxx.com/testdir/ 是一样?
    kiddult
        3
    kiddult  
       2019-04-04 20:30:06 +08:00
    @ropon 就是对于 xxx. com 这个请求,`$document_uri` 和 `$request_uri` 产生的效果应该是一致的
    msg7086
        4
    msg7086  
       2019-04-05 20:55:40 +08:00
    新开一个 location 可能会更简单明了一些。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5397 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:36 · PVG 11:36 · LAX 20:36 · JFK 23:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.