andybest
V2EX  ›  问与答

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

  •  
  •   andybest · Nov 12, 2014 · 2817 views
    This topic created in 4294 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
        1
    holyghost  
       Nov 12, 2014   ❤️ 1
    带uri的请求 需要用rewrite重写 然后再proxy_pass的
    extreme
        2
    extreme  
       Nov 12, 2014   ❤️ 1
    “$1”是正则表达式的东西,你都没用正则表达式,肯定无效。
    其实有一个更好的方法:
    proxy_pass http://127.0.0.1:81/process.php?path=$request_uri;
    extreme
        3
    extreme  
       Nov 12, 2014   ❤️ 1
    @extreme 忘了说,$request_uri是带请求参数的,如果要无参数的,就用$script_name
    extreme
        4
    extreme  
       Nov 12, 2014   ❤️ 1
    @extreme 记错了,不是$script_name ,$request_filename才对……
    ryd994
        5
    ryd994  
       Nov 13, 2014 via Android
    @extreme $uri不就没参数么
    extreme
        6
    extreme  
       Nov 13, 2014
    @ryd994 这是针对一个问题的多种解决方案。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 09:00 · PVG 17:00 · LAX 02:00 · JFK 05:00
    ♥ Do have faith in what you're doing.