4.36-域名重定向
配置第二個域名:
vi /etc/nginx/conf.d/blog.aminglinux.cc.conf
在 server_name 那一行的域名後面再加一個域名,空格做爲分隔。
nginx -t
nginx -s reload
域名重定向:
從a域名跳轉到b域名
vi /etc/nginx/conf.d/blog.aminglinux.cc.conf //增長:
if ( $host = blog.aminglinux.cc )
{
rewrite /(.*) http://www.aming.com/$1 permanent;
}
nginx -t
nginx -s reload
測試:
curl -x127.0.0.1:80 -I blog.aminglinuc.cc/1.txt
補充:
狀態碼:200(OK) 404(不存在) 304(緩存) 301(永久重定向) 302 (臨時重定向)
若是是域名跳轉,用301; 若是不涉及域名跳轉用302
rewrite /1.txt /2.txt redirect;php
4.37-用戶認證
用戶認證的目的:
實現二次認證,針對一些重要的目錄(後臺地址)
配置用戶認證:
vi 配置文件 //添加:
location ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /etc/nginx/user_passwd;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/bbs.aminglinux.cc$fastcgi_script_name;
include fastcgi_params;
}
補充:
nginx location優先級:
location / 優先級比 location ~ 要低,也就是說,若是一個請求(如,aming.php)同時知足兩個location
location /amin.php
location ~ *.php$
會選擇下面的
nginx location 文檔: https://github.com/aminglinux/nginx/tree/master/locationcss
4.38-Nginx訪問日誌
Nginx訪問日誌:
就是用戶訪問網站的記錄。
配置訪問日誌:
主配置文件:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';linux
虛擬主機配置文件:
access_log /log/to/path main;
nginx內置變量: https://github.com/aminglinux/nginx/blob/master/rewrite/variable.mdnginx
4.39--40 日誌不記錄靜態文件
日誌裏面不記錄靜態文件:
在訪問日誌裏,過濾掉一些圖片、js、css類的請求日誌。由於這樣的請求日誌沒有多大用,並且會佔用很大的磁盤空間
如何配置?
在虛擬主機配置文件裏增長配置:
location ~* \.(png|jpeg|gif|js|css|bmp|flv)$
{
access_log off;
}
補充:
tail -f /data/logs/bbs.access.log //-f選型能夠動態查看一個文件的內容
> 能夠清空一個文件內容
~* 表示不區分大小寫的匹配 後面跟正則表達式 .表示任意一個字符git
4.40-日誌切割github
爲何要作日誌切割?正則表達式
/data/logs/ 裏面有不少訪問日誌。 若是日誌愈來愈大,可能有一天會把整個磁盤寫滿。你能夠想象一下一個日誌有100G 你如何查看這個日誌? cat less tail vi
系統裏有一個日誌切割的服務緩存
logrotate 工具 配置文件: /etc/logrotate.conf 子配置文件:/etc/logrotate.d/*
Nginx的日誌切割配置文件:less
/etc/logrotate.d/nginx
內容: /var/log/nginx/.log /data/logs/.log { daily dateext missingok rotate 7 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 cat /var/run/nginx.pid
fi endscript }curl
測試執行:
logrotate -vf /etc/logrotate.d/nginx