• Nginx日志参数、location匹配规则、设置密码


    1.三个参数

    a)$http_referer:记录此次请求是从哪个链接访问过来的:

    直接访问,还是从其他网站跳转过来的.

    例如:访问:http://www.etiantian.com/,其页面首页是index.html

    <h1>www-10.0.0.8:80</h1>
    <a href="www.qingfeng.com" target="_blank"><img src="123.jpg""></a>

    点击a标签,在www.qingfeng.com(10.0.0.7)上观察日志,可得:此次请求是从www.etiantian.com而来.

    - 10.0.0.1 - - [25/Dec/2018:03:44:43 +0800] GET / HTTP/1.1200 16 http://www.etiantian.com/

    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"

    b)$http_x_forwarded_for$remote_addr

    nginx作为web服务器,想要记录客户端真实IP,需要在自身配置文件中设置此参数:

    $http_x_forwarded_for,同时也必须在前端代理服务器的配置文件中添加:

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    日志格式中添加$http_x_forwarded_for $remote_addr,如: log_format main '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_cookie" $host $request_time';

    此时web服务器的日志中$http_x_forwarded_for就是客户端真实IP,$remote_addr是代理服务器IP,

    而代理服务器上的$http_x_forwarded_for为空,$remote_addr为客户端IP,所以可得:

    $remote_addr是直接访问服务器的IP.

    2.nginx日志切割

    mkdir /server/scripts
    
    cat /server/scripts/cut_nginx_log.sh
    #!/bin/bash
    cd /application/nginx/logs/
    /bin/mv www_access.log www_access_$(date +%F).log
    # 让进程释放日志文件
    /application/nginx/sbin/nginx -s reload
    crontab -e
    59 23 * * * /bin/sh /server/scripts/cut_nginx_log.sh
    

    3.location匹配规则

    语法规则:location [=|~|~*|^~] /uri/ { … },优先级:

    第一名:"location =/{...}"  精确匹配/

    第二名:"location ^~ /images/{...}"  匹配常规字符串,不做正则匹配检查

    第三名:"location ~*.(gif|jpg|jpeg)${...}"  正则匹配

    第四名:"location /document/{...}"  匹配常规字符串,如果有正则就优先匹配正则

    第五名:"location /{...}"  所有location都不能匹配后的默认匹配

    cat www.conf
    server {
        listen       80;
        server_name  www.etiantian.com etiantian.com;
        access_log logs/www_access.log main;
        location / {
            return 401;
        }
        location = / {
            return 402;
        }
        location /document/ {
            return 403;
        }
        location ^~ /images/ {
            return 404;
        }
        location ~* .(gif|jpg|jpeg)$ {
            return 500;
        }
    }
    # = 等号--优先级最高
    curl -s -o /dev/null -I -w "%{http_code}
    " http://www.etiantian.com
    402
    # / 通用匹配--任何请求都会匹配到
    curl -s -o /dev/null -I -w "%{http_code}
    " http://www.etiantian.com/index.html
    401
    # 下面的例子说明了--优先匹配正则这一规则
    curl -s -o /dev/null -I -w "%{http_code}
    " http://www.etiantian.com/document/1.jpg
    500
    curl -s -o /dev/null -I -w "%{http_code}
    " http://www.etiantian.com/document/index.html
    403
    

    4.来自他人博客的location总结:

    =  表示精确匹配;
    ^~ 表示uri以某个常规字符串开头;
    ~  表示区分大小写的正则匹配;
    ~* 表示不区分大小写的正则匹配;
    !~和!~*分别为区分大小写不匹配及不区分大小写不匹配;
    /  通用匹配-任何请求都会匹配到.
    当有匹配成功时候,停止匹配,按当前匹配规则处理请求.
    
    有如下匹配规则:
    location = / {
       #规则A
    }
    location = /login {
       #规则B
    }
    location ^~ /static/ {
       #规则C
    }
    location ~ .(gif|jpg|png|js|css)$ {
       #规则D,括号内全是小写,只匹配小写
    }
    location ~* .png$ {
       #规则E
    }
    location !~ .xhtml$ {
       #规则F
    }
    location !~* .xhtml$ {
       #规则G
    }
    location / {
       #规则H
    }
    访问根目录/,比如http://localhost/ 将匹配规则A;
    访问http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H;
    访问 http://localhost/static/a.html 将匹配规则C;
    访问 http://localhost/a.gif,http://localhost/b.jpg 将匹配规则D和规则E,
    但是规则D顺序优先,规则E不起作用,
    而 http://localhost/static/c.png 则优先匹配到-规则C;
    访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写;
    访问 http://localhost/a.xhtml 不会匹配规则F和规则G;
    http://localhost/a.XHTML
    不会匹配规则G(因为!)规则F、规则G属于排除法,符合匹配规则也不会匹配到.
    访问 http://localhost/category/id/1111 则最终匹配到规则H,
    因为以上规则都不匹配,这个时候nginx转发请求给后端应用服务器,
    比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在.
    

    5.location实战

    实际使用中,至少有三个匹配规则定义
    # 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说.
    # 这里是直接转发给后端应用服务器了,也可以是一个静态首页.
    # 第一个必选规则
    location = / {
        proxy_pass http://tomcat:8080/index
    }
    # 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
    # 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
    location ^~ /static/ {                              //以xx开头
        root /webroot/static/;
    }
    location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {     //以xx结尾
        root /webroot/res/;
    }
    # 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
    # 非静态文件请求就默认是动态请求,自己根据实际把握
    location / {
        proxy_pass http://tomcat:8080/
    }
    

    6.rewrite

    语法:rewrite regex replacement[flag];

    rewrite 正则表达式 跳转到的地址 flag是标记

    redirect:返回302临时重定向;

    permanent:返回301永久重定向.

    server {
        listen       80;
        server_name  bbs.etiantian.com;
        location / {
            root html/bbs;
            index index.html;
        }
        location ^~ /images/ {
            rewrite ^/(.*) http://blog.oldboyedu.com/$1 permanent;
        }
    }
    访问http://bbs.etiantian.com/images/index,会跳转到下面:
    https://blog.oldboyedu.com/images/index
    

    7.案例及配置文件展示

    curl -I www.360buy.com
    HTTP/1.1 301 Moved Permanently
    Server: JDWS/2.0
    Date: Sat, 15 Dec 2018 09:12:08 GMT
    Content-Type: text/html
    Content-Length: 178
    Connection: keep-alive
    Location: http://www.jd.com/
    Via: BJ-H-NX-112(), http/1.1 BJ-GWBN-2-JCS-35 ( [cRs f ])
    Age: 1568
    
    cat index.conf
    server {
        listen       80;
        server_name  www.360buy.com;
        rewrite ^/(.*) https://www.jd.com/$1 permanent;
    }
    server {
        listen       80;
        server_name  www.jd.com;
        location / {
            root html/index;
            index index.html;
        }
    }
    
    cat bbs.conf
    server {
        listen       80;
        server_name  etiantian.com;
        rewrite ^/(.*) http://bbs.etiantian.com/$1 permanent;
    }
    server {
        listen       80;
        server_name  bbs.etiantian.com;
        location / {
            root html/bbs;
            index index.html;
        }
    }
    

    别名和跳转的状态码是不一样的,用别名的话就看不到新的域名了,用跳转可以看到新的域名

    curl -s -o /dev/null -I -w "%{http_code}
    " http://bbs.etiantian.com
    200
    curl -s -o /dev/null -I -w "%{http_code}
    " etiantian.com
    301
    curl -s -o /dev/null -I -w "%{http_code}
    " http://baidu.com
    200
    curl -s -o /dev/null -I -w "%{http_code}
    " http://taobao.com
    302
    # http://jd.com、http://qq.com访问的结果全是302,服务器返回302代码,
    # 会让搜索引擎认为新的网址是暂时的,抓取新网址时保留旧网址,SEO 302好于301;
    # 301会让搜索引擎在抓取新网址时,将旧网址替换为重定向之后的网址
    

    8.访问nginx需要账号密码

    # 查看这个命令的包名
    # -q:query;-a:all;-f:file;-l:list;-e:卸载;--nodeps:不管依赖关系
    rpm -qf /usr/bin/htpasswd
    rpm -e --nodeps  # 强制卸载
    rpm -ivh  # 安装
    rpm -Uvh  # 升级 
    rpm -ql   # 查看包里有什么文件  
    yum -y install httpd-tools
    # -c:创建新文件;-b:非交互式
    htpasswd -cb /application/nginx/conf/htpasswd lixiang root123
    
    cat hehe.conf
    server {
        listen       80;
        server_name  hehe.etiantian.com;
        access_log logs/www_access.log main;
        location / {
            auth_basic "this page need account passwd";
            auth_basic_user_file /application/nginx/conf/htpasswd;
            root  html/hehe;
            index index.html;
        }
    }
    # 配置文件中指定密码文件必须是全路径,密码输入多次错误,报401-Auth Required
    mkdir /application/nginx/html/hehe
    echo "Right Password..." > /application/nginx/html/hehe/index.html
    chmod 400 /application/nginx/conf/htpasswd
    403:网页被删了或者网页权限不对(700);
    文件存在,但配置文件里没写,没有这一行--index index.html;
    

    老男孩总结403:http://blog.51cto.com/oldboy/581383

    location和rewrite详解:https://segmentfault.com/a/1190000002797606

    nginx配置详细解释:https://blog.csdn.net/qq_33862644/article/details/79337348

  • 相关阅读:
    this_is_flag
    攻防世界-misc-如来十三掌
    攻防世界-misc-pdf
    nextcloud取消新用户的默认文件
    nextcloud开放注册-添加注册功能
    图片马制作
    Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的解决方法
    VS2017 如何安装水晶报表 VS2017 如何下载相应版本的水晶报表 VS2017 水晶报表 中文乱码
    js 带有返回值的 匿名方法
    varchar nvarchar 设计长度时 设计成 (2^n)-1 的好处
  • 原文地址:https://www.cnblogs.com/fawaikuangtu123/p/10170721.html
Copyright © 2020-2023  润新知