通过nginx代理soap webservice问题,本想像一般的http一样,配置如下:
server {
listen 8089;
location /test/v1 {
proxy_pass http://10.0.60.88:8001;
}
}
但实际请求结果报如下的错:
这个地址调用报错SocketTimeoutException invoking http://10.0.60.88:8001//test/v1/wsController: connect timed out

照网上说明,加上这个:proxy_set_header Host $host:$server_port; 变成如下,通了

server {
listen 8089;
location /test/v1 {
proxy_set_header Host $host:$server_port;
proxy_pass http://10.0.60.88:8001;
}
}
再试试,通了,设ngnix地址为192.168.2.123
然后访问代理地址:http://192.168.2.123:8089/test/v1/user?wsdl

wsdl中 service.port.address 的location元素的生成规则
通过测试发现 对应location的生成规则 和你访问 wsdl的时候使用的 http header中 host有关系
比如使用localhost

参考网上:https://www.jianshu.com/p/d4aaf5789bd8 作者夜月行者

浏览器输入 http://localhost:8786/services/policeGkService?wsdl
对应的http head 中的 Host属性是
Host: localhost:8786

返回的wsdl对应的location是

<soap:address location="http://localhost:8786/services/policeGkService"/>

如果使用 127.0.0.1

浏览器输入 http://127.0.0.1:8786/services/policeGkService?wsdl
对应的 http head 中的 Host 属性是
Host: 127.0.0.1:8786

返回的wsdl对应的location是

<soap:address location="http://127.0.0.1:8786/services/policeGkService"/>

所以返回的location地址是和你请求的时候使用的ip是有关系的。

  1. nginx中配置注意事项
    那么nginx当中应该怎么配置呢
    从2中得知,返回的location地址是和你请求的时候使用的ip是有关系的。
    所以在nginx中进行反向代理的时候记得把 Host 修改 为nginx的 ip:port 即可。

具体配置对应

upstream monitor_soap_servers{
     server 100.09.91.159:8786;
     server 100.09.168.68:8786;
     server 100.09.178.20:8786;
}
server {
    listen 80;
    server_name  100.09.129.131;

    #### nginx_proxy session共享:
    ssl_session_timeout         1d;
    ssl_session_cache           shared:SSL:50m;

    resolver                    8.8.4.4 8.8.8.8 valid=300s;
    resolver_timeout            10s;

    location / {
        access_log /data/nginx/log/third_search.kkmh.com.log api;
      # 这个地方的配置十分重要,必须这样配置才可以返回正确的wsdl文档
        proxy_set_header Host $host:$server_port;
        proxy_pass http://monitor_soap_servers/;
    }
}

在这种配置情况下
访问 wsdl文档使用的路径是

http://100.09.129.131/services/policeGkService?wsdl

返回的location是nginx对应的地址,这样的话就真正实现了负载均衡

<service name="SearchSoapServerServiceService">
    <port name="SearchSoapServerServicePort" binding="tns:SearchSoapServerServicePortBinding">
        <soap:address location="http://100.09.129.131:80/services/policeGkService"/>
    </port>
</service>