201901建站运维笔记 15

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
相关文章
相关标签/搜索