12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

12.7nginx默认虚拟主机

vim /usr/local/nginx/conf/nginx.conf

    gzip_types text/plain application/x-javascript text/css text/htm

    application/xml;

    include vhost/*.conf;            #添加虚拟主机目录  加这一行 include包含

    }

 

cd /usr/local/nginx/conf/

mkdir vhost

cd vhost/                 

vim aaa.com.conf #*.conf; 后缀名是conf的, 文件必须也是conf结尾 否则就没认到这个文件

 

server

{

    listen 80 default_server;  // 有这个标记的就是默认虚拟主机

    server_name aaa.com;

    index index.html index.htm index.php;

    root /data/wwwroot/default;

}

 

mkdir -p /data/wwwroot/default;cd /data/wwwroot/default/

vim index.html   this is default test

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

 

 

 

12.8ginx用户认证

编辑web配置文件  以下是对整站

vim /usr/local/nginx/conf/vhost/test.com.conf

 

代码

 

server

{

    listen 80;

    server_name test.com;

    index index.html index.htm index.php;

    root /data/wwwroot/test.com;

   

location  /

    {

        auth_basic              "Auth";               #定义用户认证名字

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;            #用户名密码文件

}

}

 

生成密码文件

• yum install -y httpd    #htpasswd 安装了才能生成密码文件

• htpasswd -c /usr/local/nginx/conf/htpasswd aming

• htpasswd  /usr/local/nginx/conf/htpasswd chenyun        #第2个用就不用加-c 否则覆盖

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

curl -x127.0.0.1:80 test.com

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

提示401 401就是需要用户密码

curl -uaming:aming -x127.0.0.1:80 test.com     #现在就不提示401 404说明进去了,但是没这个页面

创建test.com目录

mkdir /data/wwwroot/test.com

echo "test.com" > /data/wwwroot/test.com/index.html

 

测试

curl -uaming:aming -x127.0.0.1:80 test.com

test.com

 

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

 

只针对目录用户认证

vim /usr/local/nginx/conf/vhost/test.com.conf

修改 location / 为location /admin/

也就是将代表所有的/改为代表目录的/admin/

代码如下

server

{

    listen 80;

    server_name test.com;

    index index.html index.htm index.php;

    root /data/wwwroot/test.com;

   

location  /admin/          #针对admin这个目录做认证

    {

        auth_basic              "Auth";               #定义用户认证名字

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;            #用户名密码文件

}

}

 

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

curl -x127.0.0.1:80 test.com    不提示了直接访问

curl -x127.0.0.1:80 test.com/admin/     #访问admin目录提示401了 

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

mkdir /data/wwwroot/test.com/admin

echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html

curl -uaming:aming -x127.0.0.1:80 test.com/admin/

test.com admin dir 正确

 

 

针对单个页面

vim /usr/local/nginx/conf/vhost/test.com.conf

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

修改 location / 为location ~ admin.php

也就是将代表所有的/改为代表目录的/admin/

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

curl -x127.0.0.1:80 test.com/admin/   #不用--u直接访问就对了

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

12.9nginx域名重定向

http://blog.51cto.com/shuzonglu/2087005

编辑web配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf

增加

 

if ($host != 'test.com' ) {

rewrite ^/(.*)$http://test.com/$1 permanent;

}

修改server_name 后面增加test2.com  test3.com

代码预览

server

{

    listen 80;

    server_name test.com test2.com test3.com;                            #添加域名

    index index.html index.htm index.php;

    root /data/wwwroot/test.com;

    if ($host != 'test.com' ) {                                                                   #如果域名不等于test.com我就跳转

        rewrite  ^/(.*)$  http://test.com/$1  permanent;                         #^其实就是test.com开头(.*)$通配符 是域名后面那一部分

    }

 

 

permanent  301 永久跳转 被请求的资源已永久移动到新位置

redirect       302 请求的资源临时从不同的URI响应请求,但请求者应继续使用原有位置来进行以后的请求

301重定向和302重定向的区别

  302重定向只是暂时的重定向,搜索引擎会抓取新的内容而保留旧的地址,因为服务器返回302,所以,搜索搜索引擎认为新的网址是暂时的。

  而301重定向是永久的重定向,搜索引擎在抓取新的内容的同时也将旧的网址替换为了重定向之后的网址。

检错与重新加载

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

 

测试

curl -x127.0.0.1:80 test2.com/1.html -I

访问test1.com定位到test.com上了,成功

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

 


 

12.10 nginx访问日志配置

日志格式

 

查看nginx.conf文件

• vim /usr/local/nginx/conf/nginx.conf //搜索log_format

中间有一行是定义log的格式

 

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' #名字可以自己定义

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

其中''包围起来的不算一行,只是为了美观才分行显示,分行显示就必须加上''号;

含义:

combined_realip:定义日志格式别名,默认为combined_realip,这里我会改为test001;

$remote_addr:远程客户端ip也就是出口公网ip;

$http_x_forwarded_for:代理服务器ip;

$time_local:服务器本地时间;

$host:访问主机名(域名);

$request_uri:访问的url地址;

$status:状态码,比如404;

$http_referer:referer; 告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理。比如从我主页上链接到一个朋友那里,他的服务器就能够从HTTP Referer中统计出每天有多少用户点击我主页上的链接访问他的网站。

$http_user_agent:也就是访问的浏览器类型,比如傲游7; 

 

编辑虚拟web配置

vim /usr/local/nginx/conf/vhost/test.com.conf

增加access_log /tmp/test.com.log combined_realip; #要和log_format combined_realip 名字一样 vim /usr/local/nginx/conf/nginx.conf //搜索combined_

其中combined_realip是定义名称,记得与nginx.conf匹配;

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

检错与重新加载

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

 

访问与测试

curl -x127.0.0.1:80 test.com/admin/index.html -I

访问成功200

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

 

查看日志

cat /tmp/test.com.log

12.7nginx默认虚拟主机12.8ginx用户认证12.9nginx域名重定向12.10 nginx访问日志配置12.11 Nginx日志切割

IP                                                             时间                                                                      域名                      URl                                              状态码          referer

 

 


 

12.11nginx日志切割

创建shell脚本

vim /usr/local/sbin/nginx_log_rotate.sh

代码

#! /bin/bash## 假设nginx的日志存放路径为/tmp/

d=`date -d "-1 day" +%Y%m%d`

logdir="/tmp/"

nginx_pid="/usr/local/nginx/logs/nginx.pid"cd $logdirfor log in `ls *.log`

do

mv $log $log-$ddone

/bin/kill -HUP `cat $nginx_pid`

执行脚本(可不执行)

sh -x /usr/local/sbin/nginx_log_rotate.sh

 

[[email protected] ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh

++ date -d '-1 day' +%Y%m%d

+ d=20180314

+ logdir=/tmp/

+ nginx_pid=/usr/local/nginx/logs/nginx.pid

+ cd /tmp/

++ ls php_errors.log test.com.log

+ for log in '`ls *.log`'

+ mv php_errors.log php_errors.log-20180314

+ for log in '`ls *.log`'

+ mv test.com.logtest.com.log-20180314

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 1574

[[email protected] ~]#

任务计划执行脚本

crontab -e

写入脚本

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

意思:每天凌晨0点执行脚本/usr/local/sbin/nginx_log_rotate.sh

如何脚本删除log

find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm