Nginx防盗链、访问监控、解析php相关配置,Nginx代理

简介:

Nginx防盗链

因为改配置也是用location板块,所以本节可结合日志管理一起配置。

[root@localhost vhost]# 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;
    access_log off;
    valid_referers none blocked server_names  *.test.com ;
    #定义referer白名单
    if ($invalid_referer) {
        return 403;
    #if函数的意思是:如果不是白名单内的域名,返回值:403
    }
    access_log off;
}
……

检测并重载配置:
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 测试

    不匹配白名单不能访问:
    [root@localhost vhost]# curl -utest:159820 -e "http://www.baidu.com/1.png" -x127.0.0.1:80 test.com/1.png -I 
    HTTP/1.1 403 Forbidden
    Server: nginx/1.8.0
    Date: Sat, 06 Jan 2018 03:27:54 GMT
    Content-Type: text/html
    Content-Length: 168
    Connection: keep-alive

    匹配白名单能访问:
    [root@localhost vhost]# curl -utest:159820 -e "http://www.test.com/1.png" -x127.0.0.1:80 test.com/1.png -I
    HTTP/1.1 200 OK
    Server: nginx/1.8.0
    Date: Sat, 06 Jan 2018 03:28:04 GMT
    Content-Type: image/png
    Content-Length: 412758
    Last-Modified: Sat, 06 Jan 2018 03:18:57 GMT
    Connection: keep-alive
    ETag: "5a504021-64c56"
    Expires: Sat, 13 Jan 2018 03:28:04 GMT
    Cache-Control: max-age=604800
    Accept-Ranges: bytes

注意: 添加规则时需注意每个中括号中的内容。

Nginx访问控制

需求:允许指定ip访问/admin/目录。

  • 配置虚拟主机配置文件

    [root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

    ......
    location /admin/
    {
    allow 192.168.159.128;
    allow 127.0.0.1;
    deny all;
    #设置IP白名单,allow为允许,deny为拒绝。匹配到之后就不继续往下匹配。
    }
    ......

    测试并重载配置文件:
    [root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    [root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin/1.txt 
    test abc 123
    [root@localhost vhost]# curl -x192.168.159.128:80 test.com/admin/1.txt 
    test abc 123

    白名单ip正常访问

    [root@localhost vhost]# curl -x192.168.100.1:80 test.com/admin/1.txt 
    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx/1.8.0</center>
    </body>
    </html>

    不在白名单ip的主机访问,返回代码403

  • 针对正则匹配
  • 上传图片的目录没有做禁止就系php的操作,导致上传的一句话木马被php解析,导致数据库被盗。按理说我们应该让能上传的目录禁止解析php的操作。

    [root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf 
    ......
    location ~ .(upload|image)/..php$
    {
    deny all;
    }
    ......

    测试并重载配置:
    [root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    [root@localhost vhost]# curl -x127.0.0.1:80 test.com/upload/1.php
    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx/1.8.0</center>
    </body>
    </html>
    #不能访问php文件,返回代码403

    [root@localhost vhost]# curl -x127.0.0.1:80 test.com/upload/1.txt
    test111
    #可以访问txt文件。

  • user_agent限制

    想禁止蜘蛛,防止被其它网站爬数据。

  • 编辑虚拟主机配置文件

    [root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf 
    if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
    #user_agent 匹配Spider/3.0|YoudaoBot|Tomato
    {
    return 403;
    }

    测试并重载配置:
    [root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    [root@localhost vhost]# curl -A"YoudaoBot" -x127.0.0.1:80 test.com/upload/1.txt -I
    HTTP/1.1 403 Forbidden
    Server: nginx/1.8.0
    Date: Sat, 06 Jan 2018 07:14:19 GMT
    Content-Type: text/html
    Content-Length: 168
    Connection: keep-alive
    #user_agent 在限制范围内时,不能访问。

    [root@localhost vhost]# curl -A"baidu" -x127.0.0.1:80 test.com/upload/1.txt -I
    HTTP/1.1 200 OK
    Server: nginx/1.8.0
    Date: Sat, 06 Jan 2018 07:18:42 GMT
    Content-Type: text/plain
    Content-Length: 8
    Last-Modified: Sat, 06 Jan 2018 06:45:41 GMT
    Connection: keep-alive
    ETag: "5a507095-8"
    Accept-Ranges: bytes
    #user_agent 不在限制范围内时,可以正常访问。

注意: user_agent是严格匹配的,如果要忽略大小写,则在~后加个星号。 if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')

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;
    }
……

在此注意两点:

1、fastcgi_pass有两种格式,但是无论使用哪种格式都有保证nginx虚拟主机中的fastcgi_pass指定的路径和/usr/local/php-fpm/etc/php-fpm.conf中listen路径格式一致,否则会报错502;(要么同为.sock,么同为地址端口)

2、fastcgi _param SCRIPT _FILENAME所在行的路径要和root路径一致!

Nginx代理

Nginx代理是一种反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

pV9wIe.png

Nginx代理是在一台代理服务器中自定义一个域名,该域名指向一个IP,然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。

  • 在/usr/local/nginx/conf/vhost/目录下新建一个proxy.conf文件。

    切换目录并创建文件:
    [root@localhost ~]# cd /usr/local/nginx/conf/vhost
    [root@localhost vhost]# vim proxy.conf
    ......
    server
    {
    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@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    修改配置之前:
    [root@localhost 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.8.0</center>
    </body>
    </html>

    无法访问ask.apelearn.com

    修改配置后:
    [root@localhost 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/
    Disallow: /install/
    Disallow: /models/
    Disallow: /crond/run/
    Disallow: /search/
    Disallow: /static/
    Disallow: /setting/
    Disallow: /system/
    Disallow: /tmp/
    Disallow: /themes/
    Disallow: /uploads/
    Disallow: /url-
    Disallow: /views/
    Disallow: /
    /ajax/

    #可以访问

本文转自 豆渣锅 51CTO博客,原文链接:http://blog.51cto.com/754599082/2058143

相关文章
|
30天前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
405 87
|
28天前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
3月前
|
存储 缓存 网络协议
阿里云特惠云服务器99元与199元配置与性能和适用场景解析:高性价比之选
2025年,阿里云长效特惠活动继续推出两款极具吸引力的特惠云服务器套餐:99元1年的经济型e实例2核2G云服务器和199元1年的通用算力型u1实例2核4G云服务器。这两款云服务器不仅价格亲民,而且性能稳定可靠,为入门级用户和普通企业级用户提供了理想的选择。本文将对这两款云服务器进行深度剖析,包括配置介绍、实例规格、使用场景、性能表现以及购买策略等方面,帮助用户更好地了解这两款云服务器,以供参考和选择。
|
1月前
|
域名解析 应用服务中间件 Shell
使用nps配置内网穿透加域名解析
使用nps配置内网穿透加域名解析
219 19
|
3月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
3月前
|
应用服务中间件 nginx
Nginx进程配置指令详解
Nginx进程配置指令主要包括:`worker_processes`设置工作进程数;`worker_cpu_affinity`绑定CPU核心;`worker_rlimit_nofile`设置最大文件描述符数量;`worker_priority`设置进程优先级;`worker_connections`设置最大连接数;`daemon`控制守护进程模式;`master_process`启用主进程模式;`pid`设置PID文件路径;`user`指定用户和组;`error_log`配置错误日志。这些指令在`nginx.conf`中配置,用于优化和控制Nginx的运行行为。
169 10
|
3月前
|
监控 负载均衡 安全
静态IP代理与动态IP代理:提升速度与保障隐私的技术解析
本文探讨了静态IP代理和动态IP代理的特性和应用场景。静态IP代理通过高质量服务提供商、网络设置优化、定期更换IP与负载均衡及性能监控提升网络访问速度;动态IP代理则通过隐藏真实IP、增强安全性、绕过封锁和提供独立IP保障用户隐私。结合实际案例与代码示例,展示了两者在不同场景下的优势,帮助用户根据需求选择合适的代理服务以实现高效、安全的网络访问。
118 1
|
4月前
|
域名解析 网络协议 Ubuntu
DHCP与DNS的配置
通过这些步骤,您可以在Linux环境下成功配置和验证DHCP和DNS服务。希望这些内容对您的学习和工作有所帮助。
347 27
|
4月前
|
Java 数据库 开发者
详细介绍SpringBoot启动流程及配置类解析原理
通过对 Spring Boot 启动流程及配置类解析原理的深入分析,我们可以看到 Spring Boot 在启动时的灵活性和可扩展性。理解这些机制不仅有助于开发者更好地使用 Spring Boot 进行应用开发,还能够在面对问题时,迅速定位和解决问题。希望本文能为您在 Spring Boot 开发过程中提供有效的指导和帮助。
176 12

推荐镜像

更多
  • DNS