Nginx代理WebService

Nginx代理WebService

webservice 为IP地址

使用本地8091端口代理http://110.10.110.17:4658/calculator.asmx?wsdl 服务

nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
http{
upstream router {
server 110.10.110.17:4658 weight=1;
}
server {
listen 8091;
server_name localhost;
charset utf-8;

proxy_intercept_errors on;
location / {
proxy_pass http://router;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
proxy_read_timeout 660;
proxy_connect_timeout 10;
proxy_send_timeout 60;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

域名Webservice

Nginx代理的Webservice如果是域名访问路径,需要根据被代理WebService实际的Host请求头值来配置数据
使用本地8091端口代理http://www.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl服务

domain_webservice_nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
http{
#其它配置相同...
server {
#......
location / {
proxy_pass http://www.webxml.com.cn;
proxy_set_header Host $proxy_host:$proxy_port;
#其它配置相同...
}
#......
}
}

重点关注几个配置项:

  • upstream:用于配置 nginx 后端服务器(即 upstream,上游服务器),这里我们配置了两个后端服务器,并设置转发的权重分别为 2 和 1
  • proxy_pass:配置代码转发,即使用上面 upstream router 作为后端服务器转发
  • proxy_read_timeout:nginx 与后端服务器连接成功后,后端服务器响应的超时时间,即后端服务器处理请求的超时时间,由于后端服务器处理请求的最长时间为 600 秒,这里设置 660 秒
  • proxy_connect_timeout:nginx 与后端服务器连接的超时时间
  • proxy_send_timeout:后端服务器完成请求处理后,传输完整数据的超时时间
    先启动两个后端服务器,这两个后端服务器均对处提供 WebService 接口。然后使用 docker-compose up -d 启动 nginx 作为 WebService 接口反向代理。

客户端访问 nginx ,可以看到客户端 WebService 请求被正常转发到了两个 WebService 后端服务器处理,然后客户端通过 nginx 正常获取了后端服务器的返回结果。


原文链接:https://blog.csdn.net/lihao21/article/details/110856326

作者

zhang

发布于

2022-04-21

更新于

2023-09-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×