cd /pub ##切换到你软件包的位置
ls
tar zxf nginx-1.14.0.tar.gz ##解压
ls
cd nginx-1.14.0
ls
vi scr/core/nginx.h ###删掉型号 define NGINX_VER "nginx" ##商业一般不要别人知道自己的型号
vi auto/cc/gcc ##注销debug 下一行含g的行
yum install pcre-devel gcc openssl-devel ##解决依赖性
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio #源码编译
make && make install
cd /usr/local
ls
cd nginx
ls
cd sbin
ls
./nginx ##开启nginx
curl -I localhost (应该没有了型号)
du -sh (应该很小)
257 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
258 which nginx##应该是链接之后的位置
263 cd /usr/local/nginx/conf
264 ls
265 lscpu (显示cpu的核数)
268 vi nginx.conf ##nginx的主配置文件在:/usr/local/nginx/conf/nginx.conf
更改用户名 cpu核数 后端服务器 在最后加 …6处更改
user nginx nginx;
worker_processes 1; ##nginx的工作进程数,因为改虚拟机的cpu核数为1
worker_cpu_affinity 01; ##如果cpu核数为2 ,则为01 10 ,4 0001,0010,0100,1000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535; ##进程最大连接数,一个cpu为65535,2个为65525*2
}
http {
upstream westos{
#ip_hash ##负载均衡的算法,不写算法默认使用rr (轮询)
server 172.25.17.12:80 weigth=2 #权重为2 调用较多 down 手动挂掉;
server 172.25.17.13:80; ##此时2和3的服务要开httpd
server 172.25.17.11:8080 backup ##后端服务器全坏的主备 ;##本机的ip 需要下载httpd 开启服务,并改端口;
}
server {
listen 80; ##nginx监听80端口
server_name www.westos.org; ##访问域名 www.westos.org
location / {
proxy_pass http://westos; ##访问上边的虚拟主机
}
}
}
269 vi /etc/security/limits.conf nginx - nofile 65535 ##使nginx工作在nginx的用户空间
270 useradd -M -d /usr/local/nginx -u 800 nginx #增加nginx用户并指定家目录
271 id nginx
272 su - nginx ulimit -a
ulimit -a (显示所有的限制)
277 ls
280 nginx -t ##检测句法错误
nginx -s reload ##重新加载
在真机里面可以看见轮询 www.westos.org
将server2的权重改为2,weight=2,则出现以下轮询
###负载均衡算法ip_hash(同一个ip访问后端服务器不变)
在nginx中静态添加模块sticky
##sticky是nginx的一个模块,他是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,是来自同一个客户端的请求落在统一台服务器上,默认标示名为route
1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询的方氏将请求分发给后端服务器
2.后端服务器处理完请求,将返回数据返回给nginx
3.此时nginx生成带route的cookie,返回给客户端,route的值与后端服务器对应可能是明文,也可能是hash值
4.客户端接受请求,并保存带route的cookie
5.当客户端下一次发送请求时,会带上route,nginx会受到cookie值,转发给后端服务器
####连续加载
317 nginx -s stop ##关闭之前的nginx
318 ls
yum install unzip
319 unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip #解压这个nginx
stick 模块
320 ls
324 cd nginx-1.14.0
325 make clean
332 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/pub/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ #必须改变安装路径,不然会覆盖之前的nginx,而且之前添加的参数这次也必须选择(这叫静态添加模块)
333 make && make install
334 cd /usr/local/nginx/conf
vi nginx.conf
http {
upstream westos{
sticky;
server 172.25.17.12:80;
server 172.25.17.13:80;
}
337 nginx ##开启服务
338 nginx -t ##检测
339 nginx -s reload 加载
###可见浏览器将后端服务器中的信息缓存,以便下次使用