Hi!
I have a similar problem.
My Nginx Block:
server {
server_name MY_SITE www.MY_SITE;
root /var/www/MY_SITE/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://localhost:3000;
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;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/MY_SITE/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/MY_SITE/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
}
server {
if ($host = MY_SITE) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = MY_SITE) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name MY_SITE www.MY_SITE;
return 404; # managed by Certbot
}
When I use only node (code below) and access MY_SITE/api works perfectly.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.JS!');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
When I use Express (below). MY_SITE/api return:
Route 1 => Cannot GET / api /
Route 2 works!
How do I get Route 1 to work by changing the Nginx setting?
var express = require('express');
var app = express();
//return Cannot GET /api/
app.get('/', function (req, res) {
res.send('Hello World!');
});
//works!
app.get('/api', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});