使用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。
假如有一个服务器挂掉,则会一直分配到另一个服务器上,直到检测瘫痪的服务器正常访问之后,恢复轮换请求分发的任务。
关注下面二维码,订阅更多精彩内容。

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验