javascript 如何在 Windows 上使用 Node.js 运行 Nginx?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32460440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:18:41  来源:igfitidea点击:

How to run Nginx with Node.js on Windows?

javascriptnode.jsnginx

提问by marian

I want to install Nginx on Windows, and to run two node application. How can I do this?

我想在 Windows 上安装 Nginx,并运行两个节点应用程序。我怎样才能做到这一点?

I've tried to download Nginx 1.6.3, but I don't find something relevant about how to run on Windows. Just for Linux. I think there should be some modules for node.

我尝试下载 Nginx 1.6.3,但我没有找到有关如何在 Windows 上运行的相关信息。仅适用于 Linux。我认为节点应该有一些模块。

Any advice will be useful!

任何建议都会有用!

回答by BrTkCa

I never run Nginx on Windows, but the official documentation says how: http://nginx.org/en/docs/windows.html.

我从来没有在 Windows 上运行 Nginx,但官方文档说明了如何:http: //nginx.org/en/docs/windows.html

To run two node applications with Nginx, it's necessary to create a proxy. This is an example of how to alter the nginx.conffile for this:

要使用 Nginx 运行两个节点应用程序,需要创建一个代理。这是如何为此更改nginx.conf文件的示例:

worker_processes 1;

events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        gzip on;
server {
        listen 80;
        server_name localhost;
        access_log C:\var\log\nginx\access.log;
                location ~ ^/(javascripts|stylesheets|images) {
                root C:\app1\public;
                expires max;
        }
        location / {
                proxy_pass http://localhost:3000;
        }
  }
server {
        listen 81;
        server_name localhost;
        access_log C:\var\log\nginx\access.log;
                location ~ ^/(javascripts|stylesheets|images) {
                root C:\app2\public;
                expires max;
        }
        location / {
                proxy_pass http://localhost:3001;
        }
  }
}

In this case, there are two node applications, one running on port 3000 and an other on port 3001 - the Nginx works as a proxy.

在这种情况下,有两个节点应用程序,一个在端口 3000 上运行,另一个在端口 3001 上运行 - Nginx 用作代理。

Further documentation: https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/.

更多文档:https: //www.nginx.com/blog/nginx-nodejs-websockets-socketio/

In your case, the configuration file are localised in: C:\nginx_v1_6\conf\nginx.conf

在您的情况下,配置文件本地化为: C:\nginx_v1_6\conf\nginx.conf

Backup the default file and update the content with what I posted.

备份默认文件并使用我发布的内容更新内容。

Finally, you can test the reverse-proxy through localhost(port 80 default) and localhost:81, if the nodes servers and Nginx server are running.

最后,如果节点服务器和 Nginx 服务器正在运行,您可以通过localhost(默认端口 80)和测试反向代理localhost:81

回答by StephenG

Here's some instructions on how to install nginx for Windows:

以下是有关如何为 Windows 安装 nginx 的一些说明:

http://nginx.org/en/docs/windows.html

http://nginx.org/en/docs/windows.html