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和访问比率成正比 ...
随机推荐
- 【MySQL】锁——查看当前数据库锁请求的三种方法 20
MySQL提供了查看当前数据库锁请求的三种方法:1. show full processlist命令 观察state和info列 2. show engine innodb status\G ...
- JavaScript基础知识(数据类型)
数据类型 布尔:true/fasle console.log(typeof true);// "boolean" Number : true -->1 false --> ...
- centOs6.5配置启动ssh
.非root用户则执行su或su - 或su root或su - root切换为root用户 2.查看SSH是否安装(检查是否装了SSH包) 输入命令:rpm -qa | grep ssh 注:若没安 ...
- bug修复记录
telnet 网址 ping netaddr traceroute netaddr ,这是一个linux下的命令可以通过Vmmap观察程序运行时所需要的依赖库协议里面涉及到数组的, 一定要判断最大值 ...
- 微信小程序http 400问题
在v0.14.140900版本的wechat小程序开发工具中做网络请求,直接使用微信的网络请求代码debug过程中发生了400 (Bad request)错误. wx.request({ url: ' ...
- 斑马打印机ZT410中文打印
^XA ^CW1, E:SIMSUN.TTF^CI28^FO50,50^A1N,50,50^FD汉字^FS^XZ ******************************************* ...
- Windows平台分布式架构实践 - 负载均衡(转载)
Windows平台分布式架构实践 - 负载均衡 概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Li ...
- Kali2安装完成后的设置
1.安装中文输入法 vim /etc/apt/sources.list 全部删除,改为国内源 #中科大 deb http://mirrors.ustc.edu.cn/kali kali-rolling ...
- java框架之SpringMVC(2)-补充及拦截器
高级参数绑定 package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; i ...
- JDBC-DAO层数据访问工具类的实现
private static PreparedStatement pst; private static ResultSet rst; public static <T> int inse ...