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. Python并发编程之多进程(实战)

    一.multiprocessing和Process multiprocessing提供了支持子进程.通信和数据共享.执行不同形式的同步,提供了Process.Queue.Pipe.Lock等组件 创建 ...

  2. Ubuntu 15.04 安装配置 Qt + SQLite3

    序 最近需要在Ubuntu下使用Qt开发项目,选择简单小巧的SQLite数据库,现将安装配置以及简单操作记录如下,以便日后查阅. 安装Qt CMake和Qt Creator是Linux下开发C++程序 ...

  3. Centos7 install Openstack Juno (RDO) (转载)

    原文地址:http://www.hdume.com/centos-7-0%E5%AE%89%E8%A3%85openstack/ 1.安装系统,Centos7镜像采用CentOS-7.0-1406-x ...

  4. HDU 3435 KM A new Graph Game

    和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...

  5. vi 编辑器命令

    插入命令 a append after the cursor A append after the current line i insert before the cursor I insert b ...

  6. JAVA 消耗 CPU过高排查方法

    #找出cpu占用最高的进程top -H#再次确定进程ps aux|grep 17408 #查看进程的线程(tid) ps -mp 17408 -o THREAD,tid,time#将线程转换为十六进制 ...

  7. webdriver高级应用- 禁止IE的保护模式

    #encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.desired_capabilities i ...

  8. webapi同时支持post和get报404错误

    文章:webapi设置一个Action同时支持get和post请求 这篇文章,有提供方法.参数前加上[FromUri] [AcceptVerbs("GET", "POST ...

  9. iOS开发 UILabel实现自适应高宽

    UILabel是iOS开发常用的控件.UILabel的属性需要了解,UILabel的特殊显示效果也需要我们掌握.UILabel自适应高宽度是很多初学者遇到的技术性难题.比如段文字,要让他完全地分行显示 ...

  10. [暑假集训--数位dp]hdu3709 Balanced Number

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...