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:返回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}\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
