Bad Request Your browser sent a request that this server could not understand

Viewed 54981

Although I am by no means an expert on Ubuntu, I have had two servers running for a couple of years with no problems.

Last night, when attempting to access a local website on one of them I got the error:

**Bad Request**
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.

After several hours of frustration and no success, I rebuilt the server. While Ubuntu was installing I went to the other server and got the exact same error. The first server has now been rebuilt and it displays the same error.

I have shut down every computer on the network. Powered down the router and started over.

In addition to the two servers, the network consists of three windows machines and a Ubuntu desktop.

I have tried isolating the machines from the Internet, I have tried both wired and wireless clients.

Going to localhost on the servers displays the Ubuntu Apache Default page.

The only thing that happened about the time the problem started was the Windows decided to shutdown this machine for an update. I don't see how this could have caused a problem but I have isolated this machine from the network and the problem exists.

I cleared cookies, used five different browsers, and they all report the same error. I'm about out of ideas, and looking for any suggestions.

10 Answers

This is due to the update RFC 3986, which claims that underscores are unsafe in virtual host servernames and other elements. In my case, I could not change the URL name, so I just allowed this underscores by enabling these unsafe urls. To do that, just add HttpProtocolOptions unsafe to the httpd.conf file.

https://httpd.apache.org/docs/2.4/mod/core.html#httpprotocoloptions

I had to remove the underscore (_) from the ServerName directive, as well as the hostname in /etc/hosts.

However, the underscore in DocumentRoot is just fine.

Thus, the relevant line in /etc/hosts looks like this:

127.0.0.1       mycoolsite.localhost

And the relevant block in /etc/apache2/extra/httpd-vhosts.conf looks like this:

<VirtualHost *:80>
    DocumentRoot "/Users/satoshi/Sites/my_cool_site"
    ServerName mycoolsite.localhost
    ErrorLog "/private/var/log/apache2/my_cool_site.localhost-error_log"
    CustomLog "/private/var/log/apache2/my_cool_site.localhost-access_log" common
</VirtualHost>

Don't forget to run apachectl restart after making your changes.

In Docker Compose, if you use the networking that allows you to use container names as urls/hosts to communicate back and forth, you need to make sure you don't use underscores in the name (per Nero's answer).

This took me forever to debug, so I'm including the initial error text below (again) so that hopefully Google serves this question a little higher to the next person trying to fix php curl on a docker network.

400 Bad Request Bad Request

Your browser sent a request that this server could not understand.

Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.

This error haunted me for days, untill I finally got it.

In my case, I was making a CURL to an URL that has an empty space at the end. Can you believe it? Removing the space was enough to work fine.

For me, the issue what that the upstream backend definition of the Nginx server contains an underscore. It was hard for me to detect because centos1 and centos were remote hosts running Nginx servers whereas the last server is localhost httpd server. The two remote servers were load-balancing fine, but not the last which makes it difficult to figure out that it was the _ underscore that is the issue.

Before

location / {
        proxy_pass http://server_group;
}
upstream server_group {
        server centos1;
        server centos2;
        server localhost:8085;

}

After

location / {
        proxy_pass http://servergroup;
}
upstream servergroup {
        server centos1;
        server centos2;
        server localhost:8085;

}

I am using Microsoft identity. In my case, user claims in table claims in db was very high. The user's problem was solved when I deleted the user's claims in the table and cleared the cookie in user browser.

Try check your URL again. Could be missing slash. :-)

Related