nginx 通过方向代理实现负载均衡,负载均衡是大流量网站要做的措施,单从字面上的意思来理解为N台服务器平均分担负载,不会因为某一台服务器负载高宕机而影响用户访问网站,负载均衡至少需要三台服务器,

既然是负载均衡,分摊服务器压力,那么一台服务器最好就负责一个网站。所以只需要配置一个server即可

1.首先是主服务器:

        upstream xxx.xxx.com{
server 111.111.111.111: weight=;#权重为2/5
server 222.222.222.222: weight=;#权重为3/5
}
server{
listen ;
server_name xxx.xxx.com;
location / {
proxy_pass http://xxx.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;
}
}

2.两台从服务器

    server {
listen default_server;
listen [::]: default_server;
server_name _;
#root /usr/share/nginx/html;
root /var/www/html;
index index.html index.php; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
} location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

负载均衡4种策略

    • 轮询:默认
          • 每个请求按照时间顺序逐一分配到不同的后台服务器,如果后台当机了,自动剔除 

            upstream xxx.xxx.com{
              server 111.111.111.111:80 fail_timeout=20s;
              
              server 222.222.222.222:80 fail_timeout=20s;
              server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
            }
    • 指定权重
            • 指定轮询纪律,weight和访问比例成正比。用于后段服务器性能不均的情况

              upstream xxx.xxx.com{
                server 111.111.111.111:80 weight=2 fail_timeout=20s;   server 222.222.222.222:80 weight=3 fail_timeout=20s;
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
              }
    • IP绑定 ip_hash
            • 每个请求按访问ip的hash结果分配,这样每个访问一个后段服务器。解决了session问题

              upstream xxx.xxxxcom{
                server 111.111.111: fail_timeout=20s;
                server 222.222.222: fail_timeout=20s;
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
              ip_hash;
              }
    • fair(第三方)
            •  按照后段服务器的响应时间来分配请求,响应时间最短的优先分配

              upstream xxx.xxx.comr {
              server 111.111.:80 fail_timeout=20;
              server 222.222.:80 fail_timeout=20;
                server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
              fair;
              }

注意

systemctl restart nginx时失败;原因查看systemctl status nginx;

配置默认访问文件之间使用空格分开 index index.html index.php

配置的前面和中间的尽量使用一个空格,或者一个tal键。

当启动正常时,但访问出现500错误,可能是location / {中出现错误}

nginx 负载均衡 反向代理的更多相关文章

  1. 架构之Nginx(负载均衡/反向代理)

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器 ,也是一个 IMAP/POP3/SMTP 代理 服务器 . Nginx 是由 Igor Sys ...

  2. nginx负载均衡(反向代理)

    6,安装nginx 6.1 依赖库安装  要安装在root根目录里,不要装在虚拟环境里面 yum install gcc-c++ pcre pcre-devel zlib zlib-devel ope ...

  3. Nginx负载均衡反向代理 后端Nginx获取客户端真实IP

    Nginx 反向代理后,后端Nginx服务器无法正常获取客户端的真实IP nginx通过http_realip_module模块来实现的这需要重新编译,如果提前编译好了就无需重新编译了1,重新编译ng ...

  4. keepalived + nginx(负载均衡反向代理HTTP,https) + tomcat(HTTP,https)

    基本架构: nginx(192.168.116.198) client        --->keepalived(116.200)      ------> tomcat (192.16 ...

  5. Nginx负载均衡反向代理

    http{ upstream test.com { server 118.118.66.88:8080; } server { listen 80; server_name www.test.com; ...

  6. Nginx HTTP负载均衡/反向代理的相关参数测试

    原文地址:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1984976.html 测试目的 (1)弄清楚HTTP Upstr ...

  7. Nginx服务器部署 负载均衡 反向代理

    Nginx服务器部署负载均衡反向代理 LVS Nginx HAProxy的优缺点 三种负载均衡器的优缺点说明如下: LVS的优点: 1.抗负载能力强.工作在第4层仅作分发之用,没有流量的产生,这个特点 ...

  8. nginx域名转发 负载均衡 反向代理

    公司有三台机器在机房,因为IP不够用,肯定要分出来,所以要建立单IP 多域名的反向代理, 就是当请求www.abc.com 跳转到本机, 请求www.bbc.com 跳转到192.168.0.35 机 ...

  9. nginx做为web容器部署静态资源以及做负载均衡反向代理实现

    需求:  此时前台开发完成打包生成静态资源文件,要做到以下方面: 使用nginx部署静态资源,同时nginx要实现端口转发,隐藏真实后台地址,同时后台需要做一个负载均衡. localhost:7001 ...

随机推荐

  1. 「bzoj3956: Count」

    题目 刚开始并没有看懂题意于是痛苦的挣扎了好久 题意是这样的 问\([l,r]\)有多少对\((i,j)\)满足\(a_i\)和\(a_j\)恰好是\(a_i...a_j\)中严格最大的两个数 强制在 ...

  2. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  3. require,import区别?

    遵循的模块化规范不一样 模块化规范:即为 JavaScript 提供一种模块编写.模块依赖和模块运行的方案.谁让最初的 JavaScript 是那么的裸奔呢——全局变量就是它的模块化规范. requi ...

  4. Linq 集合比较

    List<RemindTbl> l_lstRemind = (from x in RemindTbl where (from y in RemindTblOld where x.Atten ...

  5. Android onMeasure 方法的测量规范MeasureSpec

    一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求.一个MeasureSpec由大小和模式组成.它有三种模式:UNSPECIFIED(未 ...

  6. HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...

  7. 关于css透明度的问题

    先看background和background-color background:可以设置背景颜色,背景图片,还有定位.默认background:no-repeat; background-color ...

  8. Openresty最佳案例 | 第9篇:Openresty实现的网关权限控制

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616779 本文出自方志朋的博客 简介 采用openresty 开发出的api网关有很多 ...

  9. 菜鸟笔记 -- Chapter 09 常见类和接口

    Java为我们提供了丰富的jar包支持,这其中很多都是我们日常开发所必须的,了解这些包和类,十分有助于我们的开发,深度了解源代码也有助于我们代码的优化和提高;JDK1.8为我们提供了多于4000个类与 ...

  10. TCP协议与UDP协议

    网络通信协议规定了网络通信时,数据必须采用的格式.常见的协议有TCP协议,UDP协议. TCP协议 :(Transmission Control Protocol)传输控制协议. TCP是一种面向连接 ...