12.13 Nginx防盗链
因为该配置也使用location板块,所以本节可结合日志管理一起配置:
[root@1 ~]# 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 ; #定义referer白名单 if ($invalid_referer) { return 403; #if函数的意思是:如果不是白名单内的域名,返回值:403 } access_log off;}……[root@1 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload
说明: “location ~* ^.+”在此0“ * ”的作用是后面匹配的内容不区分大小写。
检测
[root@1 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.pngHTTP/1.1 403 ForbiddenServer: nginx/1.12.1Date: Mon, 14 Aug 2017 06:22:36 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
即,使用非白名单内的referer进行访问,被拒绝!!!
12.14 Nginx访问控制
需求:访问/admin/目录的请求,只允许几个指定IP通过,配置如下:
[root@1 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf ……location /admin/ { allow 192.168.8.132; allow 127.0.0.1; deny all; #设置IP白名单 }……[root@1 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@1 ~]# /usr/local/nginx/sbin/nginx -s reload
创建上面指定的目录:
[root@1 ~]# mkdir /data/wwwroot/test.com/admin[root@1 ~]# echo “test,test”>/data/wwwroot/test.com/admin/1.html
测试
[root@1 ~]# curl -x127.0.0.1:80 test.com/admin/1.html“test,test”[root@1 ~]# curl -x192.168.8.132:80 test.com/admin/1.html“test,test”
访问控制——正则匹配
location ~ .*(abc|image)/.*\.php${ deny all;}
访问控制——user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato'){ return 403;}
说明: deny all和return 403效果一样
12.15 Nginx解析PHP相关配置
核心配置:
vim /usr/local/nginx/conf/vhost/test.com.conf……location ~ \.php$ { include fastcgi_params; #fastcgi_pass 127.0.0.1:9000 fastcgi_pass unix:/tmp/php-fcgi.sock; ##fastcgi_pass两种监听格式,但是要保证Nginx和php-fpm中格式一致 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }……
注: 在此注意两点,fastcgi_pass有两种格式,但是无论使用哪种格式都有保证Nginx和php-fpm中格式一致,否则会报错502;fastcgi _param SCRIPT _FILENAME所在行的路径要和root路径一致!
12.16 Nginx代理
Nginx代理是一种反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
工作原理
Nginx代理是在一台代理服务器中自定义一个域名,该域名指向一个IP,然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。
graph LR用户-->代理服务器代理服务器-->用户代理服务器-->web服务器web服务器-->代理服务器
进入虚拟主机目录:
[root@1 ~]# cd /usr/local/nginx/conf/vhost/
创建代理服务器
[root@1 vhost]# vim proxy.confserver{ listen 80; server_name ask.apelearn.com; #定义域名(一般和被代理ip的域名保持一致) location / { proxy_pass http://121.201.9.155/; #指定被代理(被访问)的IP(web服务器IP) proxy_set_header Host $host; #$host指的是代理服务器的servername(也是被代理IP的域名) proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
说明: 因为该虚拟主机只用作代理服务器,不需要访问本地文件,所以不需要设置根目录。
检测
设置代理前
[root@1 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt404 Not Found 404 Not Found
nginx/1.12.1
设置代理后
[root@1 vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@adailinux vhost]# /usr/local/nginx/sbin/nginx -s reload[root@1 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt## robots.txt for MiWen#User-agent: *Disallow: /?/admin/Disallow: /?/people/Disallow: /?/question/Disallow: /account/Disallow: /app/Disallow: /cache/