一、安装nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx
1.下载并解压nginx安装包nginx-1.14.0.tar.gz
[root@server1 ~]# ls
nginx-1.14.0.tar.gz
[root@server1 ~]# tar zxf nginx-1.14.0.tar.gz
2.修改配置文件
[root@server1 ~]# ls
nginx-1.14.0 nginx-1.14.0.tar.gz
[root@server1 ~]# cd nginx-1.14.0
[root@server1 nginx-1.14.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@server1 nginx-1.14.0]# cd src
[root@server1 src]# ls
core event http mail misc os stream
[root@server1 src]# cd core
[root@server1 core]# vim nginx.h
[root@server1 core]# cd ..
[root@server1 src]# cd ..
[root@server1 nginx-1.14.0]# cd auto/
[root@server1 auto]# ls
cc feature headers install module options stubs types
define have include lib modules os summary unix
endianness have_headers init make nohave sources threads
[root@server1 auto]# cd cc/
[root@server1 cc]# vim gcc
3.安装nginx插件
[root@server1 cc]# yum install gcc -y
[root@server1 cc]# yum install pcre-devel -y
[root@server1 cc]# yum install openssl-devel -y
4.编译安装
[root@server1 cc]# cd ..
[root@server1 auto]# cd ..
[root@server1 nginx-1.14.0]# ./configure --help
--prefix=PATH set installation prefix
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--with-threads enable thread pool support
--with-file-aio enable file AIO support
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-threads --with-http_stub_status_module --with-file-aio
[root@server1 nginx-1.14.0]# make
[root@server1 nginx-1.14.0]# make install
编译安装完成!
[root@server1 nginx-1.14.0]# cd /usr/local/nginx/
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# du -sh
980K .
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/
# nginx的相关命令:
[root@server1 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
[root@server1 sbin]# nginx #开启
[root@server1 sbin]# nginx -s stop #停止
[root@server1 sbin]# nginx -s reload #重新加载
网页测试:
二、修改nginx文件
[root@server1 sbin]# cd ..
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html index.html
[root@server1 html]# vim test.html
<h1>www.westos.org</h1>
网页测试:172.25.8.1/test.html
[root@server1 ~]# useradd -M -d /usr/local/nginx/ nginx #建立nginx用户,家目录为/usr/local/nginx/
[root@server1 ~]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
user nginx nginx; #用户为nginx,组为nginx
worker_processes 2; #进程,如果是4,下面应该写0001,0010,0100,1000
worker_cpu_affinity 01 10;
events {
worker_connections 65535;
}
http {
upstream westos{
server 172.25.8.2:80; #RID
server 172.25.8.3:80; #RID
}
server {
listen 80; #监听端口为80
server_name www.westos.org;
location /{
proxy_pass http://westos;
}
}
}
[root@server1 ~]# sysctl -a |grep file
fs.file-nr = 576 0 98863
fs.file-max = 98863
[root@server1 ~]# vim /etc/security/limits.conf
nginx - nofile 65536
重新加载
[root@server1 conf]# nginx -s reload
查看进程
[root@server1 conf]# ps aux
nginx 5140 0.1 2.8 74952 29172 ? S 16:47 0:00 nginx: worker proces
nginx 5141 0.3 2.8 74952 29196 ? S 16:47 0:00 nginx: worker proces
在物理机加解析
[root@foundation8 ~]# vim /etc/hosts
172.25.8.1 www.westos.org
网页测试:
server2关掉http服务
[root@server2 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
只能catch到server3
server3关掉http服务
[root@server3 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
开启server2和server3http服务
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# /etc/init.d/httpd start
在nginx配置文件中加入hash函数
[root@server1 conf]# vim nginx.conf
ip_hash;
[root@server1 conf]# nginx -s reload #重新加载nginx
网页测试:
一直都是server2
[root@server1 conf]# vim nginx.conf
http {
upstream westos{
#ip_hash;
server 172.25.8.2:80 weight=2; #权重为2
server 172.25.8.3:80;
}
[root@server1 conf]# nginx -s reload
测试:
[root@server1 conf]# vim nginx.conf
http {
upstream westos{
#ip_hash;
server 172.25.8.2:80;
server 172.25.8.3:80;
server 127.0.0.1:80 backup;
}
[root@foundation8 ~]# curl www.westos.org
三、安装两个nginx模块
下载解压nginx-1.10.1.tar.gz
[root@server1 ~]# ls
nginx-1.10.1.tar.gz nginx-1.14.0.tar.gz
nginx-1.14.0 nginx-sticky-module-ng.tar.gz
[root@server1 ~]# nginx -s stop
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 ~]# ls
nginx-1.10.1 nginx-1.14.0.tar.gz
nginx-1.10.1.tar.gz nginx-sticky-module-ng
nginx-1.14.0 nginx-sticky-module-ng.tar.gz
编译安装
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-threads --with-http_stub_status_module --with-file-aio --add-module=/root/nginx-sticky-module-ng
# 修改安装路径为/opt/nginx
[root@server1 nginx-1.10.1]# make
[root@server1 nginx-1.10.1]# make install
两个nginx模块路径不同
[root@server1 nginx-1.10.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@server1 nginx-1.10.1]# cd conf
[root@server1 conf]# ls
fastcgi.conf koi-utf mime.types scgi_params win-utf
fastcgi_params koi-win nginx.conf uwsgi_params
[root@server1 conf]# pwd
/root/nginx-1.10.1/conf
[root@server1 conf]# cd /opt/nginx/conf/
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
修改配置文件
[root@server1 conf]# cp /usr/local/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? y
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# vim nginx.conf
http {
upstream westos{
#ip_hash;
sticky;
server 172.25.8.2:80;
server 172.25.8.3:80;
#server 127.0.0.1:80 backup; #注释
}
[root@server1 conf]# /opt/nginx/sbin/nginx -t #检测
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /opt/nginx/sbin/nginx #重启
负载均衡测试:
但是不影响网页(网页一直显示server2)
按F12键
点击右上角设置图标
勾选storage
点击中间的storage,可以看到测试情况