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. 《linux设备驱动开发详解》笔记——8阻塞与非阻塞IO

    8.1 阻塞与非阻塞IO 8.1.0 概述 阻塞:访问设备时,若不能获取资源,则进程挂起,进入睡眠状态:也就是进入等待队列 非阻塞:不能获取资源时,不睡眠,要么退出.要么一直查询:直接退出且无资源时, ...

  2. GoF23种设计模式之行为型模式之状态模式

    一.概述         定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 二.适用性 1.一个对象的行为取决于它的状态,并且它必须在运行时刻 ...

  3. Python3 S.join() 个人笔记

    S.join(iterable) S:需要的分隔符 iterable:被分割对象 . 注意括号里必须只能有一个成员,比如 ','.join('a','b') 这种写法是行不通的 实例:'-'.join ...

  4. asynctask 异步下载

    public class MainActivity extends Activity{ private TextView show; @Override public void onCreate(Bu ...

  5. 转:Generating PDFs from Web Pages on the Fly with jsPDF

    The Portable Document Format has been one the major innovations in the fields of desktop publishing ...

  6. iOS启动图 LaunchImage LaunchScreen.xib

    1.Images.xcassets添加LaunchImage 2.具体大小和添加类别都是可以调的 640*960   (4/4s)                                 2X ...

  7. 大数据学习——scala类相关操作

    1 类的定义 package com /** * Created by Administrator on 2019/6/3. */ //类并不用声明为public. class Person { // ...

  8. Nginx从入门到放弃-第5章 Nginx架构篇

    5-1 Nginx常见问题_架构篇介绍 5-2 Nginx常见问题_多个server中虚拟主机读取的优先级 5-3 Nginx常见问题_多个location匹配的优先级1 5-4 Nginx常见问题_ ...

  9. Python开启进程的2中方式

    知识点一:进程的理论 进程:正在进行的一个程序或者说一个任务,而负责执行任务的则是CPU 进程运行的的三种状态: 1.运行:(由CPU来执行,越多越好,可提高效率) 2.阻塞:(遇到了IQ,3个里面可 ...

  10. tornado中文教程

    http://docs.pythontab.com/tornado/introduction-to-tornado/ch2.html#ch2-1 python的各种库的中文教程 http://docs ...