使用Nginx配置NodeJs程序(Windows平台)
发布日期:2021-05-09 07:34:57 浏览次数:20 分类:精选文章

本文共 2185 字,大约阅读时间需要 7 分钟。

简介

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

安装

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

官网下载地址:(注意:下载的时候要选择windows版的)

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

d:cd nginxstart nginx

Nginx基础命令:

nginx -s stop          // 停止nginx

nginx -sreload        // 重新加载配置文件
nginx -squit          // 退出nginx

使用

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

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

 

#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\imagehttp {    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。

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

 

 

 

 

 

上一篇:NodeJs连接Oracle数据库
下一篇:CentOS安装运行NodeJS框架Express

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月28日 19时49分10秒