How to fix nginx throws 400 bad request headers on any header testing tools?

前端 未结 7 759
滥情空心
滥情空心 2020-12-03 06:52

I have my site which is using nginx, and testing site with header testing tools e.g. http://www.webconfs.com/http-header-check.php but every time it says 400 bad request bel

相关标签:
7条回答
  • 2020-12-03 07:13

    As stated by Maxim Dounin in the comments above:

    When nginx returns 400 (Bad Request) it will log the reason into error log, at "info" level. Hence an obvious way to find out what's going on is to configure error_log to log messages at "info" level and take a look into error log when testing.

    0 讨论(0)
  • 2020-12-03 07:16

    Yes changing the error_to debug level as Emmanuel Joubaud suggested worked out (edit /etc/nginx/sites-enabled/default ):

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

    Then after restaring nginx I got in the error log with my Python application using uwsgi:

            2017/02/08 22:32:24 [debug] 1322#1322: *1 connect to unix:///run/uwsgi/app/socket, fd:20 #2
            2017/02/08 22:32:24 [debug] 1322#1322: *1 connected
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http upstream connect: 0
            2017/02/08 22:32:24 [debug] 1322#1322: *1 posix_memalign: 0000560E1F25A2A0:128 @16
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http upstream send request
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http upstream send request body
            2017/02/08 22:32:24 [debug] 1322#1322: *1 chain writer buf fl:0 s:454
            2017/02/08 22:32:24 [debug] 1322#1322: *1 chain writer in: 0000560E1F2A0928
            2017/02/08 22:32:24 [debug] 1322#1322: *1 writev: 454 of 454
            2017/02/08 22:32:24 [debug] 1322#1322: *1 chain writer out: 0000000000000000
            2017/02/08 22:32:24 [debug] 1322#1322: *1 event timer add: 20: 60000:1486593204249
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http finalize request: -4, "/?" a:1, c:2
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http request count:2 blk:0
            2017/02/08 22:32:24 [debug] 1322#1322: *1 post event 0000560E1F2E5DE0
            2017/02/08 22:32:24 [debug] 1322#1322: *1 post event 0000560E1F2E5E40
            2017/02/08 22:32:24 [debug] 1322#1322: *1 delete posted event 0000560E1F2E5DE0
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http run request: "/?"
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http upstream check client, write event:1, "/"
            2017/02/08 22:32:24 [debug] 1322#1322: *1 http upstream recv(): -1 (11: Resource temporarily unavailable)
    

    Then I took a look to my uwsgi log and found out that:

            Invalid HTTP_HOST header: 'www.mysite.local'. You may need to add u'www.mysite.local' to ALLOWED_HOSTS.
            [pid: 10903|app: 0|req: 2/4] 192.168.221.2 () {38 vars in 450 bytes} [Wed Feb  8 22:32:24 2017] GET / => generated 54098 bytes in 55 msecs (HTTP/1.1 400) 4 headers in 135 bytes (1 switches on core 0)
    

    And adding www.mysite.local to the settings.py ALLOWED_HOSTS fixed the issue :)

            ALLOWED_HOSTS = ['www.mysite.local']
    
    0 讨论(0)
  • 2020-12-03 07:20

    Just to clearify, in /etc/nginx/nginx.conf, you can put at the beginning of the file the line

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

    And then restart nginx:

    sudo service nginx restart
    

    That way you can detail what nginx is doing and why it is returning the status code 400.

    0 讨论(0)
  • 2020-12-03 07:22

    normally, Maxim Donnie's method can find the reason. But I encountered one 400 bad request will not log to err_log. I found the reason with the help with tcpdump

    0 讨论(0)
  • 2020-12-03 07:27

    A cause can be invalid encoding in the URL request. Such as % being passed un-encoded.

    0 讨论(0)
  • 2020-12-03 07:31

    When nginx returns 400(bad request) it will log the reason into error log, at "info" level and take a look into error log when testing.

    0 讨论(0)
提交回复
热议问题