nginx反向代理java springboot报400错误

看错误日志:

java.lang.IllegalArgumentException: Invalid character found in the request target [/path/{40D8168A-0000-CD14-9F54-24F9A4462B76} ]. The valid characters are defined in RFC 7230 and RFC 3986
        at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:490)

 

原因是路径中有特殊符号{},springboot用的是tomcat无法解析

搜索到的解决方案是:

location /server {
    set $modified_uri $request_uri;
    if ($modified_uri ~ "^/([\w]{2})(/.*)") {
        set $modified_uri $1;
    }
    proxy_pass http://192.168.14.141:8090$modified_uri;
}

这个方法可以行得通,不过,我直接改为如下也正常:

location /server {
    proxy_pass http://192.168.14.141:8090$request_uri;
}

附官方文档:

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

如果proxy_pass没有指定具体的URI,那么请求Nginx的URI将原封不动的被转发到目标服务器。 即便是手动将+号编码,但是Nginx貌似会对其进行解码,最后发送出去的请求仍然是没有编码的。

参考:

https://blog.csdn.net/LLittleF/article/details/93879077

https://stackoverflow.com/questions/31266629/nginx-encoding-normalizing-part-of-uri