传统IDC部署网站8

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;
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/location
4.38-Nginx访问日志
Nginx访问日志:

就是用户访问网站的记录。
配置访问日志:

主配置文件:
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ time_local] “KaTeX parse error: Double superscript at position 30: … '̲status b o d y b y t e s s e n t " body_bytes_sent " http_referer” ’
‘“ h t t p u s e r a g e n t " " 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选型可以动态查看一个文件的内容

可以清空一个文件内容
~* 表示不区分大小写的匹配 后面跟正则表达式 .表示任意一个字符
4.40-日志切割
为什么要做日志切割?

/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

猜你喜欢

转载自blog.csdn.net/weixin_44516165/article/details/86685152