博客
关于我
LNMP架构(Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理)
阅读量:801 次
发布时间:2023-02-06

本文共 2201 字,大约阅读时间需要 7 分钟。

Nginx防盗链及访问控制配置详解

Nginx防盗链

防止网站资源被非法引用是每个网站管理员的重要任务之一。Nginx提供了一种简单有效的防盗链解决方案,以下是详细的配置步骤。

1. 配置防盗链

编辑虚拟主机文件/usr/local/nginx/conf/vhost/test.com.conf,在location块中添加以下配置:

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;    }    access_log off;}

2. 配置文件检查与重新加载

执行以下命令确保配置无误并重新加载:

/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reload

3. 测试防盗链

使用curl命令测试引用的效果:

curl -e "http://www.baidu.com/123.txt" -x127.0.0.1:80 -I test.com/test.jpg

若返回403 Forbidden,说明防盗链配置成功。


Nginx访问控制

###需求仅允许特定IP访问/admin/目录,防止一句话木马攻击。

###配置步骤

编辑虚拟主机文件/usr/local/nginx/conf/vhost/test.com.conf,添加以下配置:

location /admin/ {    allow 192.168.248.129; # 允许特定IP访问    allow 127.0.0.1; # 允许本地访问    deny all; # 否则拒绝访问}

注意事项

  • 配置中的allowdeny规则按顺序执行,allow优先于deny
  • 确保目标服务器的防火墙规则开放了80端口。

测试步骤

  • 在Windows机器中:

    • 配置hosts文件。
    • 打开防火墙端口:iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  • 使用浏览器访问http://test.com/admin/,若未授权IP访问将返回403 Forbidden。

  • 查看Nginx日志:cat /tmp/test.com.log,确认被拒绝访问的IP地址。


  • Nginx解析PHP配置

    1. 配置解析PHP

    编辑虚拟主机文件/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;}

    2. 配置文件检查与重新加载

    执行以下命令:

    /usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reload

    3. 测试502错误

    使用curl测试:

    curl -x127.0.0.1:80 test.com/test0816.php

    若返回502 Bad Gateway,需检查php-fpm配置文件/etc/php-fpm.conf,确保listen设置正确。


    Nginx代理配置

    1. 创建代理配置文件

    进入/usr/local/nginx/conf/vhost/目录,创建新文件proxy.conf

    server {    listen 80;    server_name ask.apelearn.com;        location / {        proxy_pass http://223.94.95.10/;        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }}

    2. 配置文件检查与重新加载

    执行以下命令:

    usr/local/nginx/sbin/nginx -tusr/local/nginx/sbin/nginx -s reload

    3. 测试代理效果

    使用curl命令测试:

    curl -x127.0.0.1:80 ask.apelearn.com/robots.txt

    拓展

    502问题汇总

    • 确保php-fpm服务正常运行。
    • 检查nginx错误日志:tail -f /tmp/nginx.log

    location优先级

    location块的匹配顺序至关重要,^~优先于~*.*优先于.*.(ext)

    转载地址:http://eyufk.baihongyu.com/

    你可能感兴趣的文章
    Nacos服务注册与发现demo
    查看>>
    Nacos服务注册与发现的2种实现方法!
    查看>>
    nacos服务注册和发现原理简单实现案例
    查看>>
    Nacos服务注册总流程(源码分析)
    查看>>
    nacos服务注册流程
    查看>>
    Nacos服务部署安装
    查看>>
    nacos本地可以,上服务器报错
    查看>>
    Nacos注册Dubbo(2.7.x)以及namespace配置
    查看>>
    Nacos注册中心有几种调用方式?
    查看>>
    nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
    查看>>
    nacos源码 nacos注册中心1.4.x 源码 nacos源码如何下载 nacos 客户端源码下载地址 nacos discovery下载地址(一)
    查看>>
    nacos源码 nacos注册中心1.4.x 源码 spring cloud alibaba 的discovery做了什么 nacos客户端是如何启动的(二)
    查看>>
    nacos源码 nacos注册中心1.4.x 源码 如何注册服务 发送请求,nacos clinet客户端心跳 nacos 注册中心客户端如何发送的心跳 (三)
    查看>>
    Nacos源码分析:心跳机制、健康检查、服务发现、AP集群
    查看>>
    nacos看这一篇文章就够了
    查看>>
    Nacos简介、下载与配置持久化到Mysql
    查看>>
    Nacos简介和控制台服务安装
    查看>>
    Nacos管理界面详细介绍
    查看>>
    Nacos编译报错NacosException: endpoint is blank
    查看>>
    nacos自动刷新配置
    查看>>