Nginx 之负载均衡与反向代理
负载均衡服务器策略:
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 之负载均衡与反向代理的更多相关文章
- Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化
一.什么是负载均衡和反向代理 随着网站访问量的快速增长,单台服务器已经无法承担大量用户的并发访问,必须釆用多台服务器协同工作,以提高计算机系统的处理能力和计算强度,满足当前业务量的需求.而如何在完成同 ...
- nginx的负载均衡和反向代理
本文介绍一些负载均衡和反向代理的一些基本概念,然后介绍如何基于nginx实现,包括两种安装nginx的方法:yum安装和源码安装,以及ngix该如何配置等. 什么是负载均衡? 概念 负载均衡是高可用网 ...
- Nginx HTTP负载均衡和反向代理的配置与优化
一.什么是负载均衡和反向代理 1.负载均衡 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器具有等价的地位,都可以单独提供服务而无需其他服务的辅助.通过某种负载分担技术,将外部发送来的 ...
- Nginx HTTP负载均衡和反向代理配置
当前大并发的网站基本都采用了Nginx来做代理服务器,并且做缓存,来扛住大并发.先前也用nginx配置过简单的代理,今天有时间把整合过程拿出来和大家分享,不过其中大部分也是网上找来的资源. nginx ...
- 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 ...
- nginx负载均衡和反向代理有什么区别
近在研究nginx的负载均衡和反向代理,先看下这两个简单的配置吧! 负载均衡 worker_processes 1; events { worker_connections 1024; } http{ ...
- CentOS中实现Nginx负载均衡和反向代理
一.安装必要软件 负载均衡服务器:IP设置为192.168.1.10 Web服务器1:安装Apache或者Nginx,IP设置为192.168.1.11: Web服务器2:安装Apache或者Ngin ...
- Nginx 负载均衡和反向代理实践
nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50 在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...
- 【架构师之路】Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》
本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...
随机推荐
- Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.
django 报错 django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fiel ...
- Quartz.net任务调度(石英钟定时任务)
好了,现在具体来说一下怎么使用Quartz.net 2.0. 1.到网上下载Quartz.net 2.0,下载完后解压,里面有vs.net2008和vs.net2010两个版本. 2.新建一个空项目, ...
- 日志(log4j)
log4j测试代码如下: package com.learn.test; import org.apache.log4j.Logger; public class Test { public stat ...
- 二、eureka服务端注册服务
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 入口 上文我们说到,eureka是使用jersey来对外提供restful风格的rpc调 ...
- css之弹性盒模型
弹性盒子(Flexible Box/filebox)是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子 ...
- xposed获取类的属性成员
XposedBridge.log("开始获取属性:"); Field[] fields = param.thisObject.getClass().getDeclaredField ...
- linux 基础10-磁盘配额管理
1. 基本概念 1.1 概念: 在linux系统中,由于是多人多任务的使用环境,所以会有多人共同使用一个硬盘空间的情况,如果其中少数几个人大量使用了硬盘空间的话,势必会压缩其他使用者的使用空间,因此管 ...
- xargs 使用详解
参考转载:https://www.cnblogs.com/f-ck-need-u/p/5925923.html#auto_id_12 xargs 作用:将管道传输过来的stdin进行处理(分割.分批) ...
- less匹配模式
less里面的匹配模式相当于js里面的if,但又不完全是,比如用css画一个三角 html <div class="sanjiao"></div> less ...
- HDU - 6242 Geometry Problem (几何,思维,随机)
Geometry Problem HDU - 6242 Alice is interesting in computation geometry problem recently. She found ...