我想要实现的结果是:
访问 api.example.com/hello/world
就相当于访问 www.example.com/api/hellp/world
这样。就是 url 看起来好看一点。
这是 nginx 的配置

然后访问,nginx 报错:no resolver defined to resolve
我想要实现的结果是:
访问 api.example.com/hello/world
就相当于访问 www.example.com/api/hellp/world
这样。就是 url 看起来好看一点。
这是 nginx 的配置

然后访问,nginx 报错:no resolver defined to resolve
|
9200Mar 5, 2018
1
|
|
barbleMar 5, 2018
2
这个 rewrite 就能办到,不用 proxy
|
|
oonnnooMar 5, 2018
3
# 供参考
location @redirect { rewrite ^/api/(.*)$ http://api.example.com/$1 redirect; } location /api/ { proxy_set_header Host $host; proxy_pass http://api.example.com/; proxy_connect_timeout 3s; error_page 500 502 504 @redirect; } |
|
580a388da131Mar 5, 2018
5
绑定子目录就好了
|
|
menduoMar 5, 2018
6
没看出有什么总理 。location 换成 / 就行了吧。
|
|
menduoMar 5, 2018
7
没看出有什么问题 。location 换成 / 就行了吧。
|
|
JHerschelMar 5, 2018
8
在 proxy_pass 前面添加一条: " resolver 8.8.8.8; " 试试?
|
|
9
|
|
alvinbone88Mar 5, 2018
10
|
|
nextvayMar 5, 2018
11
root /home/xxxx/api;
|
|
beginorMar 5, 2018 via Android
12
可以用 proxy_pass 或者 rewrite 都可以解决, 仔细看一下 nginx 的文档就行。
不过搭车问一下, 如果 api 中返回 http 重定结果向该怎么处理? |
|
yankebuptMar 5, 2018
14
为什么我觉得这样写会不会 www.example.com/api/api/api/api......无限循环...
是不是错觉 |
|
tomczhenMar 6, 2018 via Android
15
客户端支持的话可以考虑用 307 跳转
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/307 |
|
qiayueMar 6, 2018
16
|
|
18
顶顶。
|