当前位置: 首页 > news >正文

LNMP架构 (Ⅱ)——nginx相关配置、nginx代理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

LNMP架构 (Ⅱ)

六、Nginx默认虚拟主机

在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机,但和httpd不相同的地方是,它还有一个配置用来标记默认虚拟主机,也就是说,如果没有这个标记,第一个虚拟主机为默认虚拟主机。

编辑nginx.conf主配置文件

[root@ying01 ~]# cd /usr/local/nginx/conf/
[root@ying01 conf]# vim /usr/local/nginx/conf/nginx.conf

具体看下图操作:

创建vhost目录,并新建aaa.com.conf默认虚拟主机配置内容;

[root@ying01 conf]# pwd
/usr/local/nginx/conf
[root@ying01 conf]# mkdir vhost               //创建vhost目录
[root@ying01 conf]# cd vhost/
[root@ying01 vhost]# ls
[root@ying01 vhost]# vim aaa.com.conf        

以下为aaa.com.conf内容:

server
    {
        listen 80 default_server;                  //默认虚拟主机服务
        server_name aaa.com;                       //主机名 aaa.com
        index index.html index.htm index.php;      //定义索引页
        root /data/wwwroot/default;                //默认虚拟主机网站目录
    }

创建默认的网站目录

[root@ying01 vhost]# mkdir /data/wwwroot/default
[root@ying01 vhost]# cd /data/wwwroot/default/
[root@ying01 default]# vim index.html                    //建立index.html文件

以下为index.html 内容:

this is the default site.

检测语法,重新加载配置文件;测试相关网站;任意的域名,都会指向默认主机的网站名;

[root@ying01 default]# /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@ying01 default]# /usr/local/nginx/sbin/nginx -s reload
[root@ying01 default]# curl localhost                   //访问主机
this is the default site.
[root@ying01 default]# curl -x127.0.0.1:80 aaa.com      //访问主机名aaa.com
this is the default site.
[root@ying01 default]# curl -x127.0.0.1:80 ddd.com      //任意的域名,都指向主机名
this is the default site.
[root@ying01 default]# curl -x127.0.0.1:80 qq.com
this is the default site.

查看主配置文件;

[root@ying01 default]# tail /usr/local/nginx/conf/nginx.conf
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    include vhost/*.conf;
}

最后一行就是包含了默认主机的配置,也可以把默认主机配置内容放置到下面,效果是一样的;

** include vhost/*.conf** 相当于一个虚拟主机的配置内容的模块,

七、Nginx用户认证

[root@ying01 default]# cd -
/usr/local/nginx/conf/vhost
[root@ying01 vhost]# ls
aaa.com.conf
[root@ying01 vhost]# vim 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;
     }
}


创建用户;

由于nginx没有自带创建用户的工具,因此需要借助httpd工具;假如没有,则用此命令 yum install -y httpd;因为本机已经安装,因此直接执行;

[root@ying01 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd ying
New password:                                                        //设置密码位www123
Re-type new password: 
Adding password for user ying
[root@ying01 vhost]# cat /usr/local/nginx/conf/htpasswd              //查看密码生成文件
ying:$apr1$I3caHAA/$wMALhLwm.1FKdqqJQZj0h0

[root@ying01 vhost]# /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd feng  //继续创建用户
New password: 
Re-type new password: 
Adding password for user feng
[root@ying01 vhost]# cat /usr/local/nginx/conf/htpasswd             //此时有两个密码文件生成
ying:$apr1$JRTvjHxp$idElRt2smV.wCQImpZ04w0
feng:$apr1$7kZQZ4VM$2O8ncLmdmqAsyrcvrZ3tH.

测试

测试前需要检查语法错误,以及重新加载配置文件;

[root@ying01 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@ying01 vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@ying01 vhost]# curl -x127.0.0.1:80 test.com 
<html>
<head><title>401 Authorization Required</title></head>   //出现401码,需要用户认证
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@ying01 vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 11:52:40 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

用户认证测试主机

[root@ying01 vhost]# curl -uying:www123 -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@ying01 vhost]# ls /data/wwwroot/test.com
ls: 无法访问/data/wwwroot/test.com: 没有那个文件或目录
[root@ying01 vhost]# mkdir /data/wwwroot/test.com
[root@ying01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@ying01 vhost]# curl -uying:www123 -x127.0.0.1:80 test.com
test.com
[root@ying01 vhost]# curl -uying:www123 -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 12:02:26 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Thu, 05 Jul 2018 11:58:32 GMT
Connection: keep-alive
ETag: "5b3e07e8-9"
Accept-Ranges: bytes

有时候我们需要对某个访问目录或者页面进行认证,而不是全站。所以我们需要对配置文件进行更改:

[root@ying01 vhost]# vim test.com.conf 

以下为更改的配置内容....

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;
     }
}

开始测试某个目录

[root@ying01 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@ying01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@ying01 vhost]# curl -x127.0.0.1:80 test.com
test.com
[root@ying01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@ying01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@ying01 vhost]# curl -uying:www123 -x127.0.0.1:80 test.com/admin/
test.com admin dir

[root@ying01 vhost]# vim test.com.conf 

以下为更改的配置内容....

server
{
   listen 80;
   server_name test.com;
   index index.html index.htm index.php;
   root /data/wwwroot/test.com;

   location  ~ admin.php          //注意:此处有更改;表示根目录下的admin.php文件
     {
       auth_basic         "Auth";
       auth_basic_user_file  /usr/local/nginx/conf/htpasswd;
     }
}
[root@ying01 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@ying01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@ying01 vhost]# curl -x127.0.0.1:80 test.com/admin/             //此时不需要用户认证
test.com admin dir
[root@ying01 vhost]# curl -x127.0.0.1:80 test.com/admin.php            
<html>
<head><title>401 Authorization Required</title></head>                //此时需要用户认证
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>

总结:

  • location /:针对整个目录做认证

也可以针对某一个目录或url做认证,比如:

  • location /admin/:针对admin目录做认证
  • location ~ admin.php:针对某个请求的url做认证

auth_basic_user_file:用户认证文件

八、Nginx域名重定向

当我们站点有多个域名的时候,权重降低了,但是之前的域名已经被一部分人所依赖了,也不可能去通知大家新的站点,所以我们就会选择一个主域名其它的均302跳转过来!

[root@ying01 vhost]# vim test.com.conf 

以下为更改的配置内容....

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;   //永久跳转
   }
}

permanent:永久跳转,也就是301

redirect:临时跳转,302

在Nginx配置在,server_name后面可以跟多个域名,permanent为永久重定向,相当于httpd的R=301.另外还有一个常用的redirect,相当于httpd的R=302.

[root@ying01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 12:38:40 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html           //重定向test

[root@ying01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 12:38:47 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html           //重定向test

[root@ying01 vhost]# curl -x127.0.0.1:80 www.baidu.com/index.html    //重定向于默认虚拟主机

九、Nginx日志

9.1 Nginx访问日志

nginx日志的选项:

名词释义
$remote_addr客户端ip(公网ip)
$http_x_forwarded_for代理服务器的ip
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer
$http_user_agentuser_agent

在nginx主配置文件定义日志的,其中combined_realip为日志的名称,这个名称可以自定义,比如这里自定义为 ying

[root@ying01 vhost]# vim ../nginx.conf

在nginx主配置文件里,按下图并定义日志名称

在虚拟主机配置文件里,定义日志目录和格式、名称;

[root@ying01 vhost]# vim test.com.conf 

以下为更改的配置内容....
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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
   access_log /tmp/test.com.log ying;           //定义日志格式 和目录
}


检测、加载配置后,进行测试;

[root@ying01 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@ying01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@ying01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 13:02:43 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html            

[root@ying01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 05 Jul 2018 13:02:47 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html

[root@ying01 vhost]# cat /tmp/test.com.log              //查看生成的日志
127.0.0.1 - [05/Jul/2018:21:02:43 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"   //依次为日志格式
127.0.0.1 - [05/Jul/2018:21:02:47 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"
[root@ying01 vhost]#

9.2 Nginx日志切割

由于Nginx不像Apache有自己的切割工具,在此我们需要写个脚本完成需求:

[root@ying01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh

以下为脚本内容:

#! /bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"                        //假设nginx的日志存放路径为/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`

脚本语句解释:

d=date -d "-1 day" +%Y%m%d;生成昨天的日期

[root@ying01 vhost]# date -d "-1 day" +%Y%m%d   //执行这个语句,可以得出答案
20180704
[root@ying01 vhost]# date
2018年 07月 05日 星期四 21:07:49 CST
 for log in ls *.log
 do
 mv $log $log-$d
 done

这是一个for循环,把ls列举的log文件,执行以日期格式的重命名

nginx_pid=”/usr/local/nginx/logs/nginx.pid”; 就是为了最后一行而设定的。

/bin/kill -HUP cat $nginx_pid

最后一行的意思和之前使用的 -s reload 是一个意思 重载nginx.pid,然后就会再次生成一个新的日志文件。否则不生成日志文件

sh -x 脚本详细执行过程:

[root@ying01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180704
+ 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-20180704
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180704
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 913

查看生成的test.com日志

[root@ying01 vhost]# ls /tmp/
pear
php_errors.log-20180704
php-fcgi.sock
systemd-private-94cc0dd6651e4992848100fb05207857-chronyd.service-1zARDS
systemd-private-94cc0dd6651e4992848100fb05207857-vgauthd.service-0jUT25
systemd-private-94cc0dd6651e4992848100fb05207857-vmtoolsd.service-zegNFj
test.com.log
test.com.log-20180704

日志清理

删除超过一个月的日志(当然这个也可以写在脚本里面)

[root@ying01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm

创建执行脚本的计划:比如:每天0时0分进行切割

[root@ying01 vhost]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

以下为创建的crontab内容:

0 0 * * * /usr/local/sbin/nginx_log_rotate.sh     //每天的0时0分执行此脚本

扩展:日志的切割

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

虚拟主机配置文件location~可以指定对应的静态文件,expires配置过期时间,而access_log 配置为off就可以不记录访问日志了

  • 配置文件

按以下设置虚拟主机配置文件;

[root@ying01 vhost]# vim test.com.conf 

以下为更改的配置内容....


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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$     //匹配.gif等格式的静态文件不计入日志
    {
          expires      7d;                        //有效期7天
          access_log off;                         //不记录日志
    }
location ~ .*\.(js|css)$                          //匹配js或者css文件
    {
          expires      12h;                       //有效期12小时
          access_log off;
    }

   access_log /tmp/test.com.log ying;
}

  • 测试

在网站test.com目录下,创建gif和css文件

[root@ying01 vhost]# cd /data/wwwroot/test.com/
[root@ying01 test.com]# ls
admin  index.html
[root@ying01 test.com]# vim 1.gif
[root@ying01 test.com]# vim 2.css

现在开始访问,然后看生成的日志;从下面试验,可以看出日志不记录gif及css文件;

[root@ying01 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@ying01 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@ying01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
aaaaaaaa

[root@ying01 test.com]# curl -x127.0.0.1:80 test.com/2.css
bbbbbbbbb
[root@ying01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@ying01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jul/2018:23:33:01 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@ying01 test.com]# curl -x127.0.0.1:80 test.com/2.css
bbbbbbbbb
[root@ying01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jul/2018:23:33:01 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

十、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_refere) {
          return 403;
      }
      access_log off;
   }

把此代码,放入虚拟主机配置中;

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

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }   
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;                                              //不符合,无效的引用者,则返回403;
      }
      access_log off;                                             
     } 
location ~ .*\.(js|css)$
    {
    #      expires      12h;
          access_log off;
    }     
    
   access_log /tmp/test.com.log ying;
}  

检查语句,并加载配置文件

[root@ying01 ~]# /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@ying01 ~]# /usr/local/nginx/sbin/nginx -s reload

测试,针对有效referer和无效referer的对比;

[root@ying01 ~]# curl -e "http://www.qq.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden                            //无效refer,返回403
Server: nginx/1.4.7         
Date: Fri, 06 Jul 2018 00:48:58 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

root@ying01 ~]# curl -e "http://xx.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 200 OK                                   //白名单的refer
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 00:51:19 GMT
Content-Type: image/gif
Content-Length: 10
Last-Modified: Thu, 05 Jul 2018 15:29:40 GMT
Connection: keep-alive
ETag: "5b3e3964-a"
Expires: Fri, 13 Jul 2018 00:51:19 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes

十一、Nginx访问控制

为了提高安全性,我们需要将某些页面加密处理!

11.1 针对某个目录设置

访问控制的核心代码;

location /admin/             //在admin目录下操作

{
    allow 127.0.0.1;
    allow 192.168.112.136; 
    deny all;
}

把此代码,放入虚拟主机配置中;

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

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
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;
    }
    location /admin/
    {
     #allow 127.0.0.1;        //注意不执行,可以测试的时候做对比
      allow 192.168.72.130;
      deny all;
    }
    
   access_log /tmp/test.com.log ying;
}

检查语句,并加载配置文件

[root@ying01 ~]# /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@ying01 ~]# /usr/local/nginx/sbin/nginx -s reload

测试,通过允许192.1638.112.136和禁止127.0.0.1来做实验,这两个IP主机都能连接到;

[root@ying01 ~]# curl -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 403 Forbidden                    //禁止访问,因为这个IP禁止
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 01:30:37 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

[root@ying01 ~]# curl -x192.168.112.136:80 -I test.com/admin/
HTTP/1.1 200 OK                           //这个IP可以访问
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 01:32:18 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Thu, 05 Jul 2018 12:09:55 GMT
Connection: keep-alive
ETag: "5b3e0a93-13"
Accept-Ranges: bytes

11.2 针对目录下的某类文件

这里主要是为了防止上传php文件,以免造成木马文件,影响安全;

在上传目录upload和image,禁止.php的文件;

location ~ .*(upload|image)/.*\.php$
    {
        deny all;
    }

把此代码,放入虚拟主机配置中;

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

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
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;
    }
    location /admin/
    {
     #allow 127.0.0.1;       
      allow 192.168.72.130;
      deny all;
    }
    location ~ .*(upload|image)/.*\.php$          //匹配.php文件
    {
        deny all;                                 //禁止
    }
    
   access_log /tmp/test.com.log ying;
}

检查语句,并加载配置文件

[root@ying01 ~]# /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@ying01 ~]# /usr/local/nginx/sbin/nginx -s reload

测试:在upload目录下,分别创建1.txt和1.php文件,能够访问1.txt,不能够访问1.php;

[root@ying01 ~]# echo "1111" > /data/wwwroot/test.com/upload/1.php
[root@ying01 ~]# echo "2222" > /data/wwwroot/test.com/upload/1.txt
[root@ying01 ~]# curl -x192.168.112.136:80 test.com/upload/1.php
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@ying01 ~]# curl -x192.168.112.136:80 test.com/upload/1.txt
2222

11.3 根据user-agent限制

不想被蜘蛛爬自己的网站,我们完全可以根据user-agent去禁止掉

禁止相关的user-agent,访问网站;

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}

把此代码,放入虚拟主机配置中;

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

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
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;
    }
    location /admin/
    {
     #allow 127.0.0.1;       
      allow 192.168.72.130;
      deny all;
    }
    location ~ .*(upload|image)/.*\.php$          
    {
        deny all;                                 
    }
    if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') //user_agent匹配'Spider/3.0|YoudaoBot|Tomato
    {
      return 403;
    }
   access_log /tmp/test.com.log ying;
}

检查语句,并加载配置文件

[root@ying01 ~]# /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@ying01 ~]# /usr/local/nginx/sbin/nginx -s reload

测试user_agent,不同值的试验

[root@ying01 ~]# curl -A "Tomato" -x192.168.112.136:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden                        //user_agent为Tomato,禁止访问
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 02:47:01 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

[root@ying01 ~]# curl -A "Spider/3.0" -x192.168.112.136:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden                        //user_agent为Spider/3.0,禁止访问
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 02:47:40 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

[root@ying01 ~]# curl -A "123456" -x192.168.112.136:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK                              //user_agent为除设置的3个外,任意指定,可以访问
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 02:47:54 GMT
Content-Type: text/plain
Content-Length: 5
Last-Modified: Fri, 06 Jul 2018 02:31:59 GMT
Connection: keep-alive
ETag: "5b3ed49f-5"
Accept-Ranges: bytes

十二、Nginx解析php相关配置

先创建一个3.php文件;

[root@ying01 ~]# vim /data/wwwroot/test.com/3.php


<?php
phpinfo();

测试这个3.php文件,此时不能够解析;

[root@ying01 ~]# curl -x192.168.112.136:80 test.com/3.php 
<?php
phpinfo();

解析php文件的配置文件

location ~ \.php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
      }

把此代码,放入虚拟主机配置中;

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

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') {
       rewrite ^/(.*)$  http://test.com/$1 permanent;
   }
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;
    }
    location /admin/
    {
     #allow 127.0.0.1;       
      allow 192.168.72.130;
      deny all;
    }
    location ~ .*(upload|image)/.*\.php$          
    {
        deny all;                                 
    }
    if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') 
    {
      return 403;
    }
    location ~ \.php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
      }
    
   access_log /tmp/test.com.log ying;
}

检查语句,并加载配置文件

[root@ying01 ~]# /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@ying01 ~]# /usr/local/nginx/sbin/nginx -s reload

由于用curl测试,篇幅过长,在浏览器测试:从下图可以看出能够解析php

解析php代码释义:

其中fastcgi_pass用来指定php-fpm的地址,如果php-fpm监听的是一个tcp:port的地址(比如127.0.0.1:9000),那么也需要在这里改成fastcgi_pass 127.0.0.1:9000。这个地址一定要和php-fpm服务监听的地址匹配,否是会报502错误.还有一个地方要注意fastcgi_param SCRIPT_FILENAME 后面跟的路径为该站点的根目录,和前面定义的root那个路径保持一致,如果这里配置不对,访问PHP页面会出现404;还有一种502的现象,如果内存中出现大量的php-fpm进程占据了内存,也会同样导致此问题!

十三、Nginx代理

原理:Nginx代理是一种反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

假如这家公司有很多台服务器,为了节省成本,不能为所有的服务器都分配公网IP,而如果一个没有公网的IP的复为其要提供web服务,就可以通过代理来实现,这就是 Nginx比httpd越来越受欢迎的原因

创建proxy.conf配置文件,写入以下代码;

[root@ying01 ~]# cd /usr/local/nginx/conf/vhost
[root@ying01 vhost]# vim proxy.conf

server
{
    listen 80;
    server_name ask.apelearn.com;
    location /
    {
        proxy_pass      http://47.91.145.78/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

因为是代理服务器所以不需要访问本地服务器的任何文件; ask.apelearn.com; 定义一个域名;

proxy_pass http://47.91.145.78/;真实WEB服务器的IP地址。

$host; 也就是咱们的server_name

检查语句,并加载配置文件

[root@ying01 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@ying01 vhost]# /usr/local/nginx/sbin/nginx -s reload

开始测试:127.0.0.1就是自己的代理机,访问论坛

[root@ying01 vhost]#  curl -x127.0.0.1:80 ask.apelearn.com -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Fri, 06 Jul 2018 03:50:53 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.3
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: ape__Session=tki4271fdrd4nup0jbdco33b63; path=/; domain=.apelearn.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
myheader: web1

测试网站的robots

[root@ying01 vhost]#  curl ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#

User-agent: *

Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/[root@ying01 vhost]# 

转载于:https://my.oschina.net/u/3851633/blog/1841084

相关文章:

  • ----------
  • 重装系统的时候重装不了该怎么办
  • EOS技术贴-如何创建EOS钱包和账号并发起转账和投票
  • 一、typescript介绍和安装
  • OpenStact之cinder
  • 对Node的优点和缺点提出了自己的看法?
  • 【刷算法】求1+2+3+...+n
  • 浅谈JavaScript错误
  • 洛谷P1341 无序字母对
  • 十三、数据源的配置
  • Promise 使用技巧九则
  • Linux ,强制更新只读文件,强制写入命令
  • 卸载pip工具
  • Ubuntu 12.04将默认集成Landscape管理套件【转】
  • 基础技能 | Git
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Android Studio:GIT提交项目到远程仓库
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • Django 博客开发教程 16 - 统计文章阅读量
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • js数组之filter
  • Python连接Oracle
  • React组件设计模式(一)
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • 从零开始学习部署
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 开源地图数据可视化库——mapnik
  • 坑!为什么View.startAnimation不起作用?
  • 力扣(LeetCode)21
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 区块链将重新定义世界
  • 什么是Javascript函数节流?
  • 学习笔记TF060:图像语音结合,看图说话
  • 因为阿里,他们成了“杭漂”
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • #{} 和 ${}区别
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #Spring-boot高级
  • (02)Hive SQL编译成MapReduce任务的过程
  • (175)FPGA门控时钟技术
  • (3)选择元素——(17)练习(Exercises)
  • (BFS)hdoj2377-Bus Pass
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)ssm学生管理系统 毕业设计 141543
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .net Application的目录
  • .net/c# memcached 获取所有缓存键(keys)
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题
  • .NET关于 跳过SSL中遇到的问题
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .py文件应该怎样打开?