With nginx my https site works but http does not

Linode Staff

Hello https://example.com works fine but
http://example.com
gives a 400 error.

Below is a copy of my NGINX config file, it looks good to me. So maybe there is something else going on? Thank you for your time.
NGINX COPY-----

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    listen   443;

    ssl    on;
    ssl_certificate    /home/forge/certs/example/example.com.pem;
    ssl_certificate_key    /home/forge/certs/example/example.com.key;

    root /home/forge/example.com/public;
    index index.php index.html index.htm;

    server_name example.com;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

1 Reply

Hi there,

I'm sure there are many ways to accomplish this, but in my experience the easiest way to set up this type of redirect is to have two separate server blocks. One for your port 80 which just redirects to https, and then all of your other setup stays in your 443 block. Here is an example of what that might look like:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen   443;

    ssl    on;
    ssl_certificate    /home/forge/certs/example/example.com.pem;
    ssl_certificate_key    /home/forge/certs/example/example.com.key;

    root /home/forge/example.com/public;
    index index.php index.html index.htm;

    server_name example.com;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct