ropon
V2EX  ›  问与答

nginx 重写规则疑问

  •  
  •   ropon · Apr 4, 2019 · 1808 views
    This topic created in 2795 days ago, the information mentioned may be changed or developed.

    需求:访问 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 replies  •  2019-04-05 20:55:40 +08:00
    kiddult
    kiddult
    Apr 4, 2019
    1
    /testdir/ 路径没有对应?

    这两个对于 xxx.com 这个请求应该是一致的,都会重写到 xxx.com/testdir/
    ropon
    ropon
    OP
    Apr 4, 2019
    2
    @kiddult 没看明白呢 您指的是 xxx.com xxx.com/testdir/ 是一样?
    kiddult
    kiddult
    Apr 4, 2019
    3
    @ropon 就是对于 xxx. com 这个请求,`$document_uri` 和 `$request_uri` 产生的效果应该是一致的
    msg7086
    msg7086
    Apr 5, 2019
    4
    新开一个 location 可能会更简单明了一些。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2029 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:14 · PVG 21:14 · LAX 05:14 · JFK 08:14
    ♥ Do have faith in what you're doing.