• nginx基本概念


    1. root和alias区别
    location /img/ {
        root /var/www/image;
    }
    
    location /img/ {
        alias /var/www/image/;
    }
    使用alias, /img -> /var/www/image/
    使用root, /img -> /var/www/image/img/

    alias替换掉了路由本身,root保留了路由本身
    注意,alias必须以/结尾,否则会找不到文件
     
    2. try_files

    在nginx中 try_files 的的作用一般用户url的美化, 或者是伪静态功能, 能替代一些rewrite的指令,提高解析效率
    location /images/ {
        root /opt/html/;
        try_files $uri   $uri/  /images/default.gif; 
    }

    比如 请求 127.0.0.1/images/test.gif 会依次查找

    1.文件/opt/html/images/test.gif
    2.文件夹 /opt/html/images/test.gif/下的index文件
    3. 请求127.0.0.1/images/default.gif

    4.其他注意事项
    1.try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页 即 访问127.0.0.1/images/ 不会去访问 127.0.0.1/images/index.html
     
    3. rewrite

    nginx的rewrite功能应该广泛,可以对url进行各种复杂的重写,解决

    1. 伪静态
    2. 路径格式的调整,添加、去除路径等
    2. 目录、参数等调整,例如path参数和query_string的转换
    3. 路径的各种跳转
     
    4. location语法

    location有两种匹配规则:

    1. 命名location,用@标识,类似于定于goto语句块
    location @name { … }
    例如:
    location /index/ {
      error_page 404 @index_error;
    }
    location @index_error {
      .....
    }
    
    location / {
        try_files $uri $uri/ @custom
    }
    
    location @custom {
        # ...do something
    }
    2. 匹配URL类型,有四种参数可选,当然也可以不带参数
    location [ = | ~ | ~* | ^~ ] uri { … }
    
    location   =        /uri        =开头表示精确前缀匹配,只有完全匹配才能生效
    location   ^~       /uri        ^~开头表示普通字符串匹配上以后不再进行正则匹配,一般用来匹配目录
    location   ~        pattern     ~开头表示区分大小写的正则匹配
    location   ~*       pattern     ~*开头表示不区分大小写的正则匹配
    location   /uri                 不带任何修饰符,表示前缀匹配,大小写敏感
    location   /                    通用匹配,任何未匹配到其他location的请求都会匹配到
    以上规则简单总结就是优先级从高到低依次为(序号越小优先级越高)
    1. location =    # 精准匹配
    2. location ^~   # 带参前缀匹配
    3. location ~    # 正则匹配(区分大小写)
    4. location ~*   # 正则匹配(不区分大小写)
    5. location /a   # 普通前缀匹配,优先级低于带参数前缀匹配。
    6. location /    # 任何没有匹配成功的,都会匹配这里处理
    location 实际使用,必备3项
    1. 网站首页(转发或者直接设置静态页面)
    location = / {
        proxy_pass http://tomcat:8080/index
    }
    2. 静态文件
    location ^~ /static/ {
        root /webroot/static/;
    }
    
    location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
        root /webroot/res/;
    }
    3. 动态请求(转发给后端应用处理)
    location / {
        proxy_pass http://tomcat:8080/
    }
     
    5. nginx直接返回文本或json
    location ~ ^/text {
        default_type text/plain;
        return 200 '测试文本';  
    }
    
    location ~ ^/json {
        default_type application/json;
        return 200 '{"status":"success","result":"nginx json"}';
    }
    
    location ~ ^/text {
        default_type text/html;
        add_header Content-Type 'text/html; charset=utf-8'
        return 200 '测试文本';  
    }

    6. proxy_pass

    proxy_pass的作用是代理到其他服务器,与rewrite的区别是,rewrite代理到当前域名下,通常是同一个服务器。proxy_pass代理到其他的域名和服务器。

    读写分离
    #写服务器集群
    upstream write {
        server172.168.1.77 weight=1 fail_timeout=10s max_fails=2;
    }
    #读服务器集群
    upstream read {
        server172.168.1.111 weight=1 fail_timeout=10s max_fails=2;
    }
    
    location / {
        root   html;
        index  index.html index.htm;
        if($request_method = PUT) { (当请求方法为PUT的时候,那么分发到写服务器)
            proxy_pass http://write;
        }
        proxy_pass http://read/; (请他请求都转发到读服务器)
    }
    nginx proxy_pass路径后带/与不带/的区别

    7. upstream

    upstream代表上游的服务器集群,表示nginx作为代理客户端,将请求转发到上游服务器,并且能返回上游服务器的返回的内容。

     
    参考: https://www.cnblogs.com/jedi1995/p/10900224.html
          https://blog.csdn.net/qq_41993206/article/details/117432516
  • 相关阅读:
    spring 的简单了解
    leetcode 刷题锻炼算法思维
    REDIS学习笔记
    mark:如何使用FileZilla连接虚拟机上的Fedora
    尝试在virtualbox fedora21 下安装additions和mount share folder
    字符集与Mysql字符集处理(二)
    字符集与Mysql字符集处理(一)
    MYSQL开发性能研究——INSERT,REPLACE,INSERT-UPDATE性能比较
    MYSQL开发性能研究——批量插入的优化措施
    Marven笔记贴
  • 原文地址:https://www.cnblogs.com/mengff/p/16382716.html
Copyright © 2020-2023  润新知