让nginx和node.js玩得很好

时间:2014-02-18 21:00:52

标签: node.js nginx

我正在尝试配置nginx / node.js服务器。基本上这只是在端口80上同时运行2个Web服务器的问题。我有www.mysite.com,我需要指向端口80上的nginx。但我也有一个node.js服务器,我需要api.mysite.com指向端口8888.

我在我的配置(http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)中乱用了proxy_pass,但没有运气。我也试过这个https://stackoverflow.com/a/20716524/605841而没有运气。

如果有人有任何提示会很棒。提前谢谢。

Nginx公开目录: / var / www / html。 快速应用位置: / var / www / html / myNodeAppRoot

这是我的/etc/nginx/sites-available/api.mysite.com文件(sym链接到已启用网站):

server {

    listen 80;

#    server_name ~^(?<login>[a-z]+)\.api\.mysite\.com$;
    server_name api.mysite.com$;

    location / {

#       root    /var/www/html/myNodeAppRoot;

#       proxy_pass http://unix:/tmp/\$login.api.mysite.com.sock:$uri$is_args$args;
        proxy_pass http://unix:/tmp/api.mysite.com.sock:$uri$is_args$args;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

这是我的default.conf文件:

#
# The default server
#
server {
    listen       80 default_server;
    server_name  www.mysite.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        try_files $uri =404;
    #    fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我在同一台服务器上运行一堆Node.js应用程序,而nginx提供一些静态内容。这是我的设置:

# the meteor server
server {
  server_name example.com;

  access_log /etc/nginx/logs/example.access;
  error_log /etc/nginx/logs/example.error error;

  location / {
    proxy_pass http://localhost:3030;
    proxy_set_header X-Real-IP  $remote_addr;
  }

}

我只是重复此块并更改每个新Node.js应用程序的端口。然后我使用不同的--port 3030参数运行Node.js。

答案 1 :(得分:0)

nginx可以配置为使用unix套接字,它看起来像文件系统中的项目。节点supports these out of the box。这样可以避免nginx背后的端口出现任何问题。

可以找到设置带有nginx和套接字的Node应用程序的好教程here

相关问题