How to configure nginx so it works with Express?
You express configured to serve path /index.html
, but you requires /test-express/index.html
. Either configure express to serve /test-express/index.html
or make nginx to strip /test-exress
from proxied request. Latter is as simple as add trailing slashes to location
and proxy_pass
.
location /test-express/ {
proxy_pass http://127.0.0.1:3002/;
}
See http://nginx.org/r/proxy_pass for details.
Related videos on Youtube
Mars Robertson
Updated on September 18, 2022Comments
-
Mars Robertson over 1 year
I'm trying to configure nginx so it
proxy_pass
requests to my node apps. Question on StackOverflow got many upvotes: https://stackoverflow.com/questions/5009324/node-js-nginx-and-now and I'm using config from there.(but since question is about server configuration it is supposed to be on ServerFault)
Here is the nginx configuration:
server { listen 80; listen [::]:80; root /var/www/services.stefanow.net/public_html; index index.html index.htm; server_name services.stefanow.net; location / { try_files $uri $uri/ =404; } location /test-express { proxy_pass http://127.0.0.1:3002; } location /test-http { proxy_pass http://127.0.0.1:3003; } }
Using plain node:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3003, '127.0.0.1'); console.log('Server running at http://127.0.0.1:3003/');
It works! Check: http://services.stefanow.net/test-http
Using express:
var express = require('express'); var app = express(); // app.get('/', function(req, res) { res.redirect('/index.html'); }); app.get('/index.html', function(req, res) { res.send("blah blah index.html"); }); app.listen(3002, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3002/');
It doesn't work :( See: http://services.stefanow.net/test-express
I know that something is going on.
a) test-express is NOT running
b) text-express is running
(and I can confirm it is running via command line while ssh on the server)
root@stefanow:~# service nginx restart * Restarting nginx nginx [ OK ] root@stefanow:~# curl localhost:3002 Moved Temporarily. Redirecting to /index.html root@stefanow:~# curl localhost:3002/index.html blah blah index.html
I tried setting headers as described here: http://www.nginxtips.com/how-to-setup-nginx-as-proxy-for-nodejs/ (still doesn't work)
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true;
I also tried replacing '127.0.0.1' with 'localhost' and vice versa
Please advise. I'm pretty sure I miss some obvious detail and I would like to learn more. Thank you.
-
masegaloeh almost 10 yearsAny log in
nginx
error log? -
grammar over 9 yearsIn this setup - how are you running your express application? Do you need to have a separate process like
forever
orpm2
that runs it, thennginx
just proxies to it? -
Mars Robertson over 9 yearsI cannot recall exactly... I remember the accepted answer did work for me.
-
-
Mars Robertson almost 10 yearsQ: "I'm pretty sure I miss some obvious detail" A: "as simple as add trailing slashes" (thank you, I was literally stuck)