nginx

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

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强


Nginx 特点

Nginx 做为 HTTP 服务器,有以下几项基本特性:

  • 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.

  • 无缓存的反向代理加速,简单的负载均衡和容错.

  • FastCGI,简单的负载均衡和容错.

  • 模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCGI 或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。

  • 支持 SSL 和 TLSSNI.



Nginx 的工作方式

Nginx 是以多进程的方式来工作的,当然 Nginx 也是支持多线程的方式的,只是我们主流的方式还是多进程的方式,也是 Nginx 的默认方式

Nginx 在启动后,会有一个 master 进程和多个 worker 进程。master 进程主要用来管理 worker 进程,包含:接收来自外界的信号,向各 worker 进程发送信号,监控 worker 进程的运行状态,当 worker 进程退出后(异常情况下),会自动重新启动新的 worker 进程。而基本的网络事件,则是放在 worker 进程中来处理了。多个 worker 进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个 worker 进程中处理,一个 worker 进程,不可能处理其它进程的请求。worker 进程的个数是可以设置的,一般我们会设置与机器cpu核数一致,这里面的原因与 Nginx 的进程模型以及事件处理模型是分不开的。Nginx 的进程模型


企业 - nginx

当我们在操作 Nginx 的时候,Nginx 内部做了些什么事情,那么,worker 进程又是如何处理请求的呢?我们前面有提到,worker 进程之间是平等的,每个进程,处理请求的机会也是一样的。当我们提供 80 端口的 http 服务时,一个连接请求过来,每个进程都有可能处理这个连接,怎么做到的呢?首先,每个 worker 进程都是从 master 进程 fork 过来,在 master 进程里面,先建立好需要 listen 的 socket(listenfd)之后,然后再 fork 出多个 worker 进程。所有 worker 进程的 listenfd 会在新连接到来时变得可读,为保证只有一个进程处理该连接,所有 worker 进程在注册 listenfd 读事件前抢 accept_mutex,抢到互斥锁的那个进程注册 listenfd 读事件,在读事件里调用 accept 接受该连接。当一个 worker 进程在 accept 这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接,这样一个完整的请求就是这样的了。我们可以看到,一个 请求,完全由 worker 进程来处理,而且只在一个 worker 进程中处理。




为什么 Nginx 采用异步非阻塞的方式来处理呢

首先,请求过来,要建立连接,然后再接收数据,接收数据后,再发送数据。具体到系统底层,就是读写事件,而当读写事件没有准备好时,必然不可操作,如果不 用非阻塞的方式来调用,那就得阻塞调用了,事件没有准备好,那就只能等了,等事件准备好了,再继续。阻塞调用会进入内核等待,cpu 就会让出去给别人用了,对单线程的 worker 来说,显然不合适,当网络事件越多时,大家都在等待呢,cpu 空闲下来没人用,cpu利用率自然上不去了,更别谈高并发了。

非阻塞就是,事件没有准备好,马上返回 EAGAIN,告诉你,事件还没准备好呢,你过一会,再来检查一下事件,直到事件准备好了为止,在这期间,你就可以先去做其它事情,然后再来看看事件好了没。阻塞调用会进入内核等待,cpu 就会让出去给别人用了,对单线程的 worker 来说,显然不合适,当网络事件越多时,大家都在等待呢,cpu 空闲下来没人用,cpu利用率自然上不去了,更别谈高并发了。



优点

Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高

Nginx 是一个很强大的高性能Web反向代理服务器,它具有很多非常优越的特性:

在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。


实验

实验环境

server1      nginx主机

server2      http

server3      http

server4      nginx


server1

[[email protected] ~]# tar zxf nginx-1.10.1.tar.gz   解压

企业 - nginx



[[email protected] core]# pwd
/root/nginx-1.10.1/src/core
[[email protected] core]# vim nginx.h   去掉后面内容如下


企业 - nginx

[[email protected] cc]# pwd
/root/nginx-1.10.1/auto/cc

[[email protected] cc]# vim gcc   将debug下,此内容注释

企业 - nginx


编译

[[email protected] nginx-1.10.1]# yum install -y pcre-devel openssl-devel zlib-devel gcc  安装依赖性

[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[[email protected] nginx-1.10.1]# make && make install

编译完成
企业 - nginx


开启服务

企业 - nginx

服务已打开

企业 - nginx


查看服务端口,如下80端口已开

企业 - nginx

浏览器访问nginx主机

企业 - nginx

添加用户

[[email protected] conf]# useradd -u 800 nginx

企业 - nginx

[[email protected] conf]# pwd
/usr/local/nginx/conf
[[email protected] conf]# vim nginx.conf

企业 - nginx


做软连接,平滑加载

企业 - nginx

lscpu 可列出cpu相关信息



也可更改worker数

[[email protected] conf]# vim nginx.conf  

企业 - nginx

[[email protected] conf]# nginx -s reload   重新加载

企业 - nginx


[[email protected] conf]# ulimit -SHn
[[email protected] conf]# ulimit -a

企业 - nginx


企业 - nginx


[[email protected] conf]# vim nginx.conf

企业 - nginx

[[email protected] security]# pwd
/etc/security
[[email protected] security]# vim limits.conf
企业 - nginx


企业 - nginx


[[email protected] conf]# vim nginx.conf   修改配置文件

企业 - nginx

企业 - nginx

制作证书

[[email protected] certs]# make cert.pem

企业 - nginx


企业 - nginx


企业 - nginx


浏览器访问


https://192.168.122.11

企业 - nginx



[[email protected] conf]# vim nginx.conf    修改配置文件,加入模块

企业 - nginx

[[email protected] conf]# nginx -s reload
企业 - nginx

加入http虚拟主机

[[email protected] conf]# vim nginx.conf

企业 - nginx

企业 - nginx




新建目录,编写主页,重新加载服务

企业 - nginx

客户端测试(做解析)

企业 - nginx


[[email protected] conf]# vim nginx.conf  再加入所维护的域
企业 - nginx

企业 - nginx

测试

企业 - nginx


[[email protected] conf]# vim nginx.conf

proxy_pass http://cara;   #负载均衡

cara为之前定义的upstream !

企业 - nginx


[[email protected] conf]# nginx -s reload


server2,3安装http服务

[[email protected] ~]# yum install -y httpd  

[[email protected] ~]# /etc/init.d/httpd start  开启服务
[[email protected] ~]# yum install -y httpd

[[email protected] ~]# /etc/init.d/httpd start

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf  修改端口,因为nginx主机定义的是8080

企业 - nginx

[[email protected] ~]# /etc/init.d/httpd restart

server2,3的页面如下,实际生活中应该是一致的,为了查看实验效果,测试页不同

企业 - nginx

企业 - nginx


客户端测试

企业 - nginx

可通过加入不同的参数,实现不同的需求

eg1

企业 - nginx


企业 - nginx

eg2

企业 - nginx



企业 - nginx

eg3

企业 - nginx

企业 - nginx


eg4

企业 - nginx

会报错

企业 - nginx

#重新编译时,先关掉,否则和会覆盖之前的编译

企业 - nginx

[[email protected] nginx-1.10.1]# ./configure --help
[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng

[[email protected] nginx-1.10.1]# make && make install

开启服务

企业 - nginx

企业 - nginx


但在浏览器访问时,不会跳动(类似ip_hash)

企业 - nginx

高可用


[[email protected] local]# scp -r nginx/ [email protected]:/usr/local  

企业 - nginx

新建用户,开启服务

企业 - nginx

server1,4均安装ricci服务,设置密码,设置为开机启动

[[email protected] local]# yum install -y ricci   安装服务
[[email protected] local]# yum install -y ricci   安装服务

企业 - nginx



企业 - nginx


[[email protected] local]# yum install -y luci  安装服务

开启服务

企业 - nginx

[[email protected] local]# chkconfig luci on  设置开机启动

浏览器访问,做好解析

https://server1:8084      进入集群资源管理器

企业 - nginx

企业 - nginx


[[email protected] ~]# clustat   查看集群

企业 - nginx


fence-控制断电


[email protected] Desktop]# systemctl status libvirted  用物理机做fence,查看状态

企业 - nginx

企业 - nginx

企业 - nginx


企业 - nginx

[[email protected] Desktop]# cd /etc/cluster/
[[email protected] cluster]# ls
fence_xvm.key
[[email protected] cluster]# dd if=/dev/urandom of=/etc/cluster/fence_xvm.key bs=128 count=1  做钥匙


企业 - nginx


[[email protected] cluster]# scp fence_xvm.key [email protected]:/etc/cluster
[email protected]'s password:
fence_xvm.key                                     100%  128     0.1KB/s   00:00    
[[email protected] cluster]# scp fence_xvm.key [email protected]:/etc/cluster
[email protected]'s password:
fence_xvm.key                                     100%  128     0.1KB/s   00:00   




企业 - nginx


企业 - nginx



对集群所做的操作,我们可以在/etc/cluster目录下查看

企业 - nginx




企业 - nginx




[[email protected] cluster]# nginx -s stop    关掉nginx服务

写脚本,改执行方法

[[email protected] init.d]# chmod +x nginx    加执行权限
[[email protected] init.d]# /etc/init.d/nginx start   开启服务
server4同样操作



企业 - nginx


企业 - nginx



测试,当关掉server1的nginx服务后

企业 - nginx

vip漂移

企业 - nginx


客户访问不会出现问题

企业 - nginx



转载于:https://blog.51cto.com/13362895/2083807

相关文章: