当前位置: 首页 > 编程日记 > 正文

[ 总结 ] nginx 负载均衡 及 缓存

操作系统:centos6.4 x64

前端使用nginx做反向代理,后端服务器为:apache + php + mysql

1. nginx负载均衡。

nginx编译安装(编译安装前面的文章已经写过)、apache + php + mysql 直接使用yum安装。

nginx端口:80

apache端口:800 和 8080

nginx配置如下:

在http节点下添加如下:

    upstream backend {
    ip_hash;server
127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=30s;server 127.0.0.1:800 weight=10 max_fails=2 fail_timeout=30s;}

将server节点下的location节点中添加proxy_pass 配置为:http://+upstream名称

        location / {proxy_pass http://backend;
            #root   html;#index  index.html index.htm;}

这样负载均衡就已经完成了。upstream策略如下:

weight 权重

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。上面的情况:127.0.0.1:800访问到的几率要比127.0.0.1:8080高一倍。

ip_hash:

每个请求按访问IP的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

upstream还可以为每个设备设置状态值,这些状态如下:

down 表示当前的server暂时不参与负载。

weight 默认为1,weight越大,负载的权重就越大。

max_fails  允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误。

fail_timeout max_fails次失败后,暂停的时间。

backup 其他所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力是最小的。

2. nginx缓存设置

建议:在设置nginx缓存时,最好能编译安装ngx_purge_module 模块。

[root@cloud conf]# nginx -V
nginx version: nginx/1.9.9
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=../ngx_cache_purge-2.3 \
--with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module

nginx 全部相关配置如下:

user  nginx nginx;
worker_processes  2;error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;pid        logs/nginx.pid;worker_rlimit_nofile 65535;events {use epoll;multi_accept on;worker_connections  4096;
}http {server_tokens  off;include       mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  logs/access.log  main;open_log_file_cache max=2000 inactive=20s min_uses=2 valid=1m;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;limit_conn_zone $binary_remote_addr zone=addr:5m;limit_conn addr 100;sendfile        on;tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;client_header_timeout 2m;client_body_timeout 3m;reset_timedout_connection on;send_timeout 15s;client_max_body_size 10m;client_body_buffer_size 512k;open_file_cache max=65535 inactive=20s;open_file_cache_valid 30s;open_file_cache_errors on;open_file_cache_min_uses 2;gzip  on;gzip_disable "msie6";gzip_proxied any;gzip_comp_level 4;gzip_vary on;gzip_min_length 1k;gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javasc
ript;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区proxy_temp_path
/cache/proxy_temp_dir;    # 创建临时缓存目录
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。proxy_cache_path /cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=
30g;
upstream backend {ip_hash;server
127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=30;server 127.0.0.1:800 weight=10 max_fails=2 fail_timeout=30;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
     proxy_next_upstream http_502 http_504 error timeout invalid_header;  proxy_pass http:
//backend;proxy_cache cache_one;proxy_cache_valid 200 304 12h;  #对不同的HTTP状态码设置不同的缓存时间
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
     proxy_cache_key $host$uri$is_args$args;expires 1d;
#root html;#index index.html index.htm;}location
~ /purge(/.*) {proxy_cache_purge cache_one $host$1$is_args$args;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one ##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}

转载于:https://www.cnblogs.com/hukey/p/5359507.html

相关文章:

中国剩余定理(孙子定理)的证明和c++求解

《孙子算经》里面的"物不知数"说的是这样的一个题目:一堆东西不知道具体数目,3个一数剩2个,5个一数剩3个,7个一数剩2个,问一共有多少个。 书里面给了计算过程及答案:70*2 21*3 15*2 -105*2 2…

osi七层网络层_OSI层速成课程

osi七层网络层介绍 (Introduction) Have you ever wondered how data is sent through the network from one machine to another? If yes, then the Open System Interconnected model is what you are looking for.您是否曾经想过如何通过网络将数据从一台机器发送到另一台机…

KMP算法求回溯数组的步骤

KMP算法到底是什么原理就不说了,各种资料上讲的明明白白,下面我就如何用代码来实现做一下说明和记录. KMP的核心思想就是,主串不回溯,只模式串回溯。而模式串匹配到第几位时失配,要回溯多少,由模式串本身来…

【.Net】vs2017 自带发布工具 ClickOnce发布包遇到的问题

一、遇到的问题 在安装了vs2017 社区版(Community)之后 想打包安装程序(winform) 还是想用之前的 installshield来打包 发现居然打不了,在官网查了 installshield不支持社区版(Community)&…

windows平台,开发环境变量配置

1.打开我的电脑--属性--高级--环境变量 JAVA配置环境变量变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7.0 变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 变量名:Path 变…

如何编写可测试的代码 哈利勒的方法论

Understanding how to write testable code is one of the biggest frustrations I had when I finished school and started working at my first real-world job. 当我完成学业并开始从事第一份现实世界的工作时,了解如何编写可测试的代码是我最大的挫败之一。 T…

【Java入门提高篇】Day6 Java内部类——成员内部类

内部类是什么,简单来说,就是定义在类内部的类(一本正经的说着废话)。 一个正经的内部类是长这样的: public class Outer {class Inner{} } 这是为了演示而写的类,没有什么luan用,可以看到Inner类…

POJ 1001(高精度乘法 java的2种解法)

方法1: import java.math.BigDecimal; import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc new Scanner(System.in);while(sc.hasNext()){String d sc.next();int z sc.nextInt();BigDecimal bd new BigDecimal(d);BigDeci…

Java编写的电梯模拟系统《结对作业》

作业代码:https://coding.net/u/liyi175/p/Dianti/git 伙伴成员:李伊 http://home.cnblogs.com/u/Yililove/ 对于这次作业,我刚开始一点思绪都没有,在老师安排了结对伙伴李伊之后,我的搭档问我,我们需要什么…

HTML属性说明

HTML elements can have attributes, which contain additional information about the element.HTML元素可以具有属性,其中包含有关该元素的其他信息。 HTML attributes generally come in name-value pairs, and always go in the opening tag of an element. Th…

css中的选择器

1.在html中引入css的方法&#xff1a;四种方式: a.行内式(也称内联式) 如: <h1 style"color:red;test</h1> b.内嵌式 <style type"text/css"> h1{ color:red; font-size: 10.5pt; font-family: Calibri, sans-serif; line-height: normal; widow…

javascript的call()方法与apply()方法的理解

先看一段代码 function cat() {} cat.prototype{food:fish,say:function () {console.log(I love this.food);} };var blackCat new cat(); blackCat.say(); 这时&#xff0c;控制台输出 I love fish若此时&#xff0c;有另一个对象 Dog{food:bones and shit}; dog对象没有say…

java排序算法(冒泡,插入,选择,快速,堆,归并,希尔,基数)

import java.util.Arrays; import java.util.LinkedList;/*** * * 各种排序: 冒泡&#xff0c;插入&#xff0c;选择&#xff0c;快速&#xff0c;堆&#xff0c;归并&#xff0c;希尔&#xff0c;基数*/ public class Sorts {//1. 冒泡&#xff1a;//时间复杂度:n(n-1)/2O(n^2…

边界填充算法讲解_边界填充算法

边界填充算法讲解Boundary fill is the algorithm used frequently in computer graphics to fill a desired color inside a closed polygon having the same boundary color for all of its sides.边界填充是在计算机图形学中经常使用的算法&#xff0c;用于在其所有边都具有…

使用Git管理源代码

git是个了不起但却复杂的源代码管理系统。它能支持复杂的任务&#xff0c;却因此经常被认为太过复杂而不适用于简单的日常工作。让我们诚实一记吧&#xff1a;Git是复杂的&#xff0c;我们不要装作它不是。但我仍然会试图教会你用&#xff08;我的&#xff09;基本的Git和远程代…

[.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---Linux环境搭建

最近朋友托我帮忙研究如何把一个DTCMS部署到Linux下&#xff0c;经过1天的研究&#xff0c;部署基本成功&#xff0c;可能有些细节还未注意到&#xff0c;现在把心得分享一下。过程比预期的要简单 身为.Net程序员&#xff0c;这个问题的第一步可能就是如何搭建一个Linux环境来测…

Sequence point 中文

摘自维基百科&#xff1a; In C[4] and C,[5] sequence points occur in the following places. (In C, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.) Between ev…

python中pop函数_Python中的Pop函数

python中pop函数什么是弹出功能&#xff1f; (What is the pop function?) The method pop() removes and returns the last element from a list. There is an optional parameter which is the index of the element to be removed from the list. If no index is specified…

第六周学习进度条

日期 任务 听课 编程 阅读 准备考试 日总计 周日 周一 120 300 0 0 420 100 周二 0 120 0 0 120 周三 0 0 0 0 0 周四 0 0 0 0 0 周五 0 0 0 0 0 周六 0 120 100 0 …

1071. 小赌怡情(15)

常言道“小赌怡情”。这是一个很简单的小游戏&#xff1a;首先由计算机给出第一个整数&#xff1b;然后玩家下注赌第二个整数将会比第一个数大还是小&#xff1b;玩家下注t个筹码后&#xff0c;计算机给出第二个数。若玩家猜对了&#xff0c;则系统奖励玩家t个筹码&#xff1b;…

关于年长程序员的5个误传

原文链接&#xff1a;http://kb.cnblogs.com/page/150932/ 英文原文&#xff1a;Five Pervasive Myths About Older Software Developers 最近我刚过完40岁生日&#xff0c;一个朋友向我开玩笑地说“嘿&#xff0c;你已经老了&#xff0c;不适合做程序员了&#xff01;”我虽然…

java中getter_Java中的Getter和Setters解释了

java中getterGetters and setters are used to protect your data, particularly when creating classes. Getter和Setter用于保护数据&#xff0c;尤其是在创建类时。 For each instance variable, a getter method returns its value while a setter method sets or updates…

Loadrunner手动关联详解

Loadrunner手动关联详解 一、关联的含义&#xff1a; 关联&#xff08;correlation&#xff09;&#xff1a;在脚本回放过程中&#xff0c;客户端发出请求&#xff0c;通过关联函数所定义的左右边界值&#xff08;也就是关联规则&#xff09;&#xff0c;在服务器所响应的内容中…

解决Visual Studio禁止使用strlen函数的问题

问题描述&#xff1a; 在学习C的复制构造函数以及复制赋值运算符的重载时&#xff0c;需要用到使用C风格的字符串作为引入&#xff0c;由于我用的是VS2015&#xff08;社区版&#xff09;&#xff0c;在编译时出错。编译器提醒strcpy函数是不安全的&#xff0c;建议改用strlen_…

求整型数组所有子串的和中的最大值

#include <iostream> using namespace std;const int MIN_INT -2147483647;int maxSum(const int *arr, int len){int my_max MIN_INT;int tmp 0;for(int i 0; i < len; i){//从头到尾。。tmp arr[i];//遍历相加。if(my_max < tmp){//更新my_maxmy_max tmp;}…

c语言中的if语句_If ... C中的其他语句解释

c语言中的if语句Conditional code flow is the ability to change the way a piece of code behaves based on certain conditions. In such situations you can use if statements.条件代码流是根据某些条件更改一段代码的行为的能力。 在这种情况下&#xff0c;可以使用if语句…

设计模式之笔记--装饰模式(Decorator)

装饰模式&#xff08;Decorator&#xff09; 定义 装饰模式&#xff08;Decorator&#xff09;&#xff0c;动态地给一个对象添加一些额外的职责&#xff0c;就增加功能来说&#xff0c;装饰模式比生成子类更为灵活。 类图 描述 Component&#xff1a;被装饰者和装饰者共有的基…

整型数组负数放左面,其他放右面,要求时空复杂度:O(n), O(1)。

例如&#xff1a;处理前&#xff1a;{5 -3 6 -7 -6 1 8 -4 0 0}&#xff0c;处理后&#xff1a;{-3 -7 -6 -4 5 6 1 8 0 0}. #include <iostream> #include <algorithm> using namespace std;const int LEN 10; void printInt(int c){cout<<c<<"…

[bzoj] 1176 Mokia || CDQ分治

原题 给出WW的矩阵&#xff08;S没有用&#xff0c;题目有误&#xff09;&#xff0c;给出无限次操作&#xff0c;每次操作的含义为&#xff1a; 输入1:你需要把(x,y)(第x行第y列)的格子权值增加a 输入2:你需要求出以左下角为(x1,y1),右上角为(x2,y2)的矩阵内所有格子的权值和,…

sql子查询示例_SQL更新查询示例说明

sql子查询示例In this article, were going to learn how to use the SQL update statement - what it is, what it can do, and what you need to be aware of before using it.在本文中&#xff0c;我们将学习如何使用SQL更新语句-它是什么&#xff0c;它可以做什么以及在使用…