网站首页 > 技术教程 正文
Nginx防盗链
Nginx防盗链也是使用location板块,和不记录静态文件和过期时间写在一起。
打开虚拟主机配置文件
[root@shuai-01 ~]# 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; } #不是白名单的referer ,返回403 access_log off; } 1 2 3 4 5 6 7 8 9 10 11 12 13
注意:location ~* ^.+.这里匹配到的后面的内容是不区分大小写。
测查配置文件语法并重新加载:
[root@shuai-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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload 1 2 3 4 5
测试:
[root@shuai-01 ~]# curl -e "http://www.qq.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif HTTP/1.1 403 Forbidden Server: nginx/1.12.2 Date: Tue, 09 Jan 2018 11:00:19 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@shuai-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 09 Jan 2018 11:01:15 GMT Content-Type: image/gif Content-Length: 12 Last-Modified: Mon, 08 Jan 2018 16:38:47 GMT Connection: keep-alive ETag: "5a539e97-c" Expires: Tue, 16 Jan 2018 11:01:15 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Nginx的访问控制
关于做防盗链和访问控制的原因我在httpd做访问控制和防盗链时已经说得比较清楚了。
http://blog.csdn.net/aoli_shuai/article/details/78895746
需求:访问/admin/目录,只允许那几个IP进行访问。
打开虚拟主机配置文件
location /admin/ { allow 127.0.0.1; allow 192.168.176.135; deny all; } 1 2 3 4 5 6 7
这里Nginx设置访问控制和Apache是有很大的不同,关于这个allow,deny。Apache是有一个顺序的,Nginx没有。
测查配置文件语法并重新加载:
[root@shuai-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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload 1 2 3 4 5
检测:
[root@shuai-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/admin/ HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 09 Jan 2018 11:23:23 GMT Content-Type: text/html Content-Length: 5 Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT Connection: keep-alive ETag: "5a54a628-5" Accept-Ranges: bytes [root@shuai-01 ~]# curl -x192.168.176.135:80 test.com/admin/ -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 09 Jan 2018 11:25:02 GMT Content-Type: text/html Content-Length: 5 Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT Connection: keep-alive ETag: "5a54a628-5" Accept-Ranges: bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
针对正则进行访问控制:
location ~ .*(abc|image)/.*\.php$ { deny all; } 1 2 3 4 5
针对user_agent进行限制:
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; } 1 2 3 4 5
return 403 和deny all 效果是一样的。
Nginx解析PHP的相关配置
核心配置:
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; # fastcgi_pass 127.0.0.1:9000; 如果php-fpm配置监听配置的是IP就写这个 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; } 1 2 3 4 5 6 7 8 9
Nginx要想解析PHP就要将这段核心配置写入配置文件中去。
fastcgi_pass 用来指定php-fpm监听的地址或者socket
如果是用的sock那么一定要放开php配置中的listen.mode=666(sock的权限位一定要有写的权限)
unix:/tmp/php-fcgi.sock这里的sock文件是php-fpm.conf中定义的
cat /usr/local/php-fpm/etc/php-fpm.conf配置文件中写什么就定义什么
如果php监听的是ip和端口,nginx中的配置文件就要改成
fastcgi_pass 127.0.0.1:9000;
fastcgi_param 中的路径也需要跟上面对应起来
Nginx502问题出现及解决方法:
http://ask.apelearn.com/question/9109
Nginx代理
Nginx代理分正向代理和反向代理。
http://blog.csdn.net/zjf280441589/article/details/51501408
Nginx代理是在一台代理服务器中自定义一个域名(这个域名基本上跟web服务器的域名相同),该域名指向一个IP(web服务器),然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。
做代理服务器要先做一个新的虚拟主机配置文件。
[root@shuai-01 ~]# cd /usr/local/nginx/conf/vhost/ [root@shuai-01 vhost]# [root@shuai-01 vhost]# vim proxy.conf 1 2 3 4
配置文件:
server { listen 80; server_name ask.apelearn.com;#定义代理服务器域名,一般的和web服务器域名相同 location / { proxy_pass http://121.201.9.155/;#指定web服务器IP proxy_set_header Host $host;#$host就是server_name proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14
保存退出并检查配置文件语法,重新加载
[root@shuai-01 vhost]# curl -x127.0.0.1:80 ask.apelear#.com/robots.txt # robots.txt for MiWen # User-agent: * Disallow: /?/admin/ Disallow: /?/people/ Disallow: /?/question/ Disallow: /account/ Disallow: /app/ Disallow: /cache/ Disallow: /install/
猜你喜欢
- 2024-10-09 Nginx实现高可用(一)——干货(nginx如何实现高可用)
- 2024-10-09 MAMP部署项目报403和404错误,如何解决?
- 2024-10-09 网站服务器租用预防CC攻击的方法(网站服务器租赁需要什么手续)
- 2024-10-09 Nginx防盗链(nginx防盗链 referer)
- 2024-10-09 触类旁通,从502错误看Nginx常见故障与修复
- 2024-10-09 使用 Nginx 一定要用好 444 状态码
- 2024-10-09 渗透测试中403/401页面绕过的思路
- 2024-10-09 Nginx-包教包会-进阶(nginx详细教程)
- 2024-09-10 16《Nginx 入门教程》Nginx防盗链配置
- 2024-09-10 《前端运维》二、Nginx-3静态资源服务、跨域与其他
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)