Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了③.nginx location大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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全部内容,希望文章能够帮你解决③.nginx location所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: