How do I configure Nginx proxy_pass Node.js HTTP server via UNIX socket?
I am trying to configure a Nginx server to connect to a Node.js HTTP server via a UNIX domain socket.
The Nginx configuration file:
server {
listen 80;
location / {
proxy_pass http://unix:/tmp/app.socket:/;
}
}
(according to http://wiki.nginx.org/HttpProxyModule#proxy_pass)
The Node.js script:
var http = require('http');
http.createServer(function(req, res) {
console.log('received request');
req.end('received request\n');
}).listen('/tmp/app.socket');
Now, when I try to call
curl http://localhost/
I only get the 502 Bad Gateway error page in curl and nothing on the Node.js process.
Am I doing something wrong?
edit:
After trying quanta's solution, the mistake must have to do with the Nginx configuration, since the Node.js process establishes the connection to the socket correctly.
I also tried to configure Nginx this way:
upstream myapp {
server unix:/tmp/app.socket;
}
server {
listen 80;
location / {
proxy_pass http://myapp;
}
}
But this didn't work either.
BTW I'm using Nginx v1.0.6.
The following is being written to the error log in Nginx, when I use the second configuration
2011/09/28 13:33:47 [crit] 1849#0: *5 connect() to unix:/tmp/app.socket failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/app.socket:/", host: "localhost:80"
Solution 1:
chmod 777 /tmp/app.socket
This is a solution but not the solution.
you should probably run both webservers with the same user and/or same group so you dont have to make your socket world read writable. Also i dont see why a socket needs to be executable. so 6 should be enough. i.e: 660
Solution 2:
"502 Bad Gateway" means that Nginx cannot receive response from upstream server. Make sure that you have a process listen on /tmp/app.socket
:
# netstat --protocol=unix -nlp | grep app.socket