Nginx 集群,负载均衡和配置参数解释

随着现代互联网的飞速推进,集群分布式,负载均衡 等等听上去很高大上的名词不绝于耳。今天就好好说说Nginx不废话,直接上配置文件。
1.Nginx 的目录介绍:
….conf 配置文件
… html 网页文件
…logs 日志文件
…sbin 主要二进制程序

#user  nobody;  #表示以哪个用户的身份来运行
worker_processes  1;  #设置几个工作进程,Nginx 默认的有一个主进程(master)和一个工作进程(worker)具体进程linux下你可以通过ps来查看 一般设置为 CPU数*核数
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
 #一般是配置nginx连接的特性如1个worker能同时允许多少连接
    worker_connections  1024; #这是指 一个子进程最大允许连1024个连接
}
#http 段就是用来配置虚拟主机的 其中包可以包含若干个server段 ,每个server段都可以对应一个虚拟主机
http {  //这是配置http服务器的主要段
    include       mime.types;
    default_type  application/octet-stream;
    #定义服务器日志的格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' #参数对应的是存放一个远程的ip 地址,用户名时间请求类型
    #'$status $body_bytes_sent "$http_referer" '  #请求状态, 发送的字节,上次从哪来的
    # '"$http_user_agent" "$http_x_forwarded_for"'; #用户代理
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {   #此处已经代表放置根目录 如果程序放在E盘
            root   /liyaqiang;   #表示E:/liyaqiang 文件夹
            index  index.html index.htm;
        }
        #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$ {  //用来连接所有的以.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;
        #}
    }

}

Nginx反向代理与动静分离
用Nginx 做反向代理需要使用proxy_pass,以反向代理为例, nginx不自己处理图片的相关请求,而是把图片的请求转发给apache来处理.
这里写图片描述
配置如下

location ~ \.(jpg|jpeg|png|gif)$ {
        proxy_pass HTTP://IP:port;
}

通过这种方式我们就会产生疑问,这个时候服务器端拿到的ip,为前端服务的ip 而不是真实的ip.怎么办?
代理服务器通过设置头信息字段,把用户IP传到后台服务器去,具体操作代码如下:
1.

location ~ \.(jpg|jpeg|png|gif)$ {
        proxy_set_header X-Forwarded-For $remote_addr;#配置上这个参数然后在服务日志信息中添加X-Forwarded-For
        proxy_pass IP:port;
}
  1. 在web服务器(Apache)日志信息格式中添加X-Forwarded-For如下:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common

Nginx集群与负载均衡
当后端服务器很多时候,如何做负载均衡。目前具体的负载均衡采用的DNS轮询的的方式,
这里写图片描述
DNS服务器允许一个域名下有多个A记录,当用户在访问时,一般会根据地域返回一个较近的解析记录
这样全国的用户看到的同一个页面都来自不同的服务器。当解析出结果,比如是:218.59.209.200
这台主机后面还有N台主机,也要做负载均衡。
Nginx 反向代理与负载均衡
在nginx中做集群与负载均衡,步骤都是一样的
Upstream {}模块 把多台服务器加入到一个组
然后 memcached_pass, fastcgi_pass, proxy_pass ==> upstream组
具体配置步骤:
1.:配置upstream

upstream imageserver {
    server 192.168.1.204:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.1.204:8081 weight=1 max_fails=2 fail_timeout=30s;
}

2.下游调用

location ~ \.(jpg|jpeg|png|gif)$ {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://imageserver;
}

默认的负载均衡的算法:
是设置计数器,轮流请求N台服务器.

可以安装第3方模式,来利用不同参数把请求均衡到不同服务器去
如基于cookie值区别用户做负载均衡(nginx sticky 模块)
或基于URI利用一致性哈希算法做均衡(NginxHttpUpstreamConsistentHash模块)
或基于IP做负载均衡等.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值