分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

49.Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理

发布时间:2023-09-06 01:51责任编辑:熊小新关键词:配置
一、Nginx防盗链
  • 配置如下,可以和上面的配置结合起来
vim /usr/local/nginx/conf/vhost/test.com.conf
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ ?//location后面的*是忽略大小写{ ???expires 7d; ???valid_referers none blocked server_names ?*.test.com ; ???//白名单 ???if ($invalid_referer) { ???????return 403; ???} ???access_log off;}
/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reloadecho ‘121332132‘ >> /data/wwwroot/test.com/2.jpg curl -x127.0.0.1:80 test.com/2.jpg -I curl -e "http://www.baidu.com" -x127.0.0.1:80 test.com/2.jpg -I

二、Nginx访问控制

  • 需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:
vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/{ ???allow 192.168.127.1; ???allow 127.0.0.1; ???deny all;}
mkdir /data/wwwroot/test.com/admin/ echo “test,test”>/data/wwwroot/test.com/admin/1.html/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reload curl -x127.0.0.1:80 test.com/admin/1.html -I curl -x192.168.1.111:80 test.com/admin/1.html -I
  • 之前添加了一个ens37虚拟网卡,现在将它改为主机模式,获取主机ip,用于测试。
  • 匹配正则限制
location ~ .*(upload|image)/.*\.php${ ???????deny all;}
mkdir /data/wwwroot/test.com/uploadecho 12321 > /data/wwwroot/test.com/upload/1.phpcurl -x127.0.0.1:80 test.com/upload/1.php echo 12321 > /data/wwwroot/test.com/upload/1.txtcurl -x127.0.0.1:80 test.com/upload/1.txt
  • 根据user_agent限制
if ($http_user_agent ~ ‘Spider/3.0|YoudaoBot|Tomato‘){ ?????return 403;}


deny all和return 403效果一样

curl -x127.0.0.1:80 test.com -Icurl -A ‘Tomato‘ -x127.0.0.1:80 test.com -I

三、Nginx解析php的配置

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; ???}
  • fastcgi_pass 用来指定php-fpm监听的地址或者socket
  • 指定监听地址时 fastcgi_pass ?127.0.0.1 这种格式

vim /data/wwwroot/test.com/example.php

写入

<?phpphpinfo();
curl -x127.0.0.1:80 test.com/example.php

四、Nginx代理

cd /usr/local/nginx/conf/vhostdig ??www.baidu.com ???//查找一个网站的ip


如果dig命令不存在

yum ??install ??-y ??bind* 
curl -x127.0.0.1:80 www.baidu.com/robots.txt
vim proxy.conf //加入如下内容server{ ???listen 80; ???server_name www.baidu.com; ???location / ???{ ???????proxy_pass ?????http://61.135.169.121/; ???????proxy_set_header Host ??$host; ???????proxy_set_header X-Real-IP ?????$remote_addr; ???????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ???}}
/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reloadcurl -x127.0.0.1:80 www.baidu.com/robots.txt

49.Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理

原文地址:http://blog.51cto.com/13569831/2108378

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved