Nginx&Web负载均衡集群搭建

单个服务器不能满足用户访问量的要求,就出现分布式部署;

也就就是用户访问URL时,nginx通过一定的规则把用户的请求分发到不同的服务器上,实现负载均衡。

实际运行方式是指以代理服务器来接受internet上的连接请求,

然后将请求转发给内部网络上的服务器,

并将从服务器上得到的结果返回给internet上请求连接的客户端,

此时代理服务器对外就表现为一个服务器

<>1.nginx安装部署

==============================================================================

通过https://nginx.org/en/download.html 下载

<>2.常用命令

=========================================================================

查看nginx的版本

G:\nginx-1.18.0>nginx -v

在命令行中启动nginx服务

.\nginx.exe(或者nginx即可)

start nginx --启动命令

强制停止nginx服务器,如果有未处理的数据,丢弃

nginx -s stop

如果有未处理的数据,等待处理完成之后停止

nginx -s quit

重新加载

nginx -s reload

<>3.配置文件

=========================================================================

在conf文件夹中找到nginx.conf配置文件,主要由6个部分组成:

main:用于进行nginx全局信息的配置

events:用于nginx工作模式的配置

http:用于进行http协议信息的一些配置

server:用于进行服务器访问信息的配置

location:用于进行访问路由的配置

upstream:用于进行负载均衡的配置

<>4. nginx支持的负载均衡调度算法

=======================================================================================

<>4.1、轮询(默认)


每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,

能自动剔除。

upstream backserver {

server 192.168.0.14;

server 192.168.0.15;

}

<>4.2、指定权重


指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

upstream backserver {

server 192.168.0.14 weight=10;

server 192.168.0.15 weight=10;

}

<>4.3 IP绑定 ip_hash


每个请求按访问ip的hash结果分配,

这样每个访客固定访问一个后端服务器,

可以解决集群部署环境下session共享的问题。

upstream backserver {

ip_hash;

server 192.168.0.14:88;

server 192.168.0.15:80;

}

<>5.修改Tomcat端口配置文件

===================================================================================

G:\apache-tomcat-8.5.32\conf\server.xml

##注:修改4个配置文件即可

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8444" />

<>6. 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 - r e m o t e u s e r \[ remote_user \[ remoteuser\[time_local\] "request" '

'$status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer" '

'" h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

upstream backserver {

server 127.0.0.1:8080;

server 127.0.0.1:8081;

}

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://backserver;

}

#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

相关推荐
GIS之路7 分钟前
使用命令行工具 ogr2ogr 将 CSV 转换为 Shp 数据(二)
前端
trayvontang7 分钟前
Nginx之location配置
运维·nginx
嘉琪00119 分钟前
Vue3+JS 高级前端面试题
开发语言·前端·javascript
vipbic1 小时前
用 Turborepo 打造 Strapi 插件开发的极速全栈体验
前端·javascript
天涯学馆1 小时前
为什么 JavaScript 可以单线程却能处理异步?
前端·javascript
Henry_Lau6171 小时前
主流IDE常用快捷键对照
前端·css·ide
陶甜也1 小时前
使用Blender进行现代建筑3D建模:前端开发者的跨界探索
前端·3d·blender
我命由我123452 小时前
VSCode - Prettier 配置格式化的单行长度
开发语言·前端·ide·vscode·前端框架·编辑器·学习方法
HashTang2 小时前
【AI 编程实战】第 4 篇:一次完美 vs 五轮对话 - UnoCSS 配置的正确姿势
前端·uni-app·ai编程
JIngJaneIL2 小时前
基于java + vue校园快递物流管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js