Nginx配置简单负载均衡
前提:因为本次需要两个软件在本机不同端口提供http服务,于是我准备一个tomcat在8080端口,另一个nodejs程序在3000端口,前者自不用提,后者可以到https://www.cnblogs.com/xiandedanteng/p/7514174.html 去下载并安装配置Node.js启动。
nginx主要通过proxy_pass来配置反向代理,upstream模块实现负载均衡。我们简单修改C:\nginx-1.16.1\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; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream firstdemo{ server 127.0.0.1:8080; server 127.0.0.1:3000; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { # root html; # index index.html index.htm; proxy_pass http://firstdemo; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root 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 html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
上面粗红体表示代理到firstdemo上。
粗蓝体则表示由8080和3000的端口分别对localhost:80的请求提供http服务。
之后使用C:\nginx-1.16.1>nginx.exe -s reload 重启nginx服务,在浏览器里输入localhost,再不断刷新,就可以轮换看到以下画面了:
这样每次刷新就会访问不同的服务器,至此我们实现了一个简单的负载均衡。
只启动一个比如3000的node server,不断刷新浏览器,然后再启动一个比如8080的tomcat,效果是什么样的诸位看官可以自行尝试一下。
--END-- 2019年12月14日06:37:45
Nginx配置简单负载均衡的更多相关文章
- Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布
一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...
- Nginx配置及负载均衡
转载:http://www.cnblogs.com/jingmoxukong/p/5945200.html nginx简易教程 目录 Nginx 概述 安装与使用 nginx 配置实战 参 ...
- nginx配置优化+负载均衡+动静分离详解
nginx配置如下: #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细 ...
- nginx 配置https 负载均衡
1.Winodw安装openssl工具(生成SSL证书用的)免编译版本下载: http://slproweb.com/products/Win32OpenSSL.html 注意:如果openssl在使 ...
- [转] nginx配置优化+负载均衡+动静分离(附带参数解析)
#指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志 ...
- nginx配置+uwsgi+负载均衡配置
nginx静态文件配置 location /static{ alias /var/www/myApp/static; } sudo mkdir -vp /var/www/myApp/static/ s ...
- nginx配置实现负载均衡
一.负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正 ...
- Nginx配置实例-负载均衡实例:平均访问多台服务器
场景 Nginx配置实例-反向代理实例:根据访问的路径跳转到不同端口的服务中: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- nginx 配置实例-负载均衡
1.实现效果 (1)浏览器地址栏输入地址 http://www.123.com/edu/a.html,负载均衡效果,平均 8080 和 8081 端口中 2.准备工作 (1)准备两台 tomcat 服 ...
随机推荐
- Hadoop1.x与Hadoop2.x之间的差异
一.Hadoop2.x产生背景 1.Hadoop1.x中的HDFS和MapReduce在高可用.扩展性等方面存在问题. 2.HDFS存在的问题 1.NameNode单点故障,难以应用于在线场景. 2. ...
- Navicat链接数据库报错1130解决方案
1.背景 使用localhost 可以正常连接mysql服务器,但是使用ip地址连接保存如下: 2.问题原因 默认情况下root用户只允许本机访问,即使用localhost访问,如下图: 解决方案:将 ...
- 10 分钟上手 Vue 组件 Vue-Draggable
Vue 综合了 Angualr 和 React 的优点,因其易上手,轻量级,受到了广泛应用.成为了是时下火热的前端框架,吸引着越来越多的前端开发者! 本文将通过一个最简单的拖拽例子带领大家快速上手 V ...
- FreeBSD设置开机同步时间
没有设置开机同步时间的话,重启之后时间不对. 如果装机时没正确设置时区,先设置时区:# tzsetup 用date命令手工设置时间一方面不方便,另一方面也依赖于本地管理员的时钟的正确性,那么网络上不同 ...
- MySQL/MariaDB数据库的存储过程
MySQL/MariaDB数据库的存储过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程概述 1>.存储过程优势 存储过程把经常使用的SQL语句或业务逻辑封装起 ...
- SpringCloud2.0 Hystrix Dashboard 断路器指标看板 基础教程(八)
1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...
- 网页自动跳转 HTTP-EQUIV="REFRESH"
文章:META HTTP-EQUIV="REFRESH" 实现网页自动跳转 < META HTTP-EQUIV="REFRESH" CONTENT=&qu ...
- 使用flask搭建微信公众号:接收与回复消息
token验证的意义 在看了别人的代码之后对token加密有了些理解了.但又觉得很鸡肋.第一次验证服务器的时候我在那弄了半天的验证其实不写也可以验证成功,只要直接返回echostr这个字段就行了.微信 ...
- Python 简单批量请求接口实例
#coding:utf-8 ''' Created on 2017年11月10日 @author: li.liu ''' import urllib import time str1=''' http ...
- [Dart] Mixin
Docs Mixins are a way of reusing a class’s code in multiple class hierarchies. void main() { Animal ...