LNMP架构 (4) 之 Nginx的防盗链、访问控制、解析php相关配置
2018-03-30 00:00
946 查看
1. Nginx防盗链
Nginx防盗链可结合日志管理一起配置,因为该配置也要使用location板块[root@host ~]# 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@host ~]# /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@host ~]# /usr/local/nginx/sbin/nginx -s reload 检查 [root@host ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png HTTP/1.1 403 Forbidden Server: nginx/1.12.1 Date: Mon, 11 Sep 2017 11:25:47 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive 访问被拒绝,防盗链生效
2. Nginx访问控制
只允许几个指定IP通过访问/admin/目录的请求[root@host ~]# vim /usr/local/nginx/conf/vhost/test.com.conf …… location /admin/ { #设置IP白名单 allow 192.168.8.132; allow 127.0.0.1; deny all; } [root@host ~]# /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@host ~]# /usr/local/nginx/sbin/nginx -s reload
创建目录
[root@host ~]# mkdir /data/wwwroot/test.com/admin [root@host ~]# echo “test,test”>/data/wwwroot/test.com/admin/1.html
测试
[root@host ~]# curl -x127.0.0.1:80 test.com/admin/1.html “test,test” [root@host ~]# curl -x192.168.2.107: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相关配置
核心配置文件
[root@host ~]# 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中格式是一致的,否则会报502错误 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; #fastcgi _param SCRIPT _FILENAME所在行的路径要和root路径一致 }
3. Nginx代理
在计算机网络中,反向代理是代理服务器的一种。它根据客户端的请求,从后端的服务器上获取资源,然后再将这些资源返回给客户端。与前向代理不同,前向代理作为一个媒介将互联网上获取的资源返回给相关联的客户端,而反向代理是在服务器端作为代理使用,而不是客户端。3.1 Nginx作为反向代理的特点
接收用户请求是异步的,即先将用户请求全部接收下来,再一次性发送后后端web服务器,极大的减轻后端web服务器的压力;nginx代理和后端web服务器间无需长连接;
发送响应报文时,是边接收来自后端web服务器的数据,边发送给客户端的;
调度灵活。NGINX工作在网络协议栈的第七层,能够对HTTP应用请求进行解析和分流,支持比较复杂的正则规则,具有更优化的负载均衡效果。
网络依赖型低。NGINX对网络的依赖程度非常低,理论上讲,只要能够ping通就可以实施负载均衡,而且可以有效区分内网和外网流量。
支持服务器检测。NGINX能够根据应用服务器处理页面返回的状态码、超时信息等检测服务器是否出现故障,并及时返回错误的请求重新提交到其它节点上。
3.2 工作原理
Nginx代理是在一台代理服务器中自定义一个域名,该域名指向一个IP,然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。3.3 进入虚拟主机目录
[root@host ~]# cd /usr/local/nginx/conf/vhost/
3.4 创建代理服务器
[root@host vhost]# vim proxy.conf server { listen 80; server_name ask.apelearn.com; #定义域名 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; } } 设置好以后,就可以访问aminglinux论坛了
3.5 检测验证
#之前 [root@host vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> #之后 [root@host vhost]# /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@host vhost]# /usr/local/nginx/sbin/nginx -s reload [root@host 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/
相关文章推荐
- 【LNMP】Nginx防盗链、Nginx访问控制、Nginx解析php相关配置和Nginx代理
- LNMP(nginx防盗链,访问控制,解析php相关配置,Nginx代理,常见502问题)
- linux学习第四十六篇:Nginx防盗链,Nginx访问控制,Nginx解析php相关配置,Nginx代理
- LNMP架构(四)之Nginx防盗链,访问控制,解析php,代理
- LNMP架构(四)Nginx防盗链-访问配置-解析PHP配置
- 十二周四次课 2018.01.05 Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理
- 12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置 12.16 Nginx代理
- Nginx配置防盗链、Nginx访问控制、Nginx解析php相关配置
- linux的Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理介绍
- Nginx防盗链、访问控制、Nginx解析PHP相关配置、Nginx代理
- Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理
- Nginx防盗链、Nginx访问控制、Nginx解析php的配置
- Nginx防盗链,Nginx访问控制,,Nginx解析php相关配置,Nginx代理
- 详解Nginx防盗链和Nginx访问控制与Nginx解析php的配置
- Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理
- Nginx防盗链、Nginx访问控制、Nginx解析php相关配置
- Nginx防盗链、访问监控、解析php相关配置,Nginx代理
- Nginx防盗链,Nginx访问控制, Nginx解析php相关配置, Nginx代理
- Nginx防盗链,Nginx访问控制,Nginx解析PHP的相关配置,Nginx代理