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负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...
随机推荐
- 日志(log4j)
log4j测试代码如下: package com.learn.test; import org.apache.log4j.Logger; public class Test { public stat ...
- 【算法】php实现排序(一)
选择排序方式:先让第一位与其他位比较大小找到最小的数字,然后是第二位与除第一位的其他位比较大小找出第二位,依此类推 $arr = [2,45,12,67,33,5,23,132,46]; for ($ ...
- Could not retrieve transaction read-only status from server问题排查
今天发现save task的时候经常后台会报这个错,而且有的时候还会卡住等20几分钟才执行完. 2019-11-12 15:08:29.410 http-nio-9080-exec-6 ERROR o ...
- Button控件的三种点击事件
①在布局文件中指定onClick属性的方法设置点击事件 ②使用匿名内部类的方法设置点击事件 ③实现Activity实现OnClickListen接口的方式设置点击事件 linear.xml文件 < ...
- JavaFX WebView and WebEngine Tutorial教程
JavaFX WebView JavaFX WebView is a mini browser that is called as an embedded browser in JavaFX appl ...
- OGG 自动重启脚本
6-20 * * * /oggdata/log/oggautorestart.sh >/oggdata/log/crontab_oggautorestart.log 2>&1 [说 ...
- 【Day5】1.Request对象之Header伪装策略
import urllib.request as ur import user_agent request = ur.Request( url='https://edu.csdn.net/', hea ...
- python 爬虫 Selenium的简单使用
一.Selenium基础介绍及安装 1.Selenium简介 Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面浏览器,同时也支 ...
- 关于Python的第一行语句
通常在脚本语言的第一行会看到#!/usr/bin/env python 与 #!/usr/bin/python其中之一,这两句话的目的都是指出你的python文件用什么可执行程序去运行它. #!/us ...
- 学JAVA有哪些好的技巧方法?干货分享
作为编程语言届的老大哥,学习JAVA的人数不胜数,在这里分享一些学习JAVA的技巧以及方法,当然,这些技巧及方法使用范围包含但不限于JAVA. ① 笔记软件 印象笔记:多端互通很方便(https:// ...