48次課(Nginx防盜鏈、Nginx訪問控制、Nginx解析php相關配置、Nginx代理)
阿新 • • 發佈:2018-08-16
curl urn real-ip connect referer ini adf accept txt 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
測試
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 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
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 ##不能解析直接顯示源碼
<?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
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 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代理)