【问题标题】:Getting nginx and node.js to play nice让 nginx 和 node.js 玩得很好
【发布时间】:2014-03-18 21:10:09
【问题描述】:

我有一个正在尝试配置的 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 文件(链接到启用站点的符号):

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;
    }
}

感谢您的帮助!

【问题讨论】:

    标签: node.js nginx


    【解决方案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。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 啊,这几乎正是我想要的。感谢您发布该链接。我不需要用户变量的东西,所以我删除了它,我希望这不是破坏它的原因,但无论出于何种原因,它也不起作用。 app.js 文件肯定会与 *.sock 文件一起加载,它似乎正在工作。只是当我访问 api.mysite.com 时,什么都没有。这是从 ec2 实例运行的。我为它设置了两个 ip,但我是否正确假设因为我正在使用套接字......我不再需要两个 ip?那么 www.mysite.com 和 api.mysite.com 应该指向同一个ip?
      • 您应该能够使用正确的 nginx 规则将两者托管在同一 IP 上,以将代理反向代理到相应的 unix 套接字。例如:stackoverflow.com/questions/11773544/…
      • 伙计,我就是想不通这件事。我正确设置了它,但她没有工作。如果其他人可能有一些见解,我已经更新了我原始帖子中的代码以反映我当前的设置(根据 Mark 的建议进行了更新)。
      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2011-09-17
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多