博客
关于我
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; # 否则拒绝访问}

注意事项

  • 配置中的allow和deny规则按顺序执行,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/

    你可能感兴趣的文章
    python+flask计算机毕业设计高校校园线上跳蚤市场的系统设计(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校疫情防控管理平台(程序+开题+论文)
    查看>>
    Python+Jenkins+Allure Report接口自动化测试持续集成
    查看>>
    python+locust电商全流程性能测试
    查看>>
    Python函数运行的可执行文件的终端输出如何以一般方式静音?
    查看>>
    Python+Pytest+Allure+Git+Jenkins接口自动化框架
    查看>>
    python+pytest接口自动化 —— 参数关联
    查看>>
    python+pytest接口自动化 —— 参数关联
    查看>>
    python+pytest接口自动化 —— 自动化用例编写思路 (使用pytest编写一个测试脚本)
    查看>>
    Python+pytest接口自动化之cookie绕过登录(保持登录状态)
    查看>>
    python+pytest接口自动化:接口测试
    查看>>
    Python+requests+unittest执行接口自动化测试详情
    查看>>
    Python+requests+unittest执行接口自动化测试详情
    查看>>
    python+requests+unittest执行自动化接口测试!
    查看>>
    Python+Requests编码识别Bug
    查看>>
    python函数编写_[零基础学python]传说中的函数编写条规
    查看>>
    Python+selenium —— UI自动化之鼠标操作
    查看>>
    python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
    查看>>
    Python+Selenium+Threading进行兼容性测试
    查看>>
    Python+selenium+unittest的GUI自动化框架实现
    查看>>