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

相关推荐
雪碧聊技术8 分钟前
深入解析Vue中v-model的双向绑定实现原理
前端·javascript·vue.js·v-model
快起来别睡了9 分钟前
手写 Ajax 与 Promise:从底层原理到实际应用
前端
Hellc00712 分钟前
Nginx 高级 CC 与 DDoS 防御策略指南
运维·nginx·ddos
打不着的大喇叭1 小时前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app
无我Code1 小时前
2025----前端个人年中总结
前端·年终总结·创业
程序猿阿伟1 小时前
《前端路由重构:解锁多语言交互的底层逻辑》
前端·重构
Sun_light1 小时前
6个你必须掌握的「React Hooks」实用技巧✨
前端·javascript·react.js
爱学习的茄子1 小时前
深度解析JavaScript中的call方法实现:从原理到手写实现的完整指南
前端·javascript·面试
莫空00001 小时前
Vue组件通信方式详解
前端·面试
呆呆的心1 小时前
揭秘 CSS 伪元素:不用加标签也能玩转出花的界面技巧 ✨
前端·css·html