标签:拒绝 init fda 白名单 img index.php oss 解释 配置文件
Nginx防盗链编辑虚拟配置文件
[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
测试
编辑虚拟配置文件允许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 ok
nginx: 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 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:08:28 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Tue, 14 Aug 2018 03:25:24 GMT
Connection: keep-alive
ETag: "5b724ba4-12"
Accept-Ranges: bytes
[root@100xuni1 ~]# curl -x192.168.63.100:80 -I test.com/admin/ ##是白名单里的ip可以访问
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:09:38 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Tue, 14 Aug 2018 03:25:24 GMT
Connection: keep-alive
ETag: "5b724ba4-12"
Accept-Ranges: bytes
[root@100xuni1 ~]# curl -x192.168.0.110:80 -I test.com/admin/ ##不是白名单的ip不能访问
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:23:08 GMT
Content-Type: text/html
Content-Length: 169
Connection: 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 ok
nginx: 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 ok
nginx: 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 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 10:12:25 GMT
Content-Type: text/plain
Content-Length: 4
Last-Modified: Wed, 15 Aug 2018 10:12:22 GMT
Connection: keep-alive
ETag: "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 Forbidden
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 10:14:28 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
因为现在配置虚拟文件里还不能解析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 ##不能解析直接显示源码
<?php
phpinfo();
之前做了配置没有重新加载所以上边的解析没有成功下面加载
[root@100xuni1 ~]# /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@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
写个新的配置文件
[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 ok
nginx: 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代理)
标签:拒绝 init fda 白名单 img index.php oss 解释 配置文件
原文地址:http://blog.51cto.com/8043410/2160488