11月28日任务
12.13 Nginx防盗链
12.14 Nginx访问控制
12.15 Nginx解析php相关配置
12.16 Nginx代理
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;
}
access_log off;
}
#操作过程
[root@zgxlinux-01 conf]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@zgxlinux-01 conf]# /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@zgxlinux-01 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 conf]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:00:28 GMT
Content-Type: application/javascript
Content-Length: 32
Last-Modified: Sat, 01 Dec 2018 04:04:55 GMT
Connection: keep-alive
ETag: "5c020867-20"
Accept-Ranges: bytes
[root@zgxlinux-01 conf]# curl -x127.0.0.1:80 -I test.com/2.jif
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:00:49 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 conf]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 11:05:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
12.14 Nginx访问控制
- 需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:
location /admin/
{
allow 192.168.133.1;
allow 127.0.0.1;
deny all;
}
- mkdir /data/wwwroot/test.com/admin/
- echo “test,test”>/data/wwwroot/test.com/admin/1.html
- -t && -s reload
- curl -x127.0.0.1:80 test.com/admin/1.html -I
- curl -x192.168.133.130:80 test.com/admin/1.html -I
#操作过程
[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
#允许这两个ip访问 ,其他拒绝
[root@zgxlinux-01 ~]# /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@zgxlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:43:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:45:07 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 01 Dec 2018 02:46:36 GMT
Connection: keep-alive
ETag: "5c01f60c-12"
Accept-Ranges: bytes
[root@zgxlinux-01 ~]# curl -x192.168.56.128:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 00:46:35 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Sat, 01 Dec 2018 02:46:36 GMT
Connection: keep-alive
ETag: "5c01f60c-12"
Accept-Ranges: bytes
[root@zgxlinux-01 ~]# cat /tmp/test.com.log
127.0.0.1 - [01/Dec/2018:12:07:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:12:08:29 +0800] test.com "/2.jsdsfdss" 404 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:19:00:49 +0800] test.com "/2.jif" 404 "-" "curl/7.29.0"
127.0.0.1 - [02/Dec/2018:08:44:19 +0800] test.com "/admin" 301 "http://www.baidu.com/1.txt" "curl/7.29.0"
127.0.0.1 - [02/Dec/2018:08:45:07 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
192.168.56.128 - [02/Dec/2018:08:46:35 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
[root@zgxlinux-01 ~]# dhclient ens37 #给ens37自动获取ip
[root@zgxlinux-01 ~]# ifconfig
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.32.128
- 可以匹配正则
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
- 根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
- deny all和return 403效果一样
[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@zgxlinux-01 ~]# /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@zgxlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 ~]# curl -x192.168.32.128:80 -I test.com/upload/
1.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 01:10:40 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@zgxlinux-01 ~]# mkdir /data/wwwroot/test.com/upload
[root@zgxlinux-01 ~]# echo "111111" > /data/wwwroot/test.com/upload/1.txt
[root@zgxlinux-01 ~]# curl -x192.168.32.128:80 -I test.com/upload/1.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sun, 02 Dec 2018 01:13:42 GMT
Content-Type: text/plain
Content-Length: 7
Last-Modified: Sun, 02 Dec 2018 01:13:31 GMT
Connection: keep-alive
ETag: "5c0331bb-7"
Accept-Ranges: bytes
12.15 Nginx解析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;
}
- fastcgi_pass 用来指定php-fpm监听的地址或者socket
#操作过程
[root@zgxlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf #配置不执行,我们先做一个实验,看看能否解析php
[root@zgxlinux-01 ~]# vim /data/wwwroot/test.com/3.php
[root@zgxlinux-01 ~]# curl -x127.0.0.1:80 test.com/3.php #只能显示出源码
<?php
phpinfo();
[root@zgxlinux-01 ~]# /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@zgxlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 ~]# curl -x127.0.0.1:80 test.com/3.php #此时我们就可以看到解析php的结果了。
[root@zgxlinux-01 ~]# vi /usr/local/php-fpm/etc/php-fpm.conf
12.16 Nginx代理
- cd /usr/local/nginx/conf/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@zgxlinux-01 ~]# cd /usr/local/nginx/conf/vhost/
[root@zgxlinux-01 vhost]# vim proxy.conf