Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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"
share|improve this question

3 Answers 3

up vote 3 down vote accepted

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

share|improve this answer

"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
share|improve this answer
    
I have got a process listen on /tmp/app.socket. When I run your command it gives me unix 2 [ ACC ] STREAM HÖRT 29673 7029/node /tmp/app.socket. But thanks for your tip. This command is quite handy. –  pvorb Sep 28 '11 at 0:39

I solved it. The error log message I postet above lead me to the answer.

I always started the Node.js process as a normal user, while Nginx was started by root. When Node.js was started, it created the socket with srwxr-xr-x rights. So Nginx could not write to the socket, it could only read from it. This way everything could set up correctly, when the processes got started. But once I called the a web page, Nginx realized that it didn't have the rights to proxy the request to the socket.

The solution was to run

chmod 777 /tmp/app.socket

Now, everything is ok.

Thank you, anyway!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.