在nginx的配置文件中配置多个相同的server_name
案例
[root@C7--01 ~]# cd /usr/local/nginx/html
[root@C7--01 html]# mkdir aaa
[root@C7--01 html]# cd aaa
[root@C7--01 aaa]# mkdir bbb{1..3}
[root@C7--01 aaa]# for i in {1..3};do echo "hoell c7--0$i
" > bbb"$i"/index.html;done
[root@C7--01 aaa]# cat bbb*/index.html
hoell c7--01
hoell c7--02
hoell c7--03
[root@C7--01 ~]# cd /usr/local/nginx/conf
[root@C7--01 conf]# cp nginx.conf nginx.conf.bf
[root@C7--01 conf]# sed -i '/.*#/d' nginx.conf #删除注释的行
[root@C7--01 conf]# sed -i '/^$/d' nginx.conf #删除空行
[root@C7--01 aaa]# cd ..
[root@C7--01 html]# vim ../conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name name1 192.168.2.1;
location / {
root /aaa/bbb1;
index index.html index.htm;
}
}
server {
listen 80;
server_name name2 192.168.2.1;
location / {
root /aaa/bbb2;
index index.html index.htm;
}
}
server {
listen 80;
server_name name3 192.168.2.1 ;
location / {
root /aaa/bbb3;
index index.html index.htm;
}
}
}
访问方法一:
[root@C7--01 ~]# curl 192.168.2.1
hoell c7--01
方法二:
结论:当server_name一样的时候,访问优先级是从上往下的
在一个server_name中出现多个相同的location
优先级从高到低 | |
= |
精确匹配 |
正则(正则表达式)匹配 | 更精确匹配的location |
^~ | 普通路径前缀匹配 |
~ | 区分大小写 |
~* | 不区分大小写 |
/ | 默认匹配 |
案例
[root@C7--01 ~]# cd /usr/local/nginx/conf
[root@C7--01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.2.1;
root html/aa;
index index.html;
location ~ /bbb* {
rewrite ^(.*)$ /bbb3/index.html break;
}
location ^~ /bbb {
rewrite ^(.*)$ /bbb2/index.html break;
}
location = /bbb1/ {
rewrite ^(.*)$ /bbb1/index.html break;
}
# location / {
# index index.html index.htm;
# }
}
}
保存退出
[root@C7--01 conf]# nginx -s reload
当访问bbb1的时候:= 的优先级最高 精确匹配
当访问bbb2时候:^~ 的优先级第二高
当访问bbb3时候我们发现访问的是bbb2的内容: 说明 ~ 的优先级小于^~
结论:匹配的范围越小,优先级越高
try_files:按照顺序检查文件是否存在
$uri :判断bbb2目录是否存在
$uri/ :如果bbb2目录存在,那么就将目录下的index.html文件解析之后返回给客户端;如果index.html不存在,那就看当前的bbb1目录(或者交给反向代理)下是否存在index.html页面
@表示配置文件中预定义标记点(如果没有匹配成功那么就匹配@***下面的location@***进行)
案例
[root@C7--01 ~]# cd /usr/local/nginx/html/
[root@C7--01 html]# mkdir aaa
[root@C7--01 aaa]# ls #创建两个html网页
index.html ini.aaa
[root@C7--01 aaa]# cat index.html
qqqqqqqqqqq
bbbb2
[root@C7--01 aaa]# cat ini.aaa
1355615315
[root@C7--01 aaa]# cd ../../conf/
[root@C7--01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.2.1;
location / {
root html/aaa; #添加aaa目录路径
index index.html index.htm ini.aaa; #添加ini.aaa
try_files $uri $uri/ ini.aaa; #如果index.html不存在那么就返回ini.aaa网页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
保存
[root@C7--01 conf]# nginx -s reload
删除aaa下面的index.html网页
[root@C7--01 conf]# ls ../html/aaa/
ini.aaa
[root@C7--01 conf]# nginx -s reload
删除index.html 发现访问的是ini.aaa文件说明实验成功
alias和root处理方式:
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
案例
[root@C7--01 conf]# vim nginx.conf
.....
.......
index index.html;
location /bbb1 {
root html/aa;
}
location /bbb3 {
alias /usr/local/nginx/html/aa/bbb1/;
}
保存退出
[root@C7--01 conf]# nginx -s reload
结论
共同点:Root和alias都是用来指定网页存放路径的
不同点:root可以用相对路径, 而alias必须是绝对路径
rewrite 主要实现 url地址重写,以及重定向
Rewrite使用场景
1.URL访问跳转:支持开发设计,页面跳转,兼容性支持,展示效果
2.SEO优化:依赖于url路径,以便支持搜索引擎录入
3.维护:后台维护,流量转发等
4.安全:伪静态
配置语法:rewrite ^(.*)$ /aaa/index.html
正则表达式 | |
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一次 |
. | 匹配除“\n”之外的任何单个字符。使用诸如“[.\n]”之类的模式,可匹配包括“\n”在内的任意字符 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用 |
\d | 匹配纯数字 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
[c] | 匹配单个字符c |
[a-z] | 匹配a-z小写字母的任意一个 |
[a-zA-Z] | 匹配a-z小写字母或A-Z大写字母的任意一个 |
案例:
( ) 用于匹配括号之间的内容,通过$1,$2...调用 ; chrome(谷歌浏览器)
location /aaa {
root html;
index index.html index.htm;
if ($http_user_agent ~ Chrome){
rewrite ^(.*)$ /chrome/$1 break;
}
}
正则表达式测试工具
使用网络yum源进行安装
[root@C7--01 ~]# which pcretest
/usr/bin/pcretest
[root@C7--01 ~]# pcretest #使用命令
PCRE version 8.32 2012-11-30
re> /(\d+)\.(\d+)\.(\d+)\.(\d+)/ #编写正则
data> 192.168.1.1 #验证
0: 192.168.1.1
1: 192
2: 168
3: 1
4: 1
#退出ctrl + c
flag标志位 | |
last | 终止执行rewrite模块指令集,并开始搜寻重写url后匹配的location |
break | 停止执行当前虚拟主机的后续rewrite指令集 |
redirect | 302临时重定向,地址栏会显示跳转后的地址,爬虫不更新URL |
permanent | 301永久重定向,地址栏会显示跳转后的地址,爬虫更新URL |
last与break的区别:last会发起新的location匹配,而break不会
[root@C7--01 conf]# vim nginx.conf #ccc为不存在文件
...........
.....
server {
listen 80;
server_name 192.168.2.1;
root html/aa;
index index.html;
location /bbb1/ {
rewrite ^/bbb1 /ccc/ break;
}
location /bbb2/ {
rewrite ^/bbb2 /ccc/ last;
}
location /ccc/ {
return 200 "ok????????????";
}
[root@C7--01 conf]# nginx -s reload
测试 访问:http://192.168.2.1/bbb1/
访问 :http://192.168.2.1/bbb2/
结论
break:匹配后不会进行重新发起一个请求,只会查找对应root目录下包含ccc目录,当发现不存在ccc目录时候,就会报错
last:停止当前这个请求,并根据rewrite匹配的规则重新发起一个请求
格式 : 访问请求域名+/ccc/
例如: 客户端访问的地址是:http://192.168.2.1/bbb2
|| || ||
实际上访问的是 :http://192.168.2.1/ccc
redirect与permanent的对比
配置文件
[root@C7--01 conf]# vim nginx.conf
root html/aa;
index index.html;
location /bbb3 {
rewrite ^/bbb3 http://192.168.2.1/bbb3/ redirect; #临时跳转302
}
location /bbb2 {
rewrite ^/bbb2 http://192.168.2.1/bbb4/ permanent; #永久跳转301
}
测试结果
[root@C7--01 ~]# curl http://192.168.2.1/bbb2/
301 Moved Permanently #永久跳转301
301 Moved Permanently
nginx/1.18.0
[root@C7--01 ~]# curl http://192.168.2.1/bbb3/
302 Found #临时跳转302
302 Found
nginx/1.18.0
结论:尽量在linux上测试效果明显,实际上permanent是表现在缓存在的一段时间内,而redirect则是不缓存的本地;如果使用windows进行访问效果不明显
Rewrite 的匹配优先级
1、执行server块的rewrite命令
2、执行location匹配
3、执行选定的location中的rewrite命令
nginx的优化 |
1、gzip压缩优化 |
2、expires静态文件缓存 |
3、调整网络IO模型,调整进程的最大连接 |
4、隐藏nginx名称和版本号 |
5、防盗链优化 |
6、禁止通过IP地址访问网站,禁止恶意域名解析,只允许域名访问 |
7、发DDOS、cc攻击,限制单IP并发请求连接 |
8、nginx日志相关优化访问日志切割轮询,不记录指定元素日志、最小化日志目录权限 |
9、限制上传到资源目录的程序被访问,防止木马入侵系统破坏文件 |
10、nginx加密传输优化(SSL) |
案例:禁止通过IP地址访问网站,禁止恶意域名解析,只允许域名访问
进行配置
[root@C7--01 conf]# vim nginx.conf
orker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name _; #设置为 _
return 500; #为500报错
root html/aa;
location /bbb1 {
index index.html index.htm;
}
}
server {
listen 80;
server_name www.aa.com; #设置名称为域名
root html/aa; #指定路径
location /bbb1 { #index.html的父目录
index index.html index.htm;
}
}
}
保存
[root@C7--01 conf]# nginx -s reload
修改linux的hosts文件和windows7 的hosts文件 省略......... 设置内容为192.168.2.1 www.aa.com
访问测试
使用地址访问访问失败
使用域名访问访问成功