Nginx---反向代理,负载均衡
Nginx部分功能概述:
__HTTP基础功能:__
- 处理静态文件,索引文件以及自动索引;
- 反向代理加速(无缓存),简单的负载均衡和容错;
- FastCGI,简单的负载均衡和容错;
- 模块化的结构。过滤器包括gzipping, byte ranges, chunked responses, 以及 SSI-filter 。在SSI过滤器中,到同一个 proxy 或者 FastCGI 的多个子请求并发处理;
- SSL 和 TLS SNI 支持;
__其他HTTP功能:__
- 基于IP 和名称的虚拟主机服务;
- Memcached 的 GET 接口;
- 支持 keep-alive 和管道连接;
- 灵活简单的配置;
- 重新配置和在线升级而无须中断客户的工作进程;
- 可定制的访问日志,日志写入缓存,以及快捷的日志回卷;
- 4xx-5xx 错误代码重定向;
- 基于 PCRE 的 rewrite 重写模块;
- 基于客户端 IP 地址和 HTTP 基本认证的访问控制;
- PUT, DELETE, 和 MKCOL 方法;
- 支持 FLV (Flash 视频);
- 带宽限制;
Nginx的优势:
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.
作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。
Nginx的安装:
nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。
正式开始前,编译环境gcc g++ 开发库之类的需要提前装好。
配置编译环境
[[email protected] ~]# yum install -y gcc gcc-c++
解除依赖
[[email protected] ~]# yum install -y pcre-devel openssl-devel
Nginx 一般有两个版本,分别是稳定版和开发版,可以根据您的目的来选择这两个版本的其中一个
源码包可以从官网下载:http://nginx.org/download/nginx-1.14.0.tar.gz
源码编译过程:
[[email protected] ~]# mkdir -p /usr/local/nginx
[[email protected] ~]# cd /usr/local/nginx/
[[email protected] nginx]# ls
[[email protected] nginx]# mv /root/nginx-1.14.0.tar.gz .
[[email protected] nginx]# ls
nginx-1.14.0.tar.gz
[[email protected] nginx]# tar zxf nginx-1.14.0.tar.gz
[[email protected] nginx]# ls
nginx-1.14.0 nginx-1.14.0.tar.gz
[[email protected] nginx]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[[email protected] nginx-1.14.0]# ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[[email protected] nginx-1.14.0]# make
[[email protected] nginx-1.14.0]# make install
表示./configure...成功
表示make成功
表示make install 成功
nginx编译选项
make是用来编译的,它从Makefile中读取指令,然后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测 你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。nginx的 configure命令支持以下参数:
--prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx.
--sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.
--conf-path=path 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf.
--pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.
--error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为prefix/logs/error.log.
--http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为prefix/logs/access.log.
--user=name 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody.
--group=name 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户.
--with-select_module --without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll.
--with-poll_module --without-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll.
--without-http_gzip_module 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库.
--without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持.
--without-http_proxy_module 不编译http_proxy模块.
--with-http_ssl_module 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的.
--with-pcre=path 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中.
--with-pcre-jit 编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)
--with-zlib=path 设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./configure和make完成。ngx_http_gzip_module模块需要使用zlib.
启动:
确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,这里创建软连接来启动
[[email protected] nginx-1.14.0]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 892/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 968/master
tcp 0 0 172.25.100.1:22 172.25.100.250:43642 ESTABLISHED 1750/sshd
tcp 0 0 :::22 :::* LISTEN 892/sshd
tcp 0 0 ::1:25 :::* LISTEN 968/master
[[email protected] nginx-1.14.0]# ln -s /usr/local/nginx/nginx /sbin/nginx
[[email protected] nginx-1.14.0]# nginx
测试:
出现如下界面则安装成功
编辑一个测试页面
[[email protected] html]# pwd
/usr/local/nginx/html
[[email protected] html]# vim test.html
[[email protected] html]# cat test.html
<h1> server1 <h1>
再次访问
nginx实现负载均衡
修改nginx的配置文件
[[email protected] nginx]# vim nginx.conf
2 user nginx nginx; # 使用的用户和组
3 worker_processes 1; # 指定工作衍生进程数(一般等于cpu总核数或2倍)
12 events {
13 worker_connections 65535;
14 } # 允许的连接数
17 http {
18 upstream linux{
19 server 172.25.100.2:80;
20 server 172.25.100.3:80;
21 } # 定义一个轮询组linux(名称自取)
36 server {
37 listen 80;
38 server_name www.linux.com; # 监听80端口,定义链接,定义之后可以使用网址访问
44 location / {
45 proxy_pass http://linux; # 设置代理指向,指向linux轮询组
[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload
开启server2及server3的httpd服务,并编辑好index.html索引文件
测试:
添加物理机解析
[[email protected] nginx-rpm]# vim /etc/hosts
172.25.100.1 server1 www.linux.com
输入网址(www.linux.com)测试:
Ip_hash的设置
Nginx中的ip_hash功能能够将客户端的请求定向到同一台后端web机器中,可以使客户端和某个后端 web机器就能建立起稳固的连接,直到此后端web服务器down掉。
配置很简单
[[email protected] nginx]# vim nginx.conf
17 http {
18 upstream linux{
19 server 172.25.100.2:80;
20 server 172.25.100.3:80;
21 ip_hash;
[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload
测试:
关闭server2的httpd服务测试:
自动切换至另一台服务器并持续连接
weight(权重)的设置:
可以自定义轮询次数,配置及效果如下
[[email protected] nginx]# vim nginx.conf
17 http {
18 upstream linux{
19 server 172.25.100.2:80 weight=4;
20 server 172.25.100.3:80;
21 }
[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload
健康检查:
当server2及server3都down掉后,将指向本机的8080端口
开启本机的httpd服务,并更改默认发布端口为8080,编写发布文件
[[email protected] nginx]# vim nginx.conf
17 http {
18 upstream linux{
19 server 172.25.100.2:80;
20 server 172.25.100.3:80;
21 server 127.0.0.1:8080 backup;
[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload