V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ropon
V2EX  ›  问与答

nginx 重写规则疑问

  •  
  •   ropon · Apr 4, 2019 · 1489 views
    This topic created in 2593 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
        1
    kiddult  
       Apr 4, 2019
    /testdir/ 路径没有对应?

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