Nginx – 502 Bad Gateway error using Nginx/Passenger on Mac OS X (10.6.1)

mac-osxnginxphusion-passenger

After several hours of battling with this anomaly, I've finally admitted defeat. If anyone here can help me I would be most grateful!


QUICK SUMMARY:

I have this error in my Nginx error log:

2009/11/09 02:49:59 [error] 90#0: *7 connect() to unix:/tmp/passenger.54/master/helper_server.sock
failed (61: Connection refused) while
connecting to upstream, client:
127.0.0.1, server: test.local, request: "GET / HTTP/1.1", upstream:
"passenger://unix:/tmp/passenger.54/master/helper_server.sock:",
host: "test.local"

The full details are below.


I've installed Nginx and Passenger on my local machine with a view to using it for local development. I was running Apache and Passenger but I was getting a few bugs so I decided to switch.

I installed the Passenger Nginx extension without any trouble into /usr/local/nginx

and configured Nginx in /usr/local/nginx/conf/nginx.conf

For completeness here is the contents of nginx.conf:

worker_processes  2;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.5;
    passenger_ruby /usr/local/bin/ruby;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
     listen 80;
     server_name test.local;
     root /Users/i0n/Sites/test/public;
     passenger_enabled on;
     rails_env development;
     client_max_body_size 10M;
     client_body_buffer_size 128k;
     }
}

I then added /usr/local/nginx/sbin to my path variable.
So far so good. If I start up Nginx from the terminal using sudo nginx I get no trouble whatsoever, I can view my test Rails app at http(colon)//test.local as expected.

The problem arrises when I try and configure the system to load Nginx on startup. I have followed the instructions from the Nginx wiki (with some adjustments for differing paths)

I have created a plist file in:
/System/Library/LaunchDaemons/nginx.plist

It's contents look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key><string>nginx</string>
    <key>Program</key><string>/usr/local/nginx/sbin/nginx</string>
    <key>KeepAlive</key><true/>
    <key>NetworkState</key><true/>
    <key>StandardErrorPath</key><string>/usr/local/nginx/logs/error.log</string>
    <key>LaunchOnlyOnce</key><true/>
  </dict>
</plist>

I then run sudo launchctl load -F /System/Library/LaunchDaemons/nginx.plist

If I reboot the system and go to http://test.local in the browser I see a page that says:

502 Bad Gateway
nginx/0.7.61

If I look in the Nginx error log (which is at /usr/local/nginx/logs/error.log)

I see the following error recorded:

2009/11/09 02:49:59 [error] 90#0: *7 connect() to unix:/tmp/passenger.54/master/helper_server.sock
failed (61: Connection refused) while
connecting to upstream, client:
127.0.0.1, server: test.local, request: "GET / HTTP/1.1", upstream:
"passenger://unix:/tmp/passenger.54/master/helper_server.sock:",
host: "test.local"

If I manually stop and then start Nginx from the terminal using sudo it begins to work. To be honest this one has me stumped, it's probably something really simple but I just can't see it at the moment. Any thoughts or suggestions would be gratefully received. Thanks for taking the time to read this!

Ian

Best Answer

You need to set 'daemon off'. launchctl assumes that processes it spawns don't daemonize, otherwise it cannot know when to restart a process. http://almosteffortless.com/2009/09/16/passenger-with-nginx-on-mac-os-x/