首页 > 解决方案 > 如何允许图像但拒绝其他一切?

问题描述

文件结构为

  1. 更大的项目
    • Discord-OAuth2
      • config <--不希望任何人访问它
      • 静态 <-- 这里的图片

当我不包括底部 2 个位置规则时,我的图像会显示,但我的配置是可访问的。当我设置以下规则时(我尝试了很多变体),我的图像返回 404,我的配置返回 403。

server {
    listen 80;

    location / {
    include /etc/nginx/mime.types;
        root /var/www/html/LargerProject;
        index index.php index.html index.htm;
    }

    location ^~ /Discord-OAuth2/static/ {
        allow all;
    }

    location ^~ /Discord-OAuth2/ {
        deny all;
    }
}

我的网站上没有显示任何图像(使用 Jinja 的烧瓶服务器)我希望显示图像并且配置返回 403

标签: nginx

解决方案


每个位置都应该有明确的动作。

在 ~ 正则表达式匹配位置上,较长的路径将首先匹配。

server {
    listen 80;

    location / {
        include /etc/nginx/mime.types;
        root /var/www/html/LargerProject;
        index index.php index.html index.htm;
    }

    location ^~ /Discord-OAuth2/static/ {
        include /etc/nginx/mime.types;
        root /var/www/html/LargerProject;
    }

    location ^~ /Discord-OAuth2/ {
        deny all;
    }
}

推荐阅读