编辑虚拟配置文件
[root@100xuni1 ~]# 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 ; ???if ($invalid_referer) { ???????return 403; ???} ???access_log off;}
添加配置内容解释
检测配置和重新加载配置
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload[root@100xuni1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
测试
Nginx访问控制
编辑虚拟配置文件允许ip和不允许ip访问这是针对目录的
[root@100xuni1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/{ ???allow 192.168.133.1; ???allow 127.0.0.1; ???deny all;}
添加规则和解释
检查配置和加载
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload
测试
[root@100xuni1 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/ ???##是白名单里的ip可以访问HTTP/1.1 200 OKServer: nginx/1.14.0Date: Wed, 15 Aug 2018 09:08:28 GMTContent-Type: text/htmlContent-Length: 18Last-Modified: Tue, 14 Aug 2018 03:25:24 GMTConnection: keep-aliveETag: "5b724ba4-12"Accept-Ranges: bytes
[root@100xuni1 ~]# curl -x192.168.63.100:80 -I test.com/admin/ ?????##是白名单里的ip可以访问HTTP/1.1 200 OKServer: nginx/1.14.0Date: Wed, 15 Aug 2018 09:09:38 GMTContent-Type: text/htmlContent-Length: 18Last-Modified: Tue, 14 Aug 2018 03:25:24 GMTConnection: keep-aliveETag: "5b724ba4-12"Accept-Ranges: bytes
[root@100xuni1 ~]# curl -x192.168.0.110:80 -I test.com/admin/ ????##不是白名单的ip不能访问
HTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Wed, 15 Aug 2018 09:23:08 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
编辑虚拟配置文件这是针对正则的
[root@100xuni1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ .*(abc|image)/.*\.php${ ???????deny all;}
检查配置和加载
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload
测试
[root@100xuni1 ~]# mkdir /data/wwwroot/test.com/upload ????##做一个模拟创建一个upload目录
[root@100xuni1 ~]# echo "111" > /data/wwwroot/test.com/upload/1.php ????##在upload里面创建个PHP文件
[root@100xuni1 ~]# curl -x127.0.0.1:80 test.com/upload/1.php ????##测试访问upload/1.php是被拒绝的
<html><head><title>403 Forbidden</title></head><body bgcolor="white"><center><h1>403 Forbidden</h1></center><hr><center>nginx/1.14.0</center></body></html>
根据user_agent限制配置虚拟文件
[root@100xuni1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
if ($http_user_agent ~ ‘Spider/3.0|YoudaoBot|Tomato‘){ ?????return 403;}
检查配置和加载
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload
模拟测试
[root@100xuni1 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OKServer: nginx/1.14.0Date: Wed, 15 Aug 2018 10:12:25 GMTContent-Type: text/plainContent-Length: 4Last-Modified: Wed, 15 Aug 2018 10:12:22 GMTConnection: keep-aliveETag: "5b73fc86-4"Accept-Ranges: bytes
[root@100xuni1 ~]# curl -A "Tomatoalsdkflsd" -x127.0.0.1:80 test.com/upload/1.txt -I ????##模拟user_agent
HTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Wed, 15 Aug 2018 10:14:28 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
Nginx解析php相关配置
因为现在配置虚拟文件里还不能解析php所以配置
[root@100xuni1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
需要添加的内容
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; ???}
做个PHP
[root@100xuni1 ~]# vim /data/wwwroot/test.com/3.php
<?php ????????????????????##添加到3.php里边phpinfo();
做一下测试
[root@100xuni1 ~]# curl -x127.0.0.1:80 test.com/3.php ????##不能解析直接显示源码<?phpphpinfo();
之前做了配置没有重新加载所以上边的解析没有成功下面加载
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload
**加载完成后在做测试可以解析php
**
解释配置的信息报错502
下面把php-fpm.conf配置做下更改我要监听ip端口
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload ????##重新加载[root@100xuni1 ~]# /etc/init.d/php-fpm reload ?????##重启php
Nginx代理
写个新的配置文件
[root@100xuni1 ~]# cd /usr/local/nginx/conf/vhost/ ??##进入vhost[root@100xuni1 vhost]# vim 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; ???}}
检查配置和加载
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 ~]# /usr/local/nginx/sbin/nginx -s reload
测试
48次课(Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理)
原文地址:http://blog.51cto.com/8043410/2160488