andybest
V2EX  ›  问与答

Nginx 如何将 request path 作为参数传递给后端?

  •  
  •   andybest · Nov 12, 2014 · 2928 views
    This topic created in 4399 days ago, the information mentioned may be changed or developed.
    例如客户端浏览器访问 http://my.com/abc/d.html 或者其他任何基于 my.com 下的路径,
    将该路径转至 http://127.0.0.1:81/process.php?path=/abc/d.html 进行处理

    我使用 proxy_pass 方法如下,确不生效:

    server {
    ...
    location / {
    proxy_pass http://127.0.0.1:81/process.php?path=$1;
    }
    }

    这个 $1 变量在 rewrite 指令后可代表 request path ,而 proxy_pass 下却不行呢,求教!
    6 replies  •  2014-11-13 12:48:50 +08:00
    holyghost
    holyghost
    Nov 12, 2014❤️1
    1
    带uri的请求 需要用rewrite重写 然后再proxy_pass的
    extreme
    extreme
    Nov 12, 2014❤️1
    2
    “$1”是正则表达式的东西,你都没用正则表达式,肯定无效。
    其实有一个更好的方法:
    proxy_pass http://127.0.0.1:81/process.php?path=$request_uri;
    extreme
    extreme
    Nov 12, 2014❤️1
    3
    @extreme 忘了说,$request_uri是带请求参数的,如果要无参数的,就用$script_name
    extreme
    extreme
    Nov 12, 2014❤️1
    4
    @extreme 记错了,不是$script_name ,$request_filename才对……
    ryd994
    ryd994
    Nov 13, 2014 via Android
    5
    @extreme $uri不就没参数么
    extreme
    extreme
    Nov 13, 2014
    6
    @ryd994 这是针对一个问题的多种解决方案。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2029 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:54 · PVG 21:54 · LAX 05:54 · JFK 08:54
    ♥ Do have faith in what you're doing.