首页 > 其他 > 详细

③.nginx location

时间:2021-04-05 21:20:45      阅读:18      评论:0      收藏:0      [点我收藏+]

nginx程序区域模块 - location

功能:匹配不同的uri信息,做出相应处理
Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }       语法结构
                       匹配          uri  执行什么动作
	    awk       ‘模式{动作}‘
Default:	—
Context:server, location   --- 可以配置在什么区域中
=:		精确匹配     			= /oldboy   www.oldboy.com  /oldboy
~:		模糊匹配(区分大小写)
~*:	模糊匹配(不区分大小写)
^~:     优先匹配 不识别uri信息中正则符号
 /:         默认匹配

如何应用:
location = / {                        01
    return  301;
}

location / {                          05   默认匹配
    return  302;
}

location /documents/ {                04
    return  404;
}

location ^~ /images/ {                02
    return  502;
}

location ~* \.(gif|jpg|jpeg)$ {       03
    return  520;
}

样例:测试 ~ 和 ~*
location ~ /test/ {
    return  301;
}
location ~* /test/ {
    return  302;
}

规范站点目录结构信息:
[root@web01 conf.d]# cat www.conf 
server {
    listen       80;
    server_name  www.oldboy.com;
    location ~* \.jpg$ {
        root  /html/www/jpg;
    }
    location ~* \.png$ {
        root /html/www/png;
    }
    location / {
        root  /html;
        index index.html;
    }
}

nginx程序重写功能说明 rewrite url/uri/伪静态

Syntax:	 rewrite regex          replacement   [flag];
                正则信息匹配   修改成的信息   标记
Default: —
Context: server, location(推荐), if
last:      跳转完毕,会在执行其他动作
    break:     跳转完毕,不在执行其他动作
    redirect    302  临时跳转   *****
    permanent: 301  永久跳转 

1). 跳转配置中last与break区别对比示例
server {
   listen            80;
   server_name       www.oldboy.com;
   root              /html;
   location  ~ ^/break/ {
       rewrite  ^/break/  /test/  break;
   }
   location  ~ ^/last/  {
       rewrite  ^/last/  /test/  last;
   }
   location   /test/ {
       default_type   application/json;
       return 200 ‘ok‘;
   }
}


2) 临时跳转和永久跳转配置
server {
   listen            80;
   server_name       www.oldboy.com;
   root              /html;
   location  ~ ^/oldboy {
       rewrite  ^(.*)$  https://www.etiantian.org redirect;
	   rewrite  ^(.*)$  https://www.etiantian.org permanent;
       # return 301 http://bbs.etiantian.org;
	   # return 302 http://bbs.etiantian.org;
   }
}

永久跳转记录跳转信息:  301
临时跳转不记录跳转信息:302

永久跳转记录跳转信息:  301
临时跳转不记录跳转信息:302

301永久: 用户浏览器  -请求信息->  www.360buy.com --> 301 --> www.jd.com
           客户端                              服务端
		  用户浏览器  -请求信息->  www.360buy.com
		                       ->  www.jd.com     ---> 服务端
总结:在浏览器上记录跳转的缓存信息
          www.jd.com/oldboy.jpg   ---    
          www.jd.com/2017/    404
302临时: 用户浏览器  -请求信息->  www.360buy.com --> 302 --> www.jd.com
           客户端                              服务端
          用户浏览器  -请求信息->  www.360buy.com --> 302 --> www.jjd.com
           客户端                              服务端
总结:在浏览器上不记录跳转的缓存信息
          www.jd.com/oldboy.jpg   --- www.jd.com/2018/

补充:实现地址跳转的方法
第一种:利用rewrite  正则匹配
第二种:利用retrun
  1. 常见跳转示例情况测试说明
    练习1):用户访问/abc/1.html实际上真实访问是/ccc/bbb/2.html

    http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html

    第一里程:准备真实的访问路径
    [root@web03 ~]# mkdir /code/ccc/bbb -p
    [root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

    第二个里程:Nginx跳转配置
    [root@web03 conf.d]# cat ccbb.conf
    server {
    listen 80;
    location / {
    root /html/www;
    index index.html;
    }
    location /abc/ {
    rewrite (.*) /ccc/bbb/2.html redirect;
    #return 302 /ccc/bbb/2.html;
    }
    }

    第三个里程:重启Nginx服务
    [root@web03 ~]# systemctl restart nginx

    练习2):用户访问/2018/ccc/bbb/2.html实际上真实访问是/2014/ccc/bbb/2.html

    http://www.oldboy.com/2018/ccc/bbb/2.html ==> http://www.oldboy.com/2019/ccc/bbb/2.html

    第一次访问网站:
    http://www.oldboy.com/2018/ccc/bbb/2.html --> 跳转
    第二次访问网站:
    http://www.oldboy.com/2019/ccc/bbb/2.html

    第一个里程:准备真实的访问路径
    [root@web03 ~]# mkdir /html/www/2019/ccc/bbb -p
    [root@web03 ~]# echo "2019_ccc_bbb_2" > /html/www/2019/ccc/bbb/2.html

    第二个里程:Nginx跳转配置
    [root@web03 conf.d]# cat www.conf
    server {
    listen 80;
    server_name www.oldboy.com;
    location / {
    root /html/www;
    index index.html;
    }
    location /2018 {
    rewrite ^/2018/(.*)$ /2019/$1 redirect;
    }
    }

    第三个里程:重启Nginx服务
    [root@web03 ~]# systemctl restart nginx

    练习3):用户访问/test目录下任何内容, 实际上真实访问是http://www.oldboy.com
    location /test {
    rewrite (.*) http://www.oldboy.com redirect;
    }

    练习4):用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html

    http://www.oldboy.com/course-11-22-33.html ==> http://www.oldboy.com/course/11/22/33/course_33.html

    第一个里程 准备真实的访问路径
    [root@web03 ~]# mkdir /html/www/course/11/22/33/ -p
    [root@web03 ~]# echo "docs.etiantian.org" > /html/www/course/11/22/33/course_33.html

    第二个里程 Nginx跳转配置
    [root@web03 conf.d]# cat www.conf
    server {
    listen 80;
    server_name www.oldboy.com;
    root /html/www;
    index index.html;
    location / {
    #灵活rewrite ^/course-(.)-(.)-(.).html$ /course/$1/$2/$3/course_$3.html redirect;
    #固定rewrite ^/course-(.
    ) /course/11/22/33/course_33.html redirect;
    }

    第三个里程 重启Nginx服务
    [root@web03 ~]# systemctl restart nginx

    例5:将http请求,跳转至https ???
    server {
    listen 80;
    server_name oldboy.com;
    rewrite ^(.*) https://$server_name$1 redirect;
    #return 302 https://$server_name$request_uri; ???
    }

    server {
    listen 443;
    server_name oldboy.com;
    ssl on;
    }

    终极测验:
    a 访问oldboy.com/oldboy.jpg ---> www.oldboy.com/oldboy.jpg 临时
    第一个历程:准备环境
    将oldboy.jpg --- 站点目录
    第二个历程:编写配置文件:
    server {
    listen 80;
    server_name www.oldboy.com;
    root /html/www;
    index index.html;
    rewrite ^/(.*) http://www.oldboy.com/$1 redirect;
    }

    打破循环问题:
    第一种方式:多个server配置
    server {
    listen 80;
    server_name oldboy.com;
    rewrite ^/(.) http://www.oldboy.com/$1 redirect;
    }
    server {
    listen 80;
    server_name www.oldboy.com;
    root /html/www;
    index index.html;
    }
    第二种方式:打破循环 if 内置变量
    server {
    listen 80;
    server_name www.oldboy.com;
    root /html/www;
    index index.html;
    if ($host ~ ^oldboy.com) {
    rewrite ^/(.
    ) http://www.oldboy.com/$1 redirect;
    }
    }

    nginx常用内置变量:
    $host 记录请求报文请求主体的host信息
    $server_name 当前用户配置server_name的域名信息
    $request_filename 当前请求的文件路径名(带网站的主目录/html/www/images/test.jpg)
    $request_uri 当前请求的文件路径名(不带网站的主目录/images/test.jpg)
    $scheme 用的协议,比如http或者https

    第三个历程:配置DNS解析
    10.0.0.7 oldboy.com www.oldboy.com

    b 访问www.etiantian.org ---> www.oldboy.com 永久
    第一个历程:准备环境
    将oldboy.jpg --- 站点目录
    第二个历程:编写配置文件:
    server {
    listen 80;
    server_name www.oldboy.com;
    root /html/www;
    index index.html;
    if ($host ~ www.etiantian.org) {
    rewrite ^/(.) http://www.oldboy.com/$1 redirect;
    }
    }
    第三个历程:主配置文件编写
    include /etc/nginx/conf.d/www.conf
    include /etc/nginx/conf.d/
    .conf;
    第四个历程:配置DNS解析
    10.0.0.7 oldboy.com www.oldboy.com www.etiantian.org

  2. 部署LNMP架构
    L linux系统: 防火墙/selinux关闭 /tmp 1777
    N nginx程序:
    M mysql程序: mariadb yum安装
    P php程序: 编译安装 yum安装 7.1

    mariadb安装过程:
    yum install mariadb-server mariadb -y

    php安装过程:
    第一个历程:移除其他版本php软件
    yum remove php-mysql php php-fpm php-common

    第二个历程:更新yum源
    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

    第三个历程:yum安装
    yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

③.nginx location

原文:https://www.cnblogs.com/yangtao416/p/14618880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!