简体   繁体   English

让Nginx和Node.js发挥出色

[英]Getting nginx and node.js to play nice

I have an nginx/node.js server I'm trying to configure. 我正在尝试配置一个nginx / node.js服务器。 Basically it's just the issue of running 2 web servers on port 80 at the same time. 基本上,这只是在端口80上同时运行2个Web服务器的问题。 I have www.mysite.com that I need to point to nginx on port 80. But I also have a node.js server that I need api.mysite.com to point to port 8888. 我有www.mysite.com,我需要在端口80上指向nginx。但是我还有一个node.js服务器,需要api.mysite.com指向端口8888。

I'm messing around with proxy_pass in my config ( http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass ) but with no luck. 我在我的配置( http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass )中弄乱了proxy_pass,但是没有运气。 I also tried this https://stackoverflow.com/a/20716524/605841 with no luck. 我也没有运气尝试过这个https://stackoverflow.com/a/20716524/605841

If anyone has any tips that would be great. 如果有人有任何建议,那就太好了。 Thanks in advance. 提前致谢。

Nginx public dir: /var/www/html. Nginx公用目录: / var / www / html。 Express app location: /var/www/html/myNodeAppRoot Express应用程序位置: / var / www / html / myNodeAppRoot

Here's my /etc/nginx/sites-available/api.mysite.com file (sym linked into sites-enabled): 这是我的/etc/nginx/sites-available/api.mysite.com文件(符号链接到启用了sites的文件)​​:

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

And here's my default.conf file: 这是我的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;
    }
}

Thanks for any help! 谢谢你的帮助!

I run a bunch of Node.js applications on the same server, while nginx serves some static content. 我在同一台服务器上运行了一堆Node.js应用程序,而nginx提供了一些静态内容。 Here's my setup: 这是我的设置:

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

}

I just repeat this block and change the port for each new Node.js app. 我只是重复此代码块,并为每个新的Node.js应用程序更改端口。 Then I run Node.js with a different --port 3030 parameter. 然后,我使用不同的--port 3030参数运行Node.js。

nginx can be configured to use unix sockets, which look like an item in the file system. 可以将nginx配置为使用unix套接字,它看起来像文件系统中的一项。 Node supports these out of the box . Node 支持这些功能 This allows you to avoid any issues with ports behind nginx. 这样可以避免nginx后面的端口出现任何问题。

A good tutorial for setting up a Node app with nginx and sockets can be found here . 这里可以找到有关使用nginx和套接字设置Node应用程序的很好的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM