3

I'm running two websites on a DigitalOcean droplet, and now I've upgraded to https on both of them. On one of the sites I'm doing a request to a node server I'm running on there to send emails, but since I've enabled https it blocks this request because it isn't https (logically). So I tried to use the node mailserver as a proxy pass on one of the sites that has https enabled.

To test, my node server I want to proxy pass looks like this:

exports.start = () => {
    app.use(bodyParser.json());
    app.use(cors());
    app.get('/', (req, res) => res.send('Hello World!'));
    app.listen(4004, () => console.log('Server listening on port 4004'));
};

this.start();

My Nginx sites-enabled/default looks like this:

server {

server_name mywebsite.nl www.website.nl;

location / {
    proxy_pass http://<PIVATE IP>:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /ws {
    proxy_pass http://<PIVATE IP>:4002;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
}


location /mailmeister {
            proxy_pass http://<PIVATE IP>:4004;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
}


listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mywebsite.nl/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mywebsite.nl/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

}

So the /mailmeister is where I want to redirect all requests to the node server to. When I run curl localhost:4004 or curl PRIVATE IP:4004 I get the expected Hello World! response. But when I run curl https://mywebsite.nl/mailmeister It returns a webpage that says

Cannot GET /mailmeister

Anyone know how to fix this?

milo
  • 936
  • 7
  • 18
  • 4
    `curl localhost:4004/mailmeister` should return the same 404. So you need to either configure the node.js to route `/mailmeister` or configure nginx to reduce the path to `/` see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass – generalhenry Apr 12 '18 at 20:39
  • This worked, thank you very much. – milo Apr 14 '18 at 10:13

1 Answers1

4

In a nutshell, since you have:

location /mailmeister {
            proxy_pass http://<PIVATE IP>:4004;
}

It should be handled like so:

app.get('/mailmeister ', (req, res) => res.send('Hallo World!'));

Not tested this particular setup. There is a related answer where this is explained further. Once you get it, the documentation makes more sense.

Nagev
  • 10,835
  • 4
  • 58
  • 69