域名重定向
配置第二个域名
去往nginx虚拟机配置文件
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
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
permanent 为永久重定向,指定状态码
https://mp.csdn.net/mdeditor# net后/xx统一称为URL,/(.*)代表URL
测试
浏览器测试
进入浏览器后按下F12键;
Linux下测试
curl -x127.0.0.1:80 -I blog.aminglinuc.cc/1.txt
-I为只显示header信息。
补充
状态码:200(OK) 404(不存在) 304(缓存) 301(永久重定向) 302 (临时重定向)
如果是域名跳转,用301; 如果不涉及域名跳转用302
vi /etc/nginx/conf.d/blog.aminglinux.cc.conf
直接增加
rewrite /1.txt /2.txt redirect;
用户认证
用户认证目的
实现二次认证,针对一些重要的目录(后台地址)
配置用户认证:
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/location
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"';
虚拟主机配置文件:
access_log /log/to/path main;
nginx内置变量: https://github.com/aminglinux/nginx/blob/master/rewrite/variable.md
日志不记录静态文件
日志里面不记录静态文件:
在访问日志里,过滤掉一些图片、js、css类的请求日志。因为这样的请求日志没有多大用,而且会占用很大的磁盘空间
如何配置?
在虚拟主机配置文件里增加配置:
location ~* \.(png|jpeg|gif|js|css|bmp|flv)$
{
access_log off;
}
补充:
tail -f /data/logs/bbs.access.log //-f选型可以动态查看一个文件的内容
可以清空一个文件内容
~* 表示不区分大小写的匹配 后面跟正则表达式 .表示任意一个字符
日志切割
为什么要做日志切割?
/data/logs/ 里面有很多访问日志。 如果日志越来越大,可能有一天会把整个磁盘写满。你可以想象一下一个日志有100G
你如何查看这个日志? cat less tail vi
上面的工具查看大文件日志时都有弊端,需要将日志切割,按天或者按时切割。
系统里有一个日志切割的服务
logrotate 工具
配置文件: /etc/logrotate.conf
子配置文件:/etc/logrotate.d/*
Nginx的日志切割配置文件:
/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 }
测试执行:
logrotate -vf /etc/logrotate.d/nginx