Nginx---笔记四

Nginx动静分离:
1.动静分离:
通过中间件将动态请求和静态请求分离

为什么?
分离资源,减少不必要的请求消耗,减少请求延时

场景示例:

Rewrite规则:
1.nginx的rewrite规则:
1.1实现url重写以及重定向
2.场景:
2.1 url访问跳转,支持开发设计
页面跳转,兼容性支持,展示效果等
2.2 SEO优化
2.3 维护:后台维护、流量转发等
2.4 安全
配置语法:
Syntax: rewrite regex replacement[flag];
Default: --
Context:; server,location,if

eg:
rewrite ^(.*)$ /pages/maintain.html break;
3. 正则表达式
3.1 正则表达式了解透彻
3.2 终端测试命令pcretest
>> pcre
re> /(\d+)\.(\d+)\.(\d+)\.(\d+)/
data> 192.168.2.2
data> fdjasfj
4.flag
作用:标记nginx rewrite规则对应的类型
last: 停止rewrite检测
break: 停止rewrite检测
redirect: 返回302临时重定向,地址栏会显示跳转后的地址
permanent: 返回301永久重定向,地址栏会显示跳转后的地址

eg:
server {
listen 8081 default_server;
server_name localhost phantom.wgw.io;

root /opt/LearningNginx/app/code;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 '{"status":"success"}';
}
}

因为break会去匹配目录而last会新建请求,
所以当访问时/break返回404,/last返回/test/的json内容

eg:

server {
listen 8081 default_server;
server_name localhost phantom.wgw.io;

root /opt/LearningNginx/app/code;

location ~ ^/break {
rewrite ^/break /test/ break;
}

location ~ ^/last {
rewrite ^/last /test/ redirect;
}

location ~ ^/wgw{
rewrite ^/wgw https://github.com/ permanent;
rewrite ^/wgw https://github.com/ redirect;
}

location /test/ {
default_type application/json;
return 200 '{"status":"success"}';
}
}

>> curl -vL 192.168.205.10:8081/last # 查看请求过程
>> tail -f /var/log/nginx/access.log
访问 192.168.205.10:8081/wgw
>> nginx -s stop -c /etc/nginx/nginx.conf
再次访问

永久重定向:发起一次响应后,再请求不会重新向服务器发起请求
临时重定向:反之
场景:
假设有这样的一个目录:
/opt/LearnNginx/app/code/course/11/22/course_33.html
如果我们要去访问它:
http://192.168.205.10:8081/opt/LearnNginx/app/code/course/11/22/course_33.html
这样就可以访问到了,但是太麻烦
我们可以这样做:
server {
listen 8081 default_server;
server_name localhost phantom.wgw.io;

root /opt/LearningNginx/app/code;

location / {

rewrite ^/course-(\d+)-(\d+)-(\d+)\.html$ /course/$1/$2course_$3.html break;

if ($http_user_agent ~* Chrome) {
rewrite ^/nginx http://coding.imooc.com/class/121.html redirect;
}

if (!-f $request_filename) {
rewrite ^/(.*)$ https://github.com/$1 redirect;
}

index index.html index.htm;
}

}
5. rewrite规则优先级:
执行server块的rewrite指令
执行location匹配
执行选定的location中的rewrite

6. 优雅的rewrite规则书写:
RewriteCond %{HTTP_HOST} nginx.org
RewriteRule (.*)

eg:
server{
listen 80;
server_name www.nginx.org naginx.org;
if ($http_host=nginx.org){
rewrite (.*) http://www.nginx.org $1;
}
....
}

更推荐以下写法:
server{
listen 80;
server_name nginx.org;
rewrite ^ http://www.nginx.org$request_uri?;
}

server{
listen 80;
server_name www.nginx.org;
}

Nginx 高级模块:

1.secure_link_module模块
作用:
1.制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问
2.限制链接生效周期

语法配置:
Syntax: secure_link expression;
Default: --
Context: http,server,location

Syntax: secure_link_md5 expression;
Default: --
Context: server,location

验证机制:

eg:
>> nginx -V # 确认是已经编译该模块


server {
listen 8081 default_server;
server_name localhost phantom.wgw.io;

root /opt/LearningNginx/app/code;

location / {
secure_link $arg_md5,$arg_expires; # 取MD5对应的参数值,取expires对应的参数值
secure_link_md5 "$secure_link_expires$uri imooc"; # 根据服务端定义的密钥对uri进行指定的加密,以及过期时间,之后 和上面的已经加密的md5和expires进行匹配

# 如果匹配不成功
if ($secure_link = ""){
return 403;
}

if ($secure_link = "0"){
return 410;
}
}

}
2. geoip_module模块
作用:
基于IP地址匹配MaxMind GeoIP二进制文件,读取IP所在地域信息
安装:
>> yum install nginx-module-geoip
>> cd /etc/nginx/modules/
>> ls
>> vi /etc/nginx/nginx.conf
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
user nginx;
...
# 下载国家和城市的geoip
>> wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
>> wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
>> tar -zxf # 解压

场景示例:
1.区别国内外访问,做HTTP访问规则
2.区别国内城市地域做HTTP访问规则

eg:
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/getip/GeoLiteCity.dat;

geoip_city
server {
listen 8081 default_server;
server_name localhost phantom.wgw.io;

root /opt/LearningNginx/app/code;

location / {

if ($geoip_country_code != CN) {
return 403;
}

root /usr/share/nginx/html;
index index.html index.htm;
}

location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $getip_city";
}

}

转载于:https://www.cnblogs.com/wgwblogs/p/11191255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值