天天看点

配置Nginx负载均衡

Nginx负载均衡

  • ​​编译安装​​
  • ​​Nginx目录结构​​
  • ​​Nginx配置文件结构​​
  • ​​Nginx具体应用​​
  • ​​部署静态资源​​
  • ​​反向代理​​
  • ​​正向代理​​
  • ​​负载均衡​​

编译安装

./configure --prefix=/usr/local/nginx 
make && make install      

如果出现警告或报错

提示

checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found

#安装gcc
yum install      
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.

#安装perl库
yum install      
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.

#安装zlib库
yum install      

接下来执行

make
make install      

Nginx目录结构

重点目录/文件:

  • conf/nginx.conf nginx配置文件
  • html 存放静态文件(html、css、js等)
  • logs 日志目录、存放日志文件
  • sbin/nginx 二进制文件,用于启动、停止Nginx服务

命令

检查文件正确性

在启动Nginx服务之前,可以检查conf/nginx.conf文件配置的是否有错误

./nginx -t      

启动和停止

#启动
./nginx
#停止
./nginx -s stop
#启动完成后可以查看Nginx进程
ps -ef | grep      

重新加载配置

./nginx -s reload #重新加载配置      

Nginx配置文件结构

整体结构介绍

Nginx配置文件(conf/nginx.conf)整体分为三部分:

  • 全局块 和Nginx运行相关的全局配置
  • events块 和网络连接相关的配置
  • http块 代理、缓存、日志记录、虚拟主机配置
  • http全局块
  • Server块
  • Server全局块
  • location块

注意:http块中可以配置多个Server块,每个Server块中可以配置多个location块。

配置Nginx负载均衡

Nginx具体应用

部署静态资源

Nginx可以作为静态web服务器来部署静态资源。静态资源旨在服务端真实存在并且存在并且能够直接展示的一些文件,比如常见的html页面、css、js、images、video

相对于Tomcat,Nginx处理静态资源的能力更加高效,所以在生产环境下,一般都会将静态资源部署到Nginx中 。将静态资源部署到Nginx非常简单,只需要将文件复制到Nginx安装目录下的html目录即可。

server{
  listen 80; #监听端口
  server_name localhost; #服务器名称
  location /{  #匹配客户端请求url
    root html;  #指定静态资源根目录
    index index.html;  #指定默认首页
  }
}      

反向代理

反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,及用户直接访问代理服务器就可以获得目标服务器之间的资源,反向代理服务器负责将请求转发给目标服务器。

用户不需要知道目标服务器的地址,也无需在用户端做任何设定。

配置Nginx负载均衡

配置文件

server{
        listen     82;
        server_name   localhost;
        location /{
                proxy_pass http://192.168.16.129:8080;
        }
    }      

正向代理

是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。

正向代理的典型用途为在防火墙内的局域网客户端提供访问Internet的途径

正向代理一般是在客户端设置代理服务器,通过代理服务器转发请求,最终访问到目标服务器

配置Nginx负载均衡

负载均衡

早期的网站流量和业务功能都比较单一,单台服务器就可以满足基本需求,但是随着互联网的发展,业务流量越来越大并且业务也越来越复杂,单台服务器的性能及单点故障问题就凹陷出来了,因此需要多台服务器组成应用集群,进行性能的水平扩展以及避免单点故障出现。

应用集群:将同一应用部署到多台机器上,组织应用集群,接收负载均衡器分发的请求,进行业务处理并返回相应数据

负载均衡器:将用户请求根据对应的负载均衡算法分发到应用集群中的一台服务器进行处理

配置Nginx负载均衡

负载均衡策略

名称 说明
轮询 默认方式
weight 权重方式
ip_hash 依据ip分配方式
least_conn 依据最少连接方式
url_hash 依据url分配方式
配置负载均衡:
upstream targetserver{
     server 192.168.16.129:8080 weight=10;
     server 192.168.16.129:8081 weight=5;
}
server{
        listen     82;
        server_name   localhost;
        location /{
                proxy_pass http://targetserver;
        }
}      

继续阅读