Nginx 配置负载均衡

nginx负载均衡配置,主要是proxy_pass,upstream的使用。
注意问题,多台机器间session的共享问题。
不用session,用户cookie。或者用redis替代session。
三台服务器,一台nginx转发(10.0.0.1),两台服务器(10.0.0.2,10.0.0.3)。
nginx转发配置,
upstream balance.xxx.com
{
server 10.0.0.2:80;
server 10.0.0.3:80;
}
server
{
listen 80;
server_name balance.xxx.com;
location /
{
proxy_pass http://balance.xxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /home/wwwlogs/access.log;
}
服务器1
server
{
listen 80;
#listen [::]:80 default_server ipv6only=on;
server_name balance.xxx.com;
index index.html index.htm index.php admin.php;
root /home/wwwroot/default/load;
#error_page 404 /404.html;
include enable-php-pathinfo.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
access_log /home/wwwlogs/access.log;
}
服务器2
server
{
listen 80;
#listen [::]:80 default_server ipv6only=on;
server_name balance.xxx.com;
index index.html index.htm index.php admin.php;
root /home/wwwroot/default/load;
#error_page 404 /404.html;
include enable-php-pathinfo.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
access_log /home/wwwlogs/access.log;
}
配好之后,记得重启nginx。


增加健康筛选和权重。
upstream balance.xxx.com
{
server 101.132.185.131:80 weight=1;
server 47.91.40.220:80 weight=1; # 权重
}
server
{
listen 80;
server_name balance.xxx.com;
location /
{
proxy_pass http://balance.xxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; # 健康筛选
}
access_log /home/wwwlogs/access.log;
}
实验发现,当服务器恢复正常时,负载均衡会自动恢复。
Nginx 配置负载均衡的更多相关文章
- 通过http、https域名访问静态网页、nginx配置负载均衡(nginx配置)
很多场景下需要可以通过浏览器访问静态网页,不想把服务器ip地址直接暴露出来,通过nginx可以解决这个问题. 实现http域名访问静态网页 1.域名解析配置(本文都是以阿里云为例,其他平台,操作步骤类 ...
- 解决nginx配置负载均衡时invalid host in upstream报错
当前平台: windows nginx版本: 1.11.5 前言: 在配置负载均衡时,同时也需要设置反向代理,当修改了nginx.conf时,发现nginx服务无法开启. 1. 打开"ngi ...
- nginx配置 负载均衡
配置nginx #配置虚拟主机 server { listen 80; server_name www.testaaa.com; location / { #root /usr/local/nginx ...
- NginX——配置负载均衡
A. 在http模块加上upstream配置 upstream www.myweb.com { server 127.0.0.1:9100 weight=3; server ...
- 图解nginx配置负载均衡
1. 在Linux上准备两份tomcat 2. 修改两份tomcat的端口号 修改的端口如图所示: 3. 启动两个tomcat服务器 4. 修改两个服务器上的主页方便测试区分 5. 在nginx配置文 ...
- nginx集群:nginx配置负载均衡集群(nginx1.18.0)
一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 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的安装,若安装请看博客 http://www.cnblogs.com/hqjy/p/8092983.html 本教程不讲解tomcat的安装,若安装请看博客 http://www ...
- lnmp环境下nginx配置‘负载均衡’
NGINX负载均衡分发请求的几种方式: 1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.2.weight 指定轮询几率,weight和访问比率成正比 ...
随机推荐
- Python学习之旅(八)
Python基础知识(7):数据基本类型之元组.字典 一.元组 用括号把元素括起来中间用逗号隔开.用逗号分开一些值便可创建元组 1,2,3 结果: (1, 2, 3) 空元组可以用没有包含任何内容的两 ...
- TensorRT层和每个层支持的精度模式
下表列出了TensorRT层和每个层支持的精确模式.它还列出了该层在深度学习加速器(DLA)上运行的能力.有关附加约束的更多信息,请参见 DLA Supported Layershttps://doc ...
- Django’s cache framework
小结: 1.缓存存储位置:数据库.文件系统.内存 2.通过缓存前缀实现跨服务器缓存 Django’s cache framework | Django documentation | Django h ...
- 【转】前端框架天下三分:Angular React 和 Vue的比较
前端框架天下三分:Angular React 和 Vue的比较 原文链接:http://blog.csdn.net/haoshidai/article/details/52346865 前端这几年的技 ...
- 阻塞队列(BlockingQueue)
阻塞队列是 java.util.concurrent 包提供的一个类,该类提供了多线程中通过队列实现安全高效的数据处理的功能. 所谓阻塞队列,是在普通队列基础上实现了阻塞线程的功能: 队列为空时,获取 ...
- 用php的chr和ord函数实现字符串和ASCII码互转
http://shenyongqang.blog.163.com/blog/static/22439113201002941856838/ chr和ord函数是用来字符串和ASCII码互转的. ASC ...
- Python基础(五) python装饰器使用
这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 # -*- coding:gbk -*- '''示例1: 最简单的函数,表示调用了两次 ...
- Eclipse集成scala插件
1.Eclipse中右击help,选择Eclipse Marketplace,搜索scala,一路点击安装,重启Eclipse. 2.新建工程,new->other->出现scala wi ...
- java8模拟grouby方法
public class ListUtils{ /** * list 集合分组 * * @param list 待分组集合 * @param groupBy 分组Key算法 * @param < ...
- 关于UI适配的文档
第一部分:原理 (1)根据当前屏幕尺寸与开发预设屏幕尺寸尺寸得出以下参数. 1 XRatio:当前屏幕尺寸与开发尺寸的X轴比例 2 YRtaio:当前屏幕尺寸与开发尺寸的Y轴比例 3minRatio: ...