第二十一课预习任务-LNMP2

12.7 Nginx默认虚拟主机

12.8 Nginx用户认证

12.9 Nginx域名重定向

12.10 Nginx访问日志

12.11 Nginx日志切割

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

12.13 Nginx防盗链

12.14Nginx访问控制

12.15Nginx解析php相关配置

12.16 Nginx代理

 

 

 

12.7 Nginx默认虚拟主机

编辑nginx.conf的文件

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

增加行

Include vhost/*.conf;

2,conf下创建一个vhost子目录

[root@localhost ~]# mkdir /usr/local/nginx/conf/vhost

 

在当前...vhost目录下,创建一个aaa.com.conf文件,并且加入内容,

这里说明,又有default_server代表默认为虚拟主机

server

{

       listen 80 default_server;

       server_name aaa.com;

       index index.html index.htm index.php;

       root /data/wwwroot/default;

}

 

3,创建目录,创建index.html文件

[root@localhost vhost]# mkdir /data/wwwroot

[root@localhost vhost]# mkdir /data/wwwroot/default

[root@localhost default]# /usr/local/nginx/sbin/nginx -t  //检测语法是否有错

更改完配置文件之后最好-t一下

也可以-s reload重新加载也是可以的

[root@localhost default]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost default]# curl -x127.0.0.1:80 aaa.com

This is the default site

[root@localhost default]# curl -x127.0.0.1:80 bbb.com

This is the default site

这里的意思是,只要定义了默认虚拟主机,只要指向到这里来,都会访问到这个位置

 

12.8 Nginx用户认证

目的是为了某些目录比如根目录下,登录需要认证

1,还是在vhost目录下

cd /usr/local/nginx/conf/vhost/ 编辑index.html

增加内容

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; //用户名密码文件

}

}

 

2,密码文件,如果是yum安装的apache:直接用htpasswd命令,创建nginx用户名密码

如果没有安装apachehttpd也是可以的

安装命令yum -y install httpd,因为是一个新的虚拟机,这里用httpd

这里我使用的是htpasswd直接安装

[root@localhost vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd zhao

注意,创建第二个则不需要-c-c会覆盖掉原来的文件

3,重新加载,测试

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

测试

因为没有对应test.com的目录,创建一个再测试

4,上面是对整个站点认证,如果需要对单个文件认证,可以修改路径、

先在test.com的目录下创建一个目录,并且创建index.html文件

[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/

编辑虚拟主机配置目录

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

检查nginx语法,加载,测试

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html

[root@localhost vhost]# curl -uzhao:123456 -x127.0.0.1:80 test.com/admin/

test.com admin dir

 

 

12.9 Nginx域名重定向

test.com.conf增加一个域名

Nginx上可以跟多个域名

 [root@localhost vhost]# curl -x127.0.0.1:80 test2.com/index.html -i

这里看到重定向到了test.com

 

 

12.10 Nginx访问日志

1,编辑...nginx.conf主配置文件,自定义日志格式名称

2,定义虚拟主机配置文件

配置完成之后检查

3,检查查看

[root@localhost vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -i

Cat查看 则有对应的日志了

 

 

12.11 Nginx日志切割

 

Nginx只能借助工具来定义脚本

[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh

脚本内容

cat /usr/local/sbin/nginx_log_rotate.sh

#! /bin/bash

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

logdir="/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`

日志切割在000秒的适合切割

3,执行shell脚本

[root@localhost vhost]#  sh -x /usr/local/sbin/nginx_log_rotate.sh

[root@localhost vhost]#  ls /tmp/*.log*  

如果需要清理

 find /tmp/ -name *.log-* -type f –mtime +30 |xargs rm 删除30天之前的

 

 

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

1,编辑虚拟主机配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加以下内容

上图有个错误,$1改成$

       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

       {

       expires 7d;

       access_log off;

       }

       location ~ .*\.(js|css)$

       {

       expires 12h;

       access_log off;

       }

[root@localhost ~]# /usr/local/nginx/sbin/nginx –t

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

 

 

2,进入网站目录创建图片等测试

[root@localhost ~]# cd /data/wwwroot/test.com/

[root@localhost test.com]# touch /data/wwwroot/test.com/1.jpg 2.png

 

[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.png -I

HTTP/1.1 200 OK

Server: nginx/1.8.0

看到访问js的文件,是没有记录日志的
12.13 Nginx防盗链

配置如下

 

       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;

       } 

 

2,配置好之后重新加载,并且测试

 

[root@localhost test.com]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload

 

用百度指向过来,则403,用test.com则是200OK

 

12.14Nginx访问控制

1,编辑虚拟主机文件,增加内容

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

         location /admin/

   {

            allow 127.0.0.1;

            allow 192.168.188.131;

            deny all;

 

   }

         access_log /tmp/test.com.log zhao;

2,检查语法,测试

 

这里我们增加一个ens37网卡测试

[root@localhost ~]# curl -x192.168.15.128:80 test.com/admin/

 

这里也可以匹配正则

1,进入配置文件,增加

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

 

 

增加内容

location ~ .*(upload|image)/.*\.php$

 

{

       deny all;

}     

2reload之后测试

 

而这里对1.txt的文件则不在限制

 

增加user agent匹配

编辑目录

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加内容

       if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')

              {

              return 403;

              }     

发现对Tomato开始的禁止访问

~* 'Spider/3.0|YoudaoBot|Tomato   //这里的*号匹配大小写

 

12.15Nginx解析php相关配置

 

1,还是进入到配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf,增加内容

location ~ \.php$

       {     

       include fastcgi_params;

       fastcgi_pass unix:/tmp/php-fcgi.sock;    //sock文件目录

       fastcgi_index index.php;

       fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;//上面root写的路径

       }

在不reload的情况下,是无法解析PHP的内容的,如下

Reload之后

这里显示的就是网页上的了

如果sock路径不对,那么就是502了,这里有一些记录,供参考

http://ask.apelearn.com/question/9109

错误日志,tail /usr/local/nginx/logs/nginx_err.log

 

12.16 Nginx代理

例如国内服务器访问美国很慢,而从香港到美国很快,可以从香港做一个代理服务器

 

1[root@localhost ~]# cd /usr/local/nginx/conf/vhost进入vhost目录创建proxy.conf文件,并且写入内容

server

 

{

   listen 80;

   server_name ask.apelearn.com;

   location /

   {

       proxy_pass      http://121.201.9.155/;

       proxy_set_header Host   $host;

       proxy_set_header X-Real-IP      $remote_addr;

       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   }

}

2reload之后测试

[root@localhost vhost]# curl ask.apelearn.com/robots.txt

#

# robots.txt for MiWen

#

 

User-agent: *

[root@localhost vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt  //代理

 

 

扩展
nginx.conf 配置详解 
https://coding.net/u/aminglinux/p/nginx/git/tree/master/3z
nginx rewrite四种flag 
http://unixman.blog.51cto.com/10163040/1711943 
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md 
502问题汇总 http://ask.apelearn.com/question/9109 
location优先级 https://coding.net/u/aminglinux/p/nginx/git/blob/master/location/priority.md

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值