V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
txwd
V2EX  ›  NGINX

nginx 配置中的 url 怎么拼变量,我这样拼访问不到。

  •  
  •   txwd · 2018-08-07 20:14:07 +08:00 · 4340 次点击
    这是一个创建于 2061 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下面的代码:

    set $str '中国'; location / { root D:/nginx-1.14.0/web1; index index.html index.htm; proxy_pass https://www.baidu.com/s?wd=$str; }

    11 条回复    2018-08-08 00:23:14 +08:00
    txwd
        1
    txwd  
    OP
       2018-08-07 20:20:24 +08:00
    查资料,别人都是这样写的,很奇怪,我这里就不行。
    Xiaobaixiao
        2
    Xiaobaixiao  
       2018-08-07 20:23:32 +08:00
    楼主用的是 win 服务器?路径符号注意一下
    txwd
        3
    txwd  
    OP
       2018-08-07 20:30:12 +08:00
    @Xiaobaixiao 是 win
    txwd
        4
    txwd  
    OP
       2018-08-07 20:31:41 +08:00
    @Xiaobaixiao 哪里的路径? proxy_pass https://www.baidu.com/s?wd=中国; 这样写是没问题的,就是拼变量有问题。
    Xiaobaixiao
        5
    Xiaobaixiao  
       2018-08-07 20:56:29 +08:00
    @txwd 这里-------------> root D:/nginx-1.14.0/web1 将 /换成\
    txwd
        6
    txwd  
    OP
       2018-08-07 21:01:19 +08:00
    @Xiaobaixiao 应该不是斜杠的问题。proxy_pass wd=中国; 这样写是没问题的,拼了变量就不行了。我照你说的换了,也是不行。
    cxbig
        7
    cxbig  
       2018-08-07 21:57:27 +08:00
    @txwd 貼 log 出來啊,看看錯誤提示是什麼。包括 nginx 所在機器和 upstream。
    bin456789
        8
    bin456789  
       2018-08-07 22:04:06 +08:00
    set $str '中国';
    set $str2 'https://www.baidu.com/s?wd=${str}';

    试下这样,没测试
    txwd
        9
    txwd  
    OP
       2018-08-07 22:12:32 +08:00
    @cxbig 你好,并没有错误日志。只是页面报 502 了。
    txwd
        10
    txwd  
    OP
       2018-08-07 22:13:22 +08:00
    @bin456789 你好,不行。依旧是 502 Bad Gateway
    randyzhao
        11
    randyzhao  
       2018-08-08 00:23:14 +08:00
    str 为什么是定义好的?没大看懂。。。

    不应该是想做成类似这个样子么?

    location / {
    root D:/nginx-1.14.0/web1;
    index index.html index.htm;

    if ($query_string ~* "wd=(.+)$") {
    set $str $1;
    proxy_pass https://www.baidu.com/s?wd=$str;
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2703 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:10 · PVG 23:10 · LAX 08:10 · JFK 11:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.