zoukankan      html  css  js  c++  java
  • Nginx中location模块的详细配置

    Nginx中location模块的详细配置(含示例)

     
    题记

    此前在配置Nginx location模块的时候玩出了一些bug,折腾了一段时间。后来网上也查阅了相关的资料,看着也比较混乱。周末有空想着好好整理一下location模块的配置,结合自己的亲手实验,总结了location模块的配置。

    location模块配置

    根据匹配特性大概可以分成以下几个部分(按优先级顺序)

    最高优先级(=) 第二优先级(^~) 第三优先级(按照顺序匹配,*) 第四优先级(/)

    1. 匹配即停止

    =:表示精确匹配,要么就匹配上,要么就不会被匹配。如果匹配上了,就进入该location块,其他都不看。
    ~:表示优先匹配,如果按从上往下的顺序匹配到了该~后面的URL,那么就进入该location块,其他都不看。

    2. 按顺序匹配

    ~:表示区分大小写的正则匹配,如果依照自上而下的顺序匹配上URL了,那就不会再继续寻找,即使用这个location块。
    ~*:表示不区分大小写的正则匹配,如果依照自上而下的顺序匹配上URL了,那就不会再继续寻找,即使用这个location块。

    3. 通用匹配

    /:表示任何请求都会被匹配到。

     
     

    以下这段设置是通过 location 指令来对网页 URL 进行分析处理,所有扩展名以 .gif、.jpg、.jpeg、.png、.bmp、.swf 结尾的静态文件都交给 nginx 处理,而 expires 用来指定静态文件的过期时间,这里是 30 天。

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
    root /wwwroot/www.cszhi.com;
    expires 30d;
    }

      以下这段设置是将 upload 和 html 下的所有文件都交给 nginx 来处理,当然,upload 和 html 目录包含在 /web/wwwroot/www.cszhi.com 目录中
      
      location ~ ^/(upload|html)/ {
    root /web/wwwroot/www.cszhi.com;
    expires 30d;
    }

      在最后这段设置中,location 是对此虚拟主机下动态网页的过滤处理,也就是将所有以 .jsp 为后缀的文件都交给本机的 8080 端口处理。
      
      location ~ .*.php$ {
    index index.php;
    proxy_pass http://localhost:8080;
    }

     
     
     
     
     

    location使用举例

    # 输入http://ip+port/images/1.p
    # 此时显示的是'= /images/1.p',因为=匹配优先级最高
    location = /images/1.p {
        default_type 'text/plain';
        echo '= /images/1.p';
    }
    location ^~ /images/1.p {
        default_type 'text/plain';
        echo ' /images/1.p';
    }
    
    # 输入http://ip+port/images/1.p
    # 此时显示到的是'^~ /images/1.p',因为^~只要匹配到了就会停止匹配,哪怕后续的长度更长
    location ^~ /images/ {
        default_type 'text/plain';
        echo '^~ /images/1.p';
    }
    location ~ /images/1.p {
        default_type 'text/plain';
        echo '~ /images/1.p';
    }
    
    # 输入http://ip+port/images/1.pxyzxyz
    # 此时显示到的是'~ /images/',因为这是按照顺序匹配的,匹配到了后面的就不再匹配了
    location ~ /images/ {
        default_type 'text/plain';
        echo '~ /images/';
    }
    location ~ /images/1 {
        default_type 'text/plain';
        echo '~ /images/1';
    }
    
    # 输入http://ip+port/images/  显示'/',因为没有匹配到后面的URL,使用默认的/规则
    # 输入http://ip+port/images/1xyzxyz  显示'~ /images/1',因为匹配到了后面的正则
    location / {
        default_type 'text/plain';
        echo '/';
    }
    location ~ /images/1 {
        default_type 'text/plain';
        echo '~ /images/1';
    }
    
    # 输入http://ip+port/images/ 显示'/images/'
    # 输入http://ip+port/images/1/ab 显示'/images/'
    # 输入http://ip+port/images/1/abc 显示'/images/1/abc'  匹配上第一个location后,会继续向下匹配寻找,如果有更加完整的匹配,则会有下面的。如果没有,则使用当前的。
    location /images/ {
        default_type 'text/plain';
        echo '/images/';
    }
    location /images/1/abc {
        default_type 'text/plain';
        echo '/images/1/abc';
    }
    

    注意事项

    # 在使用“=”的时候会有意外情况,比方说下面这个例子。当输入'http://ip+port/'时,发现返回的状态码是304&404
    # 原因在于Nginx发现请求的是个目录时,会重定向去请求'http://ip+port/index.html',此时返回码是304
    # 而后Nginx收到了'http://ip+port/index.html'这个请求,发现没有location能够匹配,就返回404了
    location = / {
        default_type 'text/plain';
        index index.html index.htm;
        root /web/;
    }
     
  • 相关阅读:
    python测试开发django177.启动项目添加初始化数据(fixtures的使用) 上海
    python笔记70 Python中`__repr__`和`__str__`区别 上海
    python笔记69 什么是猴子补丁(Monkey Patch)? 上海
    pytest文档78 钩子函数pytest_runtest_makereport获取用例执行报错内容和print内容 上海
    python测试开发django176.数据库迁移数据(manage.py dumpdata) 上海
    python3面试题:如何用python实现栈(Stack)的操作? 上海
    python笔记71 traceback.print_exc()保存异常内容 上海
    测试驱动开发 Rss Reader Item Marker
    Rhino Mocks (RhinoMock)2
    继承的Singleton模式的实现
  • 原文地址:https://www.cnblogs.com/sunhaoxu/p/13571206.html
Copyright © 2011-2022 走看看