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 否则覆盖

nginx日志保留时间 windows nginx默认日志_html

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 test.com

nginx日志保留时间 windows nginx默认日志_vim_02

 

提示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

 

nginx日志保留时间 windows nginx默认日志_vim_03

 

 

只针对目录用户认证

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了

nginx日志保留时间 windows nginx默认日志_nginx_04

 

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

nginx日志保留时间 windows nginx默认日志_nginx日志保留时间 windows_05

 

修改 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直接访问就对了

nginx日志保留时间 windows nginx默认日志_nginx日志保留时间 windows_06

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上了,成功

nginx日志保留时间 windows nginx默认日志_html_07

 

 


 

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匹配;

nginx日志保留时间 windows nginx默认日志_html_08

 

检错与重新加载

/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

nginx日志保留时间 windows nginx默认日志_nginx_09

 

查看日志

cat /tmp/test.com.log

nginx日志保留时间 windows nginx默认日志_html_10

IP                                                             时间                                                                      域名                      URl                                              状态码          referer

 

 


 

12.11nginx日志切割

创建shell脚本

vim /usr/local/sbin/nginx_log_rotate.sh #默认都把文件写在/usr/local/sbin 下
代码
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/data/logs"                    #根据实际情况做实验的时候改成/tmp/
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

执行脚本(可不执行)

sh -x /usr/local/sbin/nginx_log_rotate.sh
 
[root@shu-test ~]# 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
[root@shu-test ~]#

任务计划执行脚本

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      删除30天前的log文件

 

 

12.12静态文件不记录日志和过期时间

 

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

 

增加代码

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #匹配各种gif.jgep结尾的\加个托意
{
expires 7d; #过期7天
access_log off; #日志记录关闭
}
location ~ .*\.(js|css)$
{
expires 12h; #过期12小时
access_log off; #日志记录关闭
}
 
所有代码预览
server
{
    listen 80;
    server_name test.comtest2.comtest3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
         rewrite ^/(.*)$http://test.com/$1 permanent;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
    location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
 
    access_log /tmp/test.com.log combined_realip;
}

 

检错与重新加载

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
 
检查
cd /data/wwwroot/test.com/
vim 1.gif vim 2.js
 
curl -x127.0.0.1:80 test.com/1.gif
curl -x127.0.0.1:80 test.com/2.js
curl -x127.0.0.1:80 test.com/index.html
curl -x127.0.0.1:80 -I test.com/2.js

nginx日志保留时间 windows nginx默认日志_vim_11

缓存控制最大时间

 

 


 

12.13nginx防盗链

作用:防止其他网站引用本web站图片与视频资源,导致本站流量过大,从而造成不必要的经济开支;

比如:本网站test.com有图片文件1.gif,而B网站使用test.com/1.gif 引用我们的图片,那么本网站的图片访问就会上升,但是带宽会增加,访问test.com的用户量却没有增加,出口带宽成本缺增加了;

编辑虚拟配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf
 
增加代码
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ; //首先定义白名单为*.test.com,如果不是*.test.com就不允许
if ($invalid_referer) { //如果不匹配不是白名单就返回403
return 403;
}
access_log off;
}

 

代码预览

 

server
{
    listen 80;
    server_name test.comtest2.comtest3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#    {
#         expires 7d;
#         access_log off;
#    }
 
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
    expires 7d;
    valid_referers none blocked server_names  *.test.com ;       
    if ($invalid_referer) {
        return 403;
    }
    access_log off;
}        
    location ~ .*\.(js|css)$
    {
         expires 12h;
         access_log off;
    }     
    access_log /tmp/test.com.log combined_realip;
}

 

注意:如果有配置静态文件失效时间与不记录日志,一定要注释或先删除,这里是重复的;

检测与生效

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

 

 

测试

[root@root02 src]# curl -x127.0.0.1:80 -I test.com/1.gif     #正常
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sat, 22 Sep 2018 12:49:17 GMT
Content-Type: image/gif
Content-Length: 6
Last-Modified: Sat, 22 Sep 2018 06:31:08 GMT
Connection: keep-alive
ETag: "5ba5e1ac-6"
Expires: Sat, 29 Sep 2018 12:49:17 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes

curl -e "http://www.baidu.com"  -x127.0.0.1:80 test.com/1.gif -I        #测试用其他域名过来就是403

curl -e "http://www.test.com"  -x127.0.0.1:80 test.com/1.gif -I            #  白名单域名就是200

说明防盗链配置成功

 

nginx日志保留时间 windows nginx默认日志_html_12

 

 


nginx访问控制

需求:访问/admin/目录的请求,只允许某几个ip访问;

 

编辑虚拟配置文件

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