使用Nginx配置NodeJs程序(Windows平台)

简介:

简介

Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

安装

步骤:官网下载Nginx,解压到D盘目录,启动Nginx服务。

官网下载地址:http://nginx.org/en/download.html(注意:下载的时候要选择windows版的)

解压到D盘根目录,然后启动Nginx,运行CMD执行命令:

d:
cd nginx
start nginx

Nginx基础命令:

nginx -s stop          // 停止nginx
nginx -s reload        // 重新加载配置文件
nginx -s quit          // 退出nginx

使用

假设现在NodeJs的Express有两个站点访问地址:127.0.0.1:3000  | 127.0.0.1::3001 配置负载均衡与健康检测的默认模块,方法如下:

找到配置文件(我的Nginx安装目录为:D:\nginx):D:\nginx\conf\nginx.conf设置替换为如下代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#user  nobody;
worker_processes   1 ;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
     worker_connections   1024 ;
}
 
#ggcmsweb\image
http {
     include       mime.types;
     default_type  application / octet - stream;
     
     upstream sample {
      server  127.0 . 0.1 : 4030  max_fails = 1  fail_timeout = 40s ;
#     server 127.0.0.1:4140 max_fails=1 fail_timeout=40s;   
      keepalive  64 ;
     }
 
     server {
         listen        8080 ;
         charset      utf - 8 ;
         server_name   127.0 . 0.1 ;
 
         location  /  {
           proxy_http_version  1.1 ;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection  'upgrade' ;
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
           proxy_pass http: / / sample / ;
           proxy_connect_timeout  1 ;
           proxy_read_timeout  1 ;
         
         }
     location ~ . * \.(gif|jpg|jpeg|png|css|js|ico)$
     {
         root  / app / webCms / public;
         expires  1d ;
         }
     location ~ . * \.(html|shtml)$
     {
          ssi on;
          ssi_silent_errors on;
          ssi_types text / shtml;
         root  / app / webCms / public;
         }
     location ~  / $
     {
         index index.shtml index.html;
         root  / app / webCms / public;
         }      
     }
     server {
         listen        8081 ;     
         charset      utf - 8 ;    
         server_name   127.0 . 0.1 ;
 
         location  /  {
          root         / app / imageAPP / public;
          autoindex on;                         
          autoindex_exact_size off;        
          autoindex_localtime on;
          expires  30d ;  
         }
     }
     
}

现在访问地址127.0.0.1,Nginx会轮换把请求分别分发给端口3000和端口3001。

假如有一个服务器挂掉,则会一直分配到另一个服务器上,直到检测瘫痪的服务器正常访问之后,恢复轮换请求分发的任务。

 

 



本文转自王磊的博客博客园博客,原文链接:http://www.cnblogs.com/vipstone/p/4779184.html,如需转载请自行联系原作者


目录
相关文章
|
24天前
|
消息中间件 编译器 API
Windows窗口程序
Windows窗口程序
|
2月前
|
Windows
实现Windows程序的数据更新
实现Windows程序的数据更新
13 0
|
2月前
|
Windows
构建布局良好的Windows程序
构建布局良好的Windows程序
11 0
|
24天前
|
编译器 C语言 C++
|
2天前
|
Windows
Windows 程序自启动实现方法详解
Windows 程序自启动实现方法详解
13 0
|
3天前
|
Unix Shell 开发工具
windows下如何安装git以及IDEA如何配置git
该文指导安装Git 2.15.0版本。首先从Git官网下载最新安装包,双击安装,依次选择Next,同意默认配置,确保勾选添加到环境变量。在配置选项中,选择在cmd中使用Git(第2项),行结束转换选Windows(第1项),终端模拟器选MinTTY(第1项)。安装完成后,通过右键菜单或直接打开Git Bash验证安装成功。最后,配置全局用户名和邮箱,并在IDEA中设置Git路径以完成集成。
|
4天前
|
JavaScript 前端开发 Linux
Node.js安装与配置
`Node.js` 是一个基于 `Chrome V8` 引擎的 `JavaScript` 运行环境,它允许开发者使用 `JavaScript` 编写服务器端代码。以下是 `Node.js` 的安装与配置的详细步骤:
Node.js安装与配置
|
9天前
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
14 0
|
23天前
|
应用服务中间件 nginx Windows
【报错】Windows下启动nginx报:bind() to 0.0.0.0:80 failed...
【报错】Windows下启动nginx报:bind() to 0.0.0.0:80 failed...
|
25天前
|
编译器 C语言 C++
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程