400 Bad Request on nginx 1.14.0 Ubuntu

Viewed 3622

Scenario:

I have two websites admin.example.com and www.example.com. The admin website is developed in Django 2.2 and the front website is developed in Angular 8. Both websites are configured with Nginx 1.14.0 on Ubuntu 18.

Problem:

I have recently added SSL on these sites. While the admin site works good the frontend shows 400 bad request error with text "The plain HTTP request was sent to HTTPS port".

I found this Dealing with nginx 400 "The plain HTTP request was sent to HTTPS port" error for the same problem but it's not working in my case.

My configuration for admin.example.com (Django):

server {
    listen 80;
    server_name admin.example.com;
    return 301 https://admin.example.com$request_uri;
}
server {
    listen 443 ssl;
    server_name admin.example.com;
    ssl off;
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/raakesh/backend;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Configuration for example.com (Angular):

server {
    listen 80 default_server;

    server_name www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 default_server ssl;
    server_name www.example.com;
    root /home/rohangular/websiteangular/dist/angular-website;
    index index.html index.htm;

    access_log /var/log/nginx/example.com/access.log;
    error_log /var/log/nginx/example.com/error.log;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    ssl off;
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;
}

I also tried to log this information using:

error_log /var/log/nginx/example.com/error.log info;

and found this in the log file:

[info] 1533#1533: *247 client sent plain HTTP request to HTTPS port while reading client request headers, client: 188.166.28.57, server: www.example.com, request: "GET /main-es5.54e5eba613f299b8fae5.js HTTP/1.1", host: "www.example.com"

Another wired issue I am facing is, this example.com website sometimes automatically starts working and sometimes shows 400 error as described above.

I am not able to understand while the first one is working but not the second one. Please help me if I have done something wrong or I missed anything.

1 Answers

The error means you are trying to access http://example.com:443 meaning you are sending HTTP request instead of https request, maybe its a typo or a browser thing?
Also, according to nginx documentation ssl off; was made obsolete after nginx 1.15 so I would get rid of that.

And adding the following will most probably fix your issue:

if ($scheme = http) {
           return 301 https://$server_name$request_uri;
    }

And the final config should look like this:

server {
    listen 443 default_server ssl;
    server_name www.example.com;

    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }

    root /home/rohangular/websiteangular/dist/angular-website;
    index index.html index.htm;

    access_log /var/log/nginx/example.com/access.log;
    error_log /var/log/nginx/example.com/error.log;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;
}
Related