Nginx return 400 bad request | nginx proxy_pass url decoding

Nginx return 400 bad request

Refer

https://stackoverflow.com/questions/28684300/nginx-pass-proxy-subdirectory-without-url-decoding/37584637#37584637

Background

proxy pass static image to another host, most of requests are ok, but some requests return 400 (bad request).

Enable nginx debug

edit /var/nginx/nginx.conf

        error_log /var/log/nginx/error.log debug;

Track error log

nginx config

location ~ ^/static/images/(.*) {
        proxy_pass http://youranotherdomain/images/$1;
    }

http proxy status 400 "400 Bad Request"

$1 has been decode, like /images/2021/07/28/[COS福利] Hane Ame 雨波 - 布莱莫顿/13821151585.jpg

Error Log

"GET /images/2021/07/28/[COS福利] Hane Ame 雨波 - 布莱莫顿/13821151585.jpg HTTP/1.0
Host: youanotherdomain.com
Connection: close
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
accept-language: en-US,en;q=0.5
accept-encoding: gzip, deflate, br
upgrade-insecure-requests: 1
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: same-origin

Change the config to

    location ~ ^/static/images/ {
        rewrite ^ $request_uri;
        rewrite ^/static/(.*) $1 break;
        return 400;
        proxy_pass http://youranotherdomain/$uri;
    }

Log

"GET /images/2021/07/28/[COS%E7%A6%8F%E5%88%A9]%20Hane%20Ame%20%E9%9B%A8%E6%B3%A2%20-%20%E5%B8%83%E8%8E%B1%E8%8E%AB%E9%A1%BF/13821151585.jpg HTTP/1.0
Host: 42.193.105.3:10000
Connection: close
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
accept-language: en-US,en;q=0.5
accept-encoding: gzip, deflate, br
upgrade-insecure-requests: 1
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: same-origin
"

Result

All static image request are ok.

评论

还没有人评论,抢个沙发吧...

Viagle Blog

欢迎来到我的个人博客网站