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

2018-08-07 20:14:07 +08:00
 txwd

如下面的代码:

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

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

试下这样,没测试
txwd
2018-08-07 22:12:32 +08:00
@cxbig 你好,并没有错误日志。只是页面报 502 了。
txwd
2018-08-07 22:13:22 +08:00
@bin456789 你好,不行。依旧是 502 Bad Gateway
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;
}
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/477728

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX