Question

Proxy_Pass works when accessed directly but gives HTTP 400 when linked

I’m using the Ghost droplet to host a blog which is linked from example.com. Proxy pass has it setup at https://www.example.com/blog. This links works perfectly if I enter it into my URL bar and access it directly. However there is a link on example.com’s homepage. The link is to https://www.example.com/blog but for some reason my nginx configuration tries to 301 redirect it to http://www.example.com:443/blog. This then returns HTTP 400 “The plain HTTP request was sent to HTTPS port”

I have played around with this for the last 2 days and cant figure out what’s causing this. Seeing as the proxy_pass works fine it has to be something to do with the http redirect right? But i cant see any issues with it. I’m sure its something staring me right in the face!

worker_processes  1;

events {
    worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

server{
    listen 80;
    listen 443; 
    server_name example.com;

    return 301 https://www.example.com;

}

server {
    listen 80;
    listen 443; 
    server_name www.example.com;

    if ($http_x_forwarded_proto = 'http'){
        return 301 https://$host$request_uri;
    }

    sendfile on;

    default_type application/octet-stream;

    gzip on;
    gzip_http_version 1.1;
    gzip_disable      "MSIE [1-6]\.";
    gzip_min_length   256;
    gzip_vary         on;
    gzip_proxied      expired no-cache no-store private auth;
    gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_comp_level   9;

    root /usr/share/nginx/html;

    location / {
        try_files  $uri $uri/ /blog/$uri;
    }

    location /blog/ {
        proxy_pass https://ip.address:443;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

    }

}

include servers/*;
}

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Remove the 443 port from your /blog/ location. That’s redundant (you already specified https) and could be causing issues. and add “port_in_redirect off;” to your server block.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.