nginx
l架构图
graph LR
A[Clinet]-->B[Nginx 1<br>Keepalive<br>192.168.137.81]
A-->C[Nginx 2<br>Keepalived<br>192.168.137.82]
B-->D[Switch]
C-->D
D-->E[WEB1<br>192.168.137.86]
D-->F[WEB2<br>192.168.137.87]
安装Nginx1
- centos 7.2已自带pcre和zlib,请参考代码块2
上传 nginx-1.16.0.tar.gz、 pcre-8.00.tar.gz、 zlib-1.2.11.tar.gz至 /usr/local/src/目录。下面命令块可直接复制。附带注释。
groupadd www;
useradd -g www www;
yum install -y gcc automake autoconf libtool make gcc-c++ openssl openssl-devel
cd /usr/local/src
tar zvxf pcre-8.00.tar.gz;tar zvxf zlib-1.2.11.tar.gz;tar -zxvf nginx-1.16.0.tar.gz
cd zlib-1.2.11;./configure;make; make install
cd ../pcre-8.00;./configure ;make;make install
cd /usr/local/src/nginx-1.16.0;
./configure --user=www --group=www --prefix==/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.00 --with-zlib=/usr/local/src/zlib-1.2.11;make&&make install
- centos 7.2已自带pcre和zlib。故只用将nginx安装文件上传到/usr/local/src/
groupadd www;
useradd -g www www;
yum install -y gcc automake autoconf libtool make gcc-c++ openssl openssl-devel
cd /usr/local/src
tar -zxvf nginx-1.16.0.tar.gz
cd /usr/local/src/nginx-1.16.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module;make;make install;
-
groupadd www;
-
useradd -g www www;
-
yum install -y gcc automake autoconf libtool make gcc-c++ openssl openssl-devel
安装编译工具
- cd /usr/local/src
进入src目录
- tar zvxf pcre-8.00.tar.gz;tar zvxf zlib-1.2.11.tar.gz;tar -zxvf nginx-1.16.0.tar.gz
解压
- cd zlib-1.2.11;./configure;make; make install
安装zlib
- cd ../pcre-8.00;./configure ;make;make install
安装pcre
-
cd /usr/local/src/nginx-1.16.0;
-
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.00 --with-zlib=/usr/local/src/zlib-1.2.11;make&&make install
编译安装nginx,可执行文件在/usr/local/nginx/nginx
最简配置
[root@Nginx1 conf]# cat nginx.conf
events
{
}
http
{
server
{
}
}
#
#使用此配置文件即可访问Nginx欢迎页面,即Nginx至少需要此3个模块就可运行
http-->server--.Server_name
[root@Nginx1 server2]# cat /usr/local/nginx/conf/nginx.conf
events
{
}
http
{
server
{
server_name server1.com;
root /usr/local/src/server1;
}
server
{
server_name server2.com;
root /usr/local/src/server2;
}
}
[root@Nginx1 server2]# cat /usr/local/src/server1/index.html
server1
[root@Nginx1 server2]# cat /usr/local/src/server2/index.html
server2
#同一服务器上的nginx根据clinet访问域名,可返回不通得网站。
http-->server-->location
server
{
server test.com;
location /
{
return 201;
}
#默认匹配
location = /nginx
{
return 201;
}
#最精准匹配,使用精准匹配时,必须匹配到具体得文件而非目录,其实这个例子时错误的。
location /nginx
{
return 201;
root /abc/def/;
#用户访问到得是/abc/def/nginx下的内容
alias /abc/def;
#用户访问到得是/abc/def下的内容
}
#只要路径以nginx开头就匹配。linux中nginx*通配符类似效果。
location ~/Nginx/$
{
return 201;
proxy_pass http://www.test.com
#此处会报错,反向代理不支持location的通配符
}
#区分大小写得正则,以Nginx开头,以‘/’结尾
location ~*/Nginx/$
{
return 201;
}
#不区分大小写得正则,以Nginx开头,以‘/’结尾
}
#匹配优先级顺序如下
#1.=
#2.^~ 左边匹配
#3.~和 ~* 正则表达式匹配,带‘*’得差别是不区分大小写
#无匹配符号
#location
rewrite ,proxy_set_header 的使用
rewrite '^/image/[0-9]{2}/([0-9]{3})/(.*)' /image/$2;//变成了 image+第二个小括号中匹配的内容路径