当使用带有express和socket.io的nodejs时,nginx返回502

时间:2014-05-09 11:59:50

标签: node.js nginx socket.io

我无法弄清楚问题。也许有人可以帮助我。 我有一个基于express.js的node.js应用程序,也使用socket.io。这个应用程序在nginx代理后运行,直到今天一切正常,没有任何问题。 今天我不得不重新启动服务器,从那时起,当我尝试访问该网站时,我总是得到502.

这是我的nginx配置:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

upstream <my_app> {
    server <my_ip>:4567;
    keepalive 512;
}

server {
    listen 80;
    server_name <my_domain>;
    server_tokens off;
    client_max_body_size 32M;
    keepalive_timeout 10;
    large_client_header_buffers 8 32k;

    access_log  /var/log/nginx/<my_app>_access.log;
    error_log   /var/log/nginx/<my_app>_error.log;

    location ~ ^/(favicon.ico) {
        root /var/www/<my_app_root>/public;
        access_log off;
        expires max;
    }

    location / {
        proxy_next_upstream     error timeout       http_500 http_502 http_503 http_504;
        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;

        proxy_http_version      1.1;
        proxy_set_header        Host                $http_host;
        proxy_set_header        Upgrade             $http_upgrade;


        proxy_buffers           8                   32k;
        proxy_buffer_size       64k;

        proxy_pass              http://<my_app>;
        proxy_redirect          off;
    }
}

这是nginx的错误

2014/05/09 13:53:38 [error] 3093#0: *23 upstream prematurely closed connection while reading response header from upstream, client: 217.110.45.3, server: <my_domain>, request: "GET / HTTP/1.1", upstream: "http://<my_ip>:4567/", host: "<my_domain>"

我在ubuntu 12.04上使用nginx版本1.6.0

这是在/etc/init.d /中启动应用程序的脚本:

#!/bin/bash

NODE_ENV="production"
NODE_APP="<my_app>.js"
APP_DIR="/var/www/<my_domain>"
PID_FILE=/<my_path_to_pid>/app.pid
LOG_FILE=/<my_path_to_log>/app.log
NODE_EXEC="supervisor -w /var/www/<my_domain>"

start_app (){
    if [ -f $PID_FILE ]
    then
        echo "$PID_FILE exists, App is already running or crashed"
    else
        echo "Starting App ..."
        NODE_ENV=$NODE_ENV $NODE_EXEC $APP_DIR/$NODE_APP  1>$LOG_FILE 2>&1 &
        echo $! > $PID_FILE;
    fi
}

stop_app (){
    if [ ! -f $PID_FILE ]
    then
        echo "$PID_FILE does not exist, App is not running"
    else
        echo "Stopping $APP_DIR/$NODE_APP ..."
        echo "Killing `cat $PID_FILE`"
        kill `cat $PID_FILE`;
        rm -f $PID_FILE;
        echo "App stopped"
    fi
}

case "$1" in
    start)
        start_app
    ;;

    stop)
        stop_app
    ;;

    restart)
        stop_app
        start_app
    ;;

    status)
        if [ -f $PID_FILE ]
        then
            PID=`cat $PID_FILE`
            if [ -z "`ps -ef | grep $PID | grep -v grep`" ]
            then
                echo "Node app stopped but pid file exists"
            else
                echo "Node app running with pid $PID"

            fi
        else
            echo "Node app stopped"
        fi
    ;;

    *)
        echo "Usage: /etc/init.d/node-app {start|stop|restart|status}"
    ;;
esac

0 个答案:

没有答案