• nginx中级应用-续


    1、server下配置的每个location,都需要有自己的一套代理配置

        即要么加入:

        root  某个目录

        要么加入

        proxy_pass 某个地址; 
        proxy_redirect off;
        # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        否则如果该location被请求匹配,nginx会先读取该location下的root或者proxy_pass,如果没有,则取server下全局的root配置

        如果还没有,则使用默认配置(nginx安装目录下的html目录)

        如果多个location中使用了同一个配置,可以把配置抽为一个conf文件,之后再include *.conf把配置引进来

        官方root说明:http://nginx.org/en/docs/http/ngx_http_core_module.html#root

        Syntax:root path;
        Default:

        root html;

        Context:httpserverlocationif in location

        可用在http,server,location和if中

        官方proxy_pass说明:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

    Syntax: proxy_pass URL;
    Default:
    Context: locationif in locationlimit_except

        只能用在location和if中

        

        于是就导致了:要想使用expired令浏览器使用缓存机制,则必须要在静态文件的匹配中

        要么把root配置到对应的网站文件夹下,要么配置到proxy_pass

        

        配置好之后,成功让浏览器接受到304响应,嘿嘿,下面是配置文件:由此可见,若想在响应头中添加什么值,最好把代理的服务器抽出一个单独的主机配置文件,在location中

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
        
        location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt)$   
            {   
                #缓存时长
                expires      7d; 
            }
    
        #JS和CSS缓存时间设置
        location ~ .*.(js|css)?$
        {
            expires 1h;
        }
        
            error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ .php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
        server {
        listen 80;
        server_name angular.ailiailia.com;
        location / {
            proxy_pass http://localhost:8000;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_read_timeout 600;
            proxy_send_timeout 600;
        }
                    
    #    location /status {
    #        stub_status on;
    #        access_log off;
    #    }
                    
        location /.(gif|jpg|jpeg|png|bmp|swf)$ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8000;
            expires 30d;
        }
                    
        location /.(js|css)?$ {
            expires 12h;
        }
        }
    
        server {
        listen 80;
        server_name node.ailiailia.com;
    
        location / {
            proxy_pass http://localhost:3000/;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_read_timeout 600;
            proxy_send_timeout 600;
        }
                    
        location /.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 30d;
        }
                    
        location /.(js|css)?$ {
            expires 12h;
        }
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }

        

    2、nginx的匹配优先级:

        不同于防火墙,前面的规则优先

        nginx类似于css,权重高的优先

        但是也不全是:   

      一、 location 的匹配符
      1.等于匹配符:=
      等于匹配符就是等号,特点可以概括为两点:
      精确匹配
      不支持正则表达式
      2.空匹配符
      空匹配符的特点是:
      匹配以指定模式开始的 URI
      不支持正则表达式
      3.正则匹配符:~
      正则匹配符是可以使用正则表达式的匹配符。不过这里要强调的是,一般来说~是指:
      区分大小写的正则匹配
      而~*表示:
      不区分大小写的正则匹配
      但是对于一些对大小写不敏感的操作系统,这两者没有区别。另外一个就是^~,其表示以指定模式开始的正则匹配。

      4.内部访问符:@
      一般用于错误页面等,这个暂不讨论。

      二、匹配符优先级
      1.=
      2.空匹配符,满足精确匹配时
      3.^~
      4.~或~*
      5.空匹配符,满足以指定模式开始时的匹配时

      (location =) > (location 完整路径 >) >(location ^~ 路径) >(location ~* 正则) >(location 路径)

         只要匹配到,其它的都会忽略,然后返回到改匹配。


      同级匹配符按从上到下的优先级排列

      

    location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … }

    “=”是精确匹配
    “@”是命名的location ,在正常的location 匹配中不会使用,仅仅在内部跳转中才会使用到。
    “~”是区分大小写的匹配
    “~*”是不区分大小写的匹配
    “^~”表示中止正则匹配(这个平时没太注意)

    在一个请求中,匹配的顺序是这样的。先使用所有location 来匹配URI的开始部分,最精确匹配的(形象点说,就是即配置字符数最多的)为最后匹配结果;然后进行正则表达式的匹配,按照配置文件中的顺序来进行匹配,如果有一个匹配成功,则结束正则匹配,且最后匹配结果为此location ,否则,最后结果为先前最精确匹配的的那个location 。

    之前有提到过”^~”,它配置在非正则匹配中,表示,如果最精确匹配的loction为此location ,则立即返回该location 作为结果,而不进行下一步的正则匹配,这样,就此可以不必要进入到正则匹配当中,以加快匹配速度。

    还有”=”,它是最精确的匹配,而且优先级最高。最先进行带”=”的匹配,如果匹配成功,立马返回。

    最后总结下匹配的过程,有四步:
    1. 带”=”前缀的先进行匹配,如果找到了,中止查找。
    2. 所有其它location 进行非正则的匹配,找到最精确匹配的那个,如果匹配到带”^~”前缀的,则中止查找。
    3. 正则查找,按照我们配置文件中配置的location 顺序进行查找。
    4. 如果正则查找匹配成功,则使用此正则匹配的location ,否则,使用第二步查找的结果。

    这里要特别说明下”=”与”^~”的区别:
    “=”在匹配时,则匹配带”=”的location 。而”^~”,则会匹配所有非”=”的非正则location ,只有在确认它是最精确匹配的location 后,才生效。

    location 匹配的优先级(与location在配置文件中的顺序无关)
    = 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
    普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
    ^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
    最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。

    location 优先级官方文档

    1. Directives with the = prefix that match the query exactly. If found, searching stops.
    2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
    3. Regular expressions, in order of definition in the configuration file.
    4. If #3 yielded a match, that result is used. Else the match from #2 is used.
    1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
    2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
    3. 正则表达式,在配置文件中定义的顺序。
    4. 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。

    例如

    location  = / {
      # 只匹配"/".
      [ configuration A ] 
    }
    location  / {
      # 匹配任何请求,因为所有请求都是以"/"开始
      # 但是更长字符匹配或者正则表达式匹配会优先匹配
      [ configuration B ] 
    }
    location ^~ /images/ {
      # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
      [ configuration C ] 
    }
    location ~* .(gif|jpg|jpeg)$ {
      # 匹配以 gif, jpg, or jpeg结尾的请求. 
      # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
      [ configuration D ] 
    }

    请求URI例子:

    • / -> 符合configuration A
    • /documents/document.html -> 符合configuration B
    • /images/1.gif -> 符合configuration C
    • /documents/1.jpg ->符合 configuration D

    @location 例子
    error_page 404 = @fetch;

    location @fetch(
    proxy_pass http://fetch;
    )

    3、if判别的使用

      nginx重定向的IF条件判断
    在server和location两种情况下可以使用nginx的IF条件判断,条件可以为以下几种:
    正则表达式
    如:
    匹配判断
    ~  为区分大小写匹配; !~为区分大小写不匹配
     ~* 为不区分大小写匹配;!~为不区分大小写不匹配
    例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
    if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /nginx-ie/$1 break;
    }
    文件和目录判断
      -f和!-f判断是否存在文件
     -d和!-d判断是否存在目录
     -e和!-e判断是否存在文件或目录
     -x和!-x判断文件是否可执行
    例如下面设定nginx在文件和目录不存在的时候重定向:
    if (!-e $request_filename) {
    proxy_pass http://127.0.0.1/;
    }
    return
    返回http代码,例如设置nginx防盗链:
    location ~* .(gif|jpg|png|swf|flv)$ {
    valid_referers none blocked http://www.jefflei.com/ http://www.leizhenfang.com/;
    if ($invalid_referer) {
    return 404;
    }
    }
    set
    设置nginx变量

     
     
     
     

    flag标记有:
    * last 相当于Apache里的[L]标记,表示完成rewrite 
    * break 终止匹配, 不再匹配后面的规则 
    * redirect 返回302临时重定向 地址栏会显示跳转后的地址 
    * permanent 返回301永久重定向 地址栏会显示跳转后的地址
    一些可用的全局变量有,可以用做条件判断(待补全)
    $args 
    $content_length 
    $content_type 
    $document_root 
    $document_uri 
    $host 
    $http_user_agent 
    $http_cookie 
    $limit_rate 
    $request_body_file 
    $request_method 
    $remote_addr 
    $remote_port 
    $remote_user 
    $request_filename 
    $request_uri 
    $query_string 
    $scheme 
    $server_protocol 
    $server_addr 
    $server_name 
    $server_port 
    $uri

     
     

    http://m.blog.chinaunix.net/uid-24250828-id-3265253.html

    4、执行以下命令,把ngx_http_substitutions_filter_module模块编译进去,主要为了反向绑定域名过滤到页面的URL地址。

     

    git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/root/nginx-0.7.64/ngx_http_substitutions_filter_module
    make && make install

     

  • 相关阅读:
    LINUX 环境变量总结
    make的自动变量和预定义变量
    函数调用约定和堆栈
    如何查看linux命令源代码
    shell脚本中特定符合变量的含义
    【转载】Redhat5和6 YUM源配置的区别
    用路径分析法来编写测试用例
    linux ip 设置
    Mysql 的存储引擎,myisam和innodb的区别。
    一些编译php时的configure 参数
  • 原文地址:https://www.cnblogs.com/guangshan/p/4853775.html
Copyright © 2020-2023  润新知