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}\n" http://www.etiantian.com
402
# / 通用匹配--任何请求都会匹配到
curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.com/index.html
401
# 下面的例子说明了--优先匹配正则这一规则
curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.com/document/1.jpg
500
curl -s -o /dev/null -I -w "%{http_code}\n" 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:返回临时重定向;

permanent:返回永久重定向.

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}\n" http://bbs.etiantian.com
200
curl -s -o /dev/null -I -w "%{http_code}\n" etiantian.com
301
curl -s -o /dev/null -I -w "%{http_code}\n" http://baidu.com
200
curl -s -o /dev/null -I -w "%{http_code}\n" 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

Nginx日志参数、location匹配规则、设置密码的更多相关文章

  1. nginx中的location匹配规则

    概述: 1. location在nginx配置文件中的作用是根据用户请求的URI来执行不同的应用. 2.URI的定义:标识.定位任何资源的字符串 协议://域名/目录a/目录b/文件c http:// ...

  2. nginx配置中location匹配规则详解

    一.概述 nginx官方文档给出location语法如下: 1 location [=|~|~*|^~] uri { … } 其中,方括号中的四种标识符是可选项,用来改变请求字符串和uri的匹配方式. ...

  3. Nginx之Location匹配规则

    概述 经过多年发展,nginx凭借其优异的性能征服了互联网界,成为了各个互联网公司架构设计中不可获取的要素.Nginx是一门大学问,但是对于Web开发者来说,最重要的是需要能捋的清楚Nginx的请求路 ...

  4. Nginx之location 匹配规则详解

    有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...

  5. 前端开发掌握nginx常用功能之server&location匹配规则

    nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的 ...

  6. nginx多虚拟主机优先级location匹配规则及tryfiles的使用

    nginx多虚拟主机优先级location匹配规则及tryfiles的使用 .相同server_name多个虚拟主机优先级访问 .location匹配优先级 .try_files使用 .nginx的a ...

  7. location 匹配规则 (NGINX)

    转:https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html location 匹配规则 ...

  8. nginx教程1:location 匹配规则

    worker_process # 表示工作进程的数量,一般设置为cpu的核数 worker_connections # 表示每个工作进程的最大连接数 server{} # 块定义了虚拟主机 liste ...

  9. Nginx的alias与root的用法区别和location匹配规则

    1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...

随机推荐

  1. jQuery将物体居中,并且转换显示和隐藏

    今天来给大家贴一段代码,代码的作用就是利用jQuery将物体居中,并且转换显示和隐藏: 首先建立一个div标签并且写好css样式,具体如下 然后我想要的效果是当我点击了button这个按钮,test可 ...

  2. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

  3. Android兼容性测试CTS --环境搭建、测试执行、结果分析

    为了确保Android应用能够在所有兼容Android的设备上正确运行,并且保持相似的用户体验,在每个版本发布之时,Android提供了一套兼容性测试用例集合(Compatibility Test S ...

  4. IE6 单文件绿色版

    IE6单文件绿色版,可以直接运行,无需安装,完美兼容Win10(自带2016年1月更新). https://www.lanzous.com/i3w7dej

  5. js各种继承方式和优缺点的介绍

    js各种继承方式和优缺点的介绍 作者: default 参考网址2 写在前面 本文讲解JavaScript各种继承方式和优缺点. 注意: 跟<JavaScript深入之创建对象>一样,更像 ...

  6. 实现类似QQ单一账户登录,在另一个地方登录后在原登录窗口提示下线

    首先,使用框架做的最好,可以在框架页直接做一次就好了 再登陆成功后保存session的代码后添加以下代码: 注意:需要引入命名空间using System.Collections; SetApplic ...

  7. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  8. # Linux 命令学习记录

    Linux 命令学习记录 取指定文件夹下的任意一个文件,并用vim打开 vi $(ls -l|grep "^-"|head -n 1|awk '{print $9}') 统计给定文 ...

  9. niubi-job:一个分布式的任务调度框架设计原理以及实现

    niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之间独立 ...

  10. [python学习篇][廖雪峰][2]函数式编程

    函数名也是变量: >>> f = abs >>> f(-10) 10 然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就 ...