是一个高性能的HTTP和反向代理服务,工作在网络的7层之上,可以针对http应用做一些分流的策略, 比如针对域名、目录结构,它的正则规则比HAProxy更为强大 和灵活,这也是它目前广泛流行的主要原因之一,Nginx单凭 这点可利用的场合就远多于LVS了。Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.
iptables和selinux关闭
1.操作系统:rhel6.5
2.主机:
物理主机:172.25.254.68
server2 : 172.25.254.2 负载均衡器
Server3: 172.25.254.3 web01服务器
server4: 172.25.254.4 web02服务器
tar zxf nginx-1.14.0.tar.gz
cd nginx-1.14.0
vim src/core/nginx.h #把版本号隐藏
vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #注释这行
yum install gcc openssl-devel -y pcre-devel #安装一些依赖性
第一步生成makefile
第二步读取文件生成的二进制文件
第三步开始安装
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #制作软链接
nginx -t 检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
nginx -s stop 关闭服务
nginx 打开服务
nginx -s reload 重载服务
curl -I localhost
把server2的CPU核数改为2
lscpu
cd /usr/local/nginx/conf/
vim nginx.conf
useradd -M -d /usr/local/nginx/ -u 800 nginx #nginx建立用户默认组也为nginx
id nginx
nginx
nginx -t #检查语法错误
nginx -s reload
vim /etc/security/limits.conf
vim /usr/local/nginx/conf/nginx.conf 编辑主配置文件
server 2 nginx配置文件:
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream westos{ #这里是定义Web服务器池,包含了 3.4两个Web节点
server 172.25.254.3:80;
server 172.25.254.4:80;
}
server { #这里是定义代理的负载均衡域名虚拟主机
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos; #访问www.westos.org,请求发送给jaff里面的节点
}
}
}
nginx -t
nginx -s reload
/etc/init.d/httpd start
vim /etc/hosts
172.25.254.2 server2 www.westos.org
curl www.westos.org
vim /usr/local/nginx/conf/nginx.conf
nginx -t
nginx -s reload
yum install -y httpd
vim /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080
/etc/init.d/httpd restart
vim /var/www/html/index.html
网站维护中.......
/etc/init.d/httpd stop
vim /usr/local/nginx/conf/nginx.conf
http {
upstream westos{
server 172.25.2.3:80 weight=2;
server 172.25.2.4:80;
server 172.25.2.2:8080 backup;
}
nginx -t
nginx -s reload
vim /usr/local/nginx/conf/nginx.conf
nginx -t
nginx -s reload
测试:
curl localhost
Sticky工作原理 Sticky是nginx的一个模块,通过分发和识别 cookie,来使同一个客户端的请求落在同一台服务器上。sticky 的处理过程如下(假设cookie名称为route):
1.客户端首次发起请求,请求头未带route的cookie。nginx接收请求, 发现请求头没有route,则以轮询方式将请求分配给后端服务器。
2.后端服务器处理完请求,将响应头和内容返回给nginx。
3.nginx生成route的cookie,返回给客户端。route的值与 后端服务器对应,可能是明文,也可能是md5、sha1等Hash值。
4.客户端接收请求,并创建route的cookie。
5.客户端再次发送请求时,带上route。 6.nginx接收到route,直接转给对应的后端服务器
cd /mnt
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/mnt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
make && make install
cd /usr/local/nginx/conf/
vim nginx.conf
在浏览器测试:www.westos.org 浏览器才有cookie,curl命令不生效
nginx的配置多个基于域名的虚拟主机:
在server4上配置:
[root@server4 html]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.westos.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.westos.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.westos.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
[root@server4 html]# nginx -s reload
[root@server4 ~]# cd /usr/local/nginx/html/
[root@server4 html]# mkdir blog
[root@server4 blog]# vim index.html
[root@server4 html]# mkdir www
[root@server4 html]# cd www/
[root@server4 www]# vim index.html
[root@server4 html]# mkdir bbs
[root@server4 bbs]# vim index.html
测试:
[root@foundation68 ~]# vim /etc/hosts
172.25.254.4 server4 bbs.westos.org www.westos.org blog.westos.org
[root@foundation68 ~]# curl www.westos.org
WWW
[root@foundation68 ~]# curl blog.westos.org
BLOG
[root@foundation68 ~]# curl bbs.westos.org
BBS