-
nginx所在的位置
client -> cdn -> lvs(4) -> nginx(7)
-
nginx 源码安装
tar zxf nginx-1.10.1.tar.gz
取消nginx的版本显示
(防止有人知道这个版本的漏洞而进行针对性攻击,因此不对外显示,比较安全)
/root/nginx-1.10.1/src/core
14 #define NGINX_VER "nginx/"
取消debug模式,装的包比较小
/root/nginx-1.10.1/auto/cc
178 # debug
179 #CFLAGS="$CFLAGS -g"
将这两行注释掉
安装它的依赖性
yum install -y pcre-devel openssl-devel zlib-devel
编译
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
make install
nginx #启动
nginx -s reload #重新加载
nginx -s stop #关闭
nginx -t #检测配置文件
[root@server1 conf]# cat nginx.conf
user nginx;
worker_processes 1; #常见三个参数 1 2 auto自动 ps ax lscpu
#worker_cpu_affinity 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;
# 并发量控制
# 修改此处也要修改etc/security/limits.conf 最后一行添加
# nginx - nofile 65535
}
http {
upstream westos { #调度器设置
#ip_hash; #采用hash算法(访问真实服务器和你的ip绑定,采用hash不能写backup,负责无法启动)
server 172.25.200.3:80 weight=3; #weight 权重
server 172.25.200.4:80; #后面加 backup 可以作为后台
server 127.0.0.1:80 backup; 如果上面都挂掉这台服务器顶替
}
#虚拟主机设置,最后面行添加
server {
listen 80;
server_name www.westos.org;
location / {
# root /www1;
proxy_pass http://westos; #调用上面的 upstream 22行
}
}
}
测试
在客户机中添加域名解析(必须添加,因为前面配置中已经指定域名)
curl 172.25.12.1
源码编译(给轮询算法中添加sticky模式,与浏览器绑定)
nginxtar zxf nginx-sticky-module-ng.tar.gz
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng #重新配置--add-module添加模块
make
make install
进入nginx的配置文件中,修改
http {
upstream westos { #调度器设置
sticky;
server 172.25.200.3:80 weight=3; #weight 权重
server 172.25.200.4:80; #后面加 backup 可以作为后台
#server 127.0.0.1:80 backup; 如果上面都挂掉这台服务器顶替
}
在浏览器中访问就只能访问其中一台服务器