负载均衡服务器策略:

1.轮循  每个请求逐个分发到后端服务器

2.加权轮循  按照分配的权重将请求分发到后端服务器

3.ip hash 轮询的基础上,保持一个客户端多次请求分发到一台后端服务器上

一、轮询配置

  #定义后端服务器组
upstream nginx-test{
server 192.168.0.128;
server 192.168.0.127;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root "G:/phpstudy/nginx/html"; location / {
index index.html index.htm index.php l.php;
autoindex on;
proxy_pass http://nginx-test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
} }

二、轮询加权配置

  #定义后端服务器组
upstream nginx-test{
server 192.168.0.128 weight=2;
server 192.168.0.127;
server 192.168.0.126 backup; # 备份服务器,其他服务器宕机后启动
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root "G:/phpstudy/nginx/html"; location / {
index index.html index.htm index.php l.php;
autoindex on;
proxy_pass http://nginx-test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

三、IP Hash配置

  #定义后端服务器组
upstream nginx-test{
ip_hash;
server 192.168.0.128;
server 192.168.0.127;
server 192.168.0.126;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root "G:/phpstudy/nginx/html"; location / {
index index.html index.htm index.php l.php;
autoindex on;
proxy_pass http://nginx-test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

四、负载均衡与反向代理区别

1、负载均衡需要通过反向代理来实现
2、反向代理就是指nginx作为前端服务器,将请求转发到后端,再将后端服务器的结果,返回给客户端
它在中间做了一个代理服务器的角色
3、负载均衡对反向代理增加了一些策略,因为后端是多台服务器,nginx会根据设定的策略将请求转发给一个相对空闲的服务器,对负载进行分流,减轻服务器压力

五、反向代理配置

  #定义后端服务器组
upstream nginx-test{
server 192.168.0.127;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root "G:/phpstudy/nginx/html";   #代理配置参数
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr; location / {
  proxy_pass http://nginx-test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Nginx 之负载均衡与反向代理的更多相关文章

  1. Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化

    一.什么是负载均衡和反向代理 随着网站访问量的快速增长,单台服务器已经无法承担大量用户的并发访问,必须釆用多台服务器协同工作,以提高计算机系统的处理能力和计算强度,满足当前业务量的需求.而如何在完成同 ...

  2. nginx的负载均衡和反向代理

    本文介绍一些负载均衡和反向代理的一些基本概念,然后介绍如何基于nginx实现,包括两种安装nginx的方法:yum安装和源码安装,以及ngix该如何配置等. 什么是负载均衡? 概念 负载均衡是高可用网 ...

  3. Nginx HTTP负载均衡和反向代理的配置与优化

    一.什么是负载均衡和反向代理 1.负载均衡 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器具有等价的地位,都可以单独提供服务而无需其他服务的辅助.通过某种负载分担技术,将外部发送来的 ...

  4. Nginx HTTP负载均衡和反向代理配置

    当前大并发的网站基本都采用了Nginx来做代理服务器,并且做缓存,来扛住大并发.先前也用nginx配置过简单的代理,今天有时间把整合过程拿出来和大家分享,不过其中大部分也是网上找来的资源. nginx ...

  5. nginx配置负载均衡与反向代理

    #给文件夹授权   1 chown -R www:www /usr/local/nginx #修改配置文件vim nginx.conf   1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. nginx负载均衡和反向代理有什么区别

    近在研究nginx的负载均衡和反向代理,先看下这两个简单的配置吧! 负载均衡 worker_processes 1; events { worker_connections 1024; } http{ ...

  7. CentOS中实现Nginx负载均衡和反向代理

    一.安装必要软件 负载均衡服务器:IP设置为192.168.1.10 Web服务器1:安装Apache或者Nginx,IP设置为192.168.1.11: Web服务器2:安装Apache或者Ngin ...

  8. Nginx 负载均衡和反向代理实践

    nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50    在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...

  9. 【架构师之路】Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...

随机推荐

  1. CSS一些常用样式

    限制行数溢出省略号 display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: ; overflow: hidden ...

  2. SVN 问题解决之 The XML response contains invalid XML

    公司几个同事的SVN更新时出现了The XML response contains invalid XML报错 经Google得到一个线索,可能和Http请求有关. 想起之前项目改过一次网络请求方式, ...

  3. css3 transform实现水平和垂直居中

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. oracle exists和 not exists 的用法

    比如 a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在: select * from a where exists(select 1 from b where b ...

  5. web服务器端挖矿代码攻击的错误检测及排除

    a)挖矿代码简要阐述: 网页中嵌入Javascript, 一旦用户打开该网站,浏览器便会按照脚本的指令变成一个门罗币挖矿机.这一段附加的挖矿代码通常因为大量占用CPU,使用户的计算机变得异常卡顿甚至无 ...

  6. JS实现数组去重(重复元素保留一个)

    1.遍历数组法 它是最简单的数组去重方法(indexOf方法) 实现思路:新建一个数组,遍历去要重的数组,当值不在新数组的时候(indexOf为-1)就加入该新数组中: var arr=[2,8,5, ...

  7. HTML锚点的使用

    锚点: 连接到的本页面的位置:

  8. Computer Vision_33_SIFT:PCA-SIFT A More Distinctive Representation for Local Image Descriptors——2004

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  9. Scyther 形式化分析工具资料整理(三)

    1.作者Cas Cremers在做TLS1.3的时候我么发现并没有使用Scyther 形式化丰分析工具对其进行分析,而是使用了 The Tamarin .作者建立了TLS.13的模型. 那么我的目标是 ...

  10. DNS zone file

    zone file格式最初由Berkeley Internet Name Domain(BIND)软件包使用,但已被其他DNS server software广泛采用.它们中的一些(例如NSD,Pow ...