第一次使用
配置第一台虚拟主机:
server {
listen 80 default_server; //设置此台虚拟主机为默认虚拟主机
#server_name localhost;
server_name test.com; //虚拟主机的域名或者Ip地址
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
root /tan/html; //文件的根目录
index a.html; //虚拟主机的首页
}
文件路径如下:
如果需要域名访问,则需要将ip绑定到对应域名即可,启动nginx,访问效果如下:
nginx 路由
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#统一配置nginx项目的配置文件
include /etc/nginx/*.conf;
}
查看配置是否正确:
配置文件路径如下:
location 配置
1、location = / 精准匹配
= 号开头表示精准匹配
2、location ^~ /static/ 一般匹配
^~开头表示uri以某个常规字符串开头,禁止正则匹配
3、ocation ~* .(gif|png|css|js)$ 正则匹配
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
4、/ 表示用户所使用的代理(一般为浏览器)
我们按着流程图的匹配规则设计出对应的测试用例:
精准匹配
http {
include mime.types;
# default_type application/octet-stream;
#设置默认返回浏览器的文件类型为数据
default_type text/plain;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#统一配置nginx项目的配置文件
include /etc/nginx/*.conf;
}
#location指令优先级验证:
# = 精准匹配,优先级最高,匹配就直接返回
# ~,~* 为正则匹配
# / 表示用户所使用的代理
server {
listen 80;
server_name 121.4.182.15;
#精准匹配测试
#第1,2条虽然匹配,但第三条是精准匹配,出第三条结果
#测试路径/equal/a/b/c
location ~ /equal/* {#被命中,但被下面的推断:location = /equal/a/b/c
echo '/equal/*';
}
location /equal/a/b {#被命中,但被下面的推断:location = /equal/a/b/c
echo '/equal/a/b';
}
location = /equal/a/b/c {#被命中,直接执行,不等待
echo '/equal/a/b/c';
}
}
效果如下:
= 符号匹配最优先
一般匹配多个匹配规则
#普通匹配测试
#第1,2条虽然匹配,第三条匹配更长,出第三条结果
#测试路径/match/a/b/c
location /match/a {#被命中,但不是最长
return 200 "/match/a";
}
location /match/a/b {#被命中,但不是最长
return 200 "/match/a/b";
}
location /match/a/b/c {#被命中,且最长
return 200 "/match/a/b/c";
}
location /match/a/b/c/d {#不命中
return 200 "/match/a/b/c/d";
}
效果如下:
路径最长匹配最优先
正则匹配
#正则匹配覆盖普通匹配测试
#会覆盖普通匹配,不会覆盖=和^~
location =/re/a.js {#访问/re/a.js,不会被后面的正则覆盖
echo 'match =';
}
location ^~ /re/a/b {#访问/re/a/b开头的路径,不会被后面的正则覆盖
echo 'math ^~/re/a/b*';
}
location /re/a.htm {#访问/re/a.htm,会被后面的正则覆盖
echo 'match /re/a.htm';
}
location ~ /re/(.*)\.(htm|js|css)$ {#覆盖/re/a.htm路径
echo "cover /re/$1.$2";
}
#正则匹配成功一条后,便不再走其它正则
#最长正则匹配是第三个,但匹配第一个后便不往下走
#测试路径/rex/a/b/c.htm
location ~ /rex/.*\.(htm|js|css)$ {#覆盖/re/a.htm路径
echo "match first";
}
location ~ /rex/a/(.*)\.(htm|js|css)$ {#覆盖/re/a.htm路径
echo "match second";
}
location ~ /rex/a/b/(.*)\.(htm|js|css)$ {#覆盖/re/a.htm路径
echo "match third";
}
location / {
root /etc/nginx/html;
index c.html;
}
效果如下:
总结:
1、=精准匹配命中时,停止location动作,最接走精准匹配
2、一般匹配命中时,先收集所有的普通匹配,最后对比出最长的那一条
3、如果最长的那一条普通匹配为非正则,直接匹配此条,停止location
nginx安装echo模块
##下载第三方模块
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
tar -zxvf v0.61.tar.gz ##解压
cd nginx-1.15.8 ##进入 nginx 源码目录,准备重新配置 nginx
##配置,–add-module 指向模块的解压文件目录即会安装插件到 nginx 中
./configure --add-module=/usr/local/src/echo-nginx-module-0.61/
make && make install 命令重新编译安装加完新模块后的nginx
查看插件是否安装成功:
nginx 代理
动态代理
server {
listen 80;
server_name 121.4.182.15;
#后台服务原始路径:http://124.221.132.27:5601/api/saved_objects/_find?fields=title&per_page=10000&type=index-pattern
#无/,访问路径:http://121.4.182.15/api/saved_objects/_find?fields=title&per_page=10000&type=index-pattern
location /api{
#此处未关闭,传递整个路径/app到目标ip:port
proxy_pass http://124.221.132.27:5601;
}
#有/,访问路径 :http://121.4.182.15/api/saved_objects/_find?fields=title&per_page=10000&type=index-pattern
location /nginx/api {#匹配路径/dynamic,剩余路径/nginx/enjoy/getInfo
proxy_pass http://124.221.132.27:5601/api;
}
}
如果代理目标:ip+端口,则直接把api/saved_objects/_find?fields=title&per_page=10000&type=index-pattern补在代理地址后面。
如果代理目标:ip+端口+项目名称,则至二级把/nginx/api 替换成/api,并将/saved_objects/_find?fields=title&per_page=10000&type=index-pattern给补在api地址的后面。
静态代理
#访问路径:http://121.4.182.15/static/d.html
location /static {#匹配路径/static,剩余路径/a.html
root /etc/nginx/html/;#root声明,在html文件夹,查找/static/a.html文件
}
#访问路径:http://121.4.182.15/target/d.html
location /target {#匹配路径/target,剩余路径/a.html
alias /etc/nginx/html/static/;##alias声明,在static/文件夹,查找a.html文件
}
root:无法自定义路径名称;alias:自定义路径名称
负载均衡 upstream使用
upstream nginx {
server 172.17.0.4:8081 weight=2;
server 172.17.0.5:8081 weight=1;
}
nginx的upstream常规使用
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。down 暂时不参与负载
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream使用实例
1、首先先部署俩台后台Tomcat服务器
第一台服务器:
2、将俩台Tomcat上的web项目负载配置到nginx代理服务器上
upstream test {
ip_hash;
server 124.221.132.27:8080 weight=3;
server 124.221.132.27:8081 weight=1;
}
server {
listen 80;
server_name 121.4.182.15;
location /test/nginx/tan{
##后台请求为:http://124.221.132.27:8080/nginx/tan/getPage
##调整后请求:http://124.221.132.27:8081/nginx/tan/getPage
##故代理需要关闭path1的传递
proxy_pass http://test/nginx/tan;
}
}
3、测试
rewrite使用
rewrite regex replacement [flag];
flag=【break/last/redirect/permanent】
regex 是正则表达式
replacement 是替换值,新值
flag – 后续处理标识
1、flag=break 发生 nginx 内部重定向,path 值被更新,rewrite 层面的命令会中断。原控制流程逻辑不变往下走
2、flag=last 发生 nginx 内部重定向,path 值被更新,rewrite 层面的命令会中断。控制流程刷新,重新进 行整个 location 层的逻辑流程。
3、flag= redirect/permanent 发生页面重定向(301 永久重定向/302 临时重定向),nginx 流程结束,返回 http 响应到浏览 器,页面 url 更新
4、flag 为空 发生 nginx 内部重定向,path 值被更新,rewrite 层面的命令继续。最后一个 rewrite 完毕, 刷新控制流程,重新进行 location 重匹配
server {
listen 80;
server_name 121.4.182.15;
location /a.html {
echo 'I am a.html';
}
location /b.html {
echo 'I am b.html';
}
#此路径请求:http://121.4.182.15/aa.html
location /aa.html {##内部重定向
rewrite ^/ /a.html break;##停止指令,流程不变往下走
rewrite ^/ /b.html break;
root /etc/nginx/html/;
}
#此路径请求:http://121.4.182.15/ab.html
location /ab.html {
rewrite ^/ /a.html last;##停止指令,但重新location匹配
rewrite ^/ /b.html last;
rewrite ^/ /c.html;
root /etc/nginx/html/;
}
#此路径请求:http://121.4.182.15/bb
location /bb {
rewrite ^/ /b.html redirect;##302临时重定向
set $aa 12;
root html/;
}
#此路径请求:http://121.4.182.15/ba
location /ba {
rewrite ^/ /b.html permanent;##301永久重定向
root html/;
}
#此路径请求:http://121.4.182.15/cc.html
location /cc.html {
rewrite ^/ /c.html;##指令不停,继续往下
rewrite ^/ /b.html;
rewrite ^/ /a.html;##最后一条,生效的是这条
root /etc/nginx/html/;
}
}