问题描述:在内网环境下, 用IP访问接口均正常。但是在外网环境中用域名访问接口,get和post请求均正常,而put和delete请求一直报错如下
org.springframework.web.client.ResourceAccessException:
I/O error on PUT request for "报错接口!!":
Connection reset; nested exception is java.net.SocketException: Connection reset
查了各种资料,也按要求配置了nginx,就是不行
server {
listen 8099;
server_name http://aa.com;
#index index.php index.html index.htm default.php default.htm default.html;
gzip on;
gzip_static on; # 需要http_gzip_static_module 模块
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,PUT,POST,OPTIONS;
# 若新增后端路由前缀注意在此处添加(|新增)
location / {
proxy_pass http://aa.com/gateway;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
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;
}
# 避免端点安全问题
if ($request_uri ~ "/actuator"){
return 403;
}
}
后来了解一点内网穿透的东西,
网关层有可以设置过滤拦截, 网关服务器拦截了delete和put请求, 只允许get和post,
所有的PUT/DELETE请求,都要求改成POST;
所有的PUT请求,要携带Header:X-HTTP-Method-Override: PUT
所有的DELETE请求,要携带Header:X-HTTP-Method-Override: DELETE
参考
Request URL: http://xxx.com/admin/log/12345
Request Method: POST
POST /admin/log/12345 HTTP/1.1
Host: 192.168.2.170:8080
Connection: keep-alive
Content-Length: 0
Accept: application/json, text/plain, */*
1
Authorization: Bearer 5542d6-5cd6-48ac-a3b2-19b3fb4d
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
X-HTTP-Method-Override: delete
Origin: http://192.168.1.170:8080
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
2.服务端根据Header:X-HTTP-Method-Override,转换请求method
或在nginx层修改和转发,服务端不需要做任何处理即可,参考配置:
在nginx的server中添加
set $method $request_method;
if ($http_X_HTTP_Method_Override ~* 'DELETE') {
set $method DELETE;
}
if ($http_X_HTTP_Method_Override ~* 'PUT') {
set $method PUT;
}
proxy_method $method;
location / {
......
改完重启服务, 200 OK !!!