2019-02-01笔记—Nginx功能配置(域名重定向、认证、日志、防盗链)

域名重定向

[root@linux2019 ~]# vi /etc/nginx/conf.d/blog.aibenwoniu.xyz.conf  #添加第二个域名,并添加重定向配置
if ( $host = blog.aibenwoniu.xyz )
    {
        rewrite /(.*)  http://blog.woniu.xyz/$1 permanent;
    }
[root@linux2019 ~]# nginx -t
[root@linux2019 ~]# nginx -s reload

验证

[root@linux2019 ~]# curl -x127.0.0.1:80 -I blog.aibenwoniu.xyz/abc/123
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 25 Jan 2019 09:11:29 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://blog.woniu.xyz/abc/123

PS1:curl命令

  • -x:在给定的端口上使用HTTP代理
  • -I:只显示请求头信息

重定向参数说明:

  • $host:指的是域名
  • (.*):域名后面的url
  • $1:指的是前面的url-(.*)
  • permanent:永久生效

HTTP基本状态码

  • 200(OK)
  • 404(不存在)
  • 304(缓存)
  • 301(永久重定向)
  • 302 (临时重定向)

用户认证

目的:针对一些重要的目录、后台url实现二次认证,提高安全性。

添加location配置:

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.aibenwoniu.xyz$fastcgi_script_name;
        include        fastcgi_params;
    }

配置报错情况示例:

用户认证后报错:HTTP404

解决:

#将:#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#修改为:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
#解释:$document_root 就是nginx配置项“root”指定的位置参数的变量

建议将root定义成全局配置,这样就不用每次新增location配置时都要定义root配置

  • root /data/wwwroot/bbs.aibenwoniu.xyz;
  • index index.html index.htm index.php;

PS1:location优先级

  • =:完全匹配,优先级最高
  • ^~:前缀匹配
  • ~*:正则匹配,不区分大小写
  • ~:正则匹配,区分大小写
  • /:通用匹配,优先级最低

示例说明:

location = "/12.jpg" { ... }
如:
www.aminglinux.com/12.jpg 匹配
www.aminglinux.com/abc/12.jpg 不匹配

location ^~ "/abc/" { ... }
如:
www.aminglinux.com/abc/123.html 匹配
www.aminglinux.com/a/abc/123.jpg 不匹配

location ~ "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.png 匹配
www.aminglinux.com/aaa/png/123.html 匹配

location ~* "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配
www.aminglinux.com/aaa/png/123.html 匹配


location /admin/ { ... }
如:
www.aminglinux.com/admin/aaa/1.php 匹配
www.aminglinux.com/123/admin/1.php 不匹配

Nginx访问日志

  1. 主配置文件:
    /etc/nginx/nginx.conf
#定义日志格式(log_format)
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

参数说明:

变量说明
$args请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2
$content_lengthHTTP请求信息里的"Content-Length"
$conten_typeHTTP请求信息里的"Content-Type"
$document_rootnginx虚拟主机配置文件中的root参数对应的值
$document_uri当前请求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含后面的参数
$host主机头,也就是域名
$http_refererurl跳转来源
$http_user_agent客户端的详细信息,也就是浏览器的标识,用curl -A可以指定
$http_cookie客户端的cookie信息
$limit_rate如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$request请求的URI和HTTP协议
$remote_addr客户端的公网ip
$remote_port客户端的port
$remote_user如果nginx有配置认证,该变量代表客户端认证的用户名
$request_body_file做反向代理时发给后端服务器的本地资源的名称
$request_method请求资源的方式,GET/PUT/DELETE等
$request_filename当前请求的资源文件的路径名称,相当于是 d o c u m e n t r o o t / document_root/ documentroot/document_uri的组合
$request_uri请求的链接,包括 d o c u m e n t u r i 和 document_uri和 documenturiargs
$scheme请求的协议,如ftp,http,https
$server_protocol客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr服务器IP地址
$server_name服务器的主机名
$server_port服务器的端口号
$uri和$document_uri相同
$http_referer客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定
  1. 虚拟主机配置文件:
    /etc/nginx/conf.d/bbs.aibenwoniu.xyz.conf
#access_log  /var/log/nginx/host.access.log  main;
开启配置:access_log  /data/logs/bbs.access.log  main;

Nginx日志不记录静态文件

目的:在访问日志里,过滤掉一些图片、js、css类的请求日志。这类的请求日志没有多大用处,过滤掉这些日志可以节省一部分磁盘空间

虚拟主机配置文件添加配置:

 location ~* \.(png|jpeg|gif|js|css|bmp|flv)$
    {
    access_log off;
    }

日志切割

工具:logrotate

主配置文件:/etc/logrotate.conf

一般通过yum工具安装的软件日志切割的子配置文件在:/etc/logrotate.d/*

常用配置参数:

  • daily、weekly、monthly:指定转储周期
  • rotate count:保留备份
  • dateext:使用当期日期作为命名格式切割
  • dateformat.%s:配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,只支持 %Y %m %d %s 这四个参数
  • size:日志文件到达指定的大小时才转储
  • compress:通过gzip 压缩转储以后的日志
  • create:转储后新建文件
  • missingok:日志转储期间忽略错误

logrotate -vf /etc/logrotate.d/nginx

  • -v:可视化
  • -f:强制执行
  • -d:debug模式

静态文件的过期时间(expires)

expires参数起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求

虚拟主机配置文件添加配置,例:

location ~* \.(png|jpeg|gif|js|css|bmp|flv)$
    {
    expires 1d; #1天
    access_log off;
    }

PS:计算器工具(bc)
文件传输:(windows—linux)

Nginx防盗链

目的:防止网页上的文件、图片、视频等被其他网站引用

虚拟主机配置文件添加配置:

location ~ \.(png|jpg|gif|jpeg|bmp|mp3|mp4|flv)$
    {
        root   /data/wwwroot/blog.aibenwoniu.xyz;
        valid_referers none blocked server_names ;
        if ($invalid_referer) {
	    rewrite ^/ http://blog.woniu.xyz/return.jpg; 
        # return 403;
        }
	expires 1d;
	access_log off;
    }

参数说明:

  • png|jpg|gif|jpeg|bmp|mp3|mp4|flv:指定盗链的文件类型,可自己指定
  • valid_referers:允许文件链出的域名白名单
  • if判断:如果链路不在白名单中,即跳转到指定界面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值