Proxy_Pass works when accessed directly but gives HTTP 400 when linked

October 4, 2018 544 views
Nginx Ghost

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/*;
}
1 Answer
unixynet October 4, 2018
Accepted Answer

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

Have another answer? Share your knowledge.