milo
milo

Reputation: 976

'Cannot GET' on Express when using proxy pass in Nginx

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?

Upvotes: 4

Views: 10265

Answers (1)

Nagev
Nagev

Reputation: 13305

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.

Upvotes: 5

Related Questions