3

I am running my own webserver with nginx on ubuntu 17.10. I have opened ports 80 and 443.

Trying to get the HTTP to HTTPS redirect working, but everthing i seem to have tried fails. i am still getting the 400 Bad Request The plain HTTP request was sent to HTTPS port error when i navigate directly to the http site.

Hoping someone can spot the issue in my nginx config file.

server {
    listen 80;
    listen [::]:80;
    server_name  example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl http2; # managed by Certbot
    server_name  example.com www.example.com;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    root /var/www/html/wordpress;
    index  index.php index.html index.htm;

    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }

    # Deny access to .htaccess files, if Apache's document
    # root concurs with nginx's one

    location ~ /\.ht {
        deny all;

    }

    location ^~ /.well-known/acme-challenge/ {
        auth_basic off;
        autoindex on;
    }

}
2

What you have here is not the nginx conf, but the sites conf file. Your 301 redirect from http to https seems correct, but you are requiring a secure connection prior to accepting the connection through https, thus triggering a 400 error.

Just comment out the ssl on; line like so:

server {
    listen 443 ssl;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;
    #ssl on;
    ...

or set to off:

server {
    listen 443 ssl;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;
    ssl off;
    ...

I also recommend you add http2 to the ipv4 block:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;
    ssl off;
    ...

More information here.

2

maybe your server isn't connected via IpV6, and you only needed the standard ipV4 entries:

  server{
         listen 80;
         server_name example.com www.example.com;
         return 301 https://www.example.com$request_uri;
             }
     server {
        listen 443 ssl http2;
        server_name example.com www.example.com;
        root   /var/www/html/example.com/;
        index index.php index.html index.htm;

          .....the rest of your port 443 arguments....

         }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.