nginx-只提供图片 [英] nginx - serve only images

查看:68
本文介绍了nginx-只提供图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置nginx,以便"static.domain.com"只能投放图片.这是我想出的,但是我知道可以更有效地完成.如果有人尝试访问任何.htm,.php,目录(我还缺少其他文件),我希望提供403.html服务.当然,除了403.htm和static.htm文件.

I'm trying to setup nginx so "static.domain.com" can only serve images. This is what I have come up with, but I know it can be done more efficiently. I want to serve 403.html if someone tries to access any .htm, .php, directory (anything else I'm missing?) files. Of course, with the exception of 403.htm and static.htm files.

有什么想法可以正确保护它吗?

Any ideas how I can secure this properly?

server {
     listen          xx.xx.xx.xx:80;

     server_name     static.domain.com;

     root            /www/domain.com/httpdocs;
     index           static.htm;

     access_log      off;
     error_log       /dev/null crit;

     error_page  403  /403.html;

     # Disable access to .htaccess or any other hidden file
     location ~ /\.ht  {
        deny all;
     }

     location ~* \.php {
        deny all;
     }

     # Serve static files directly from nginx
     location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
        add_header        Cache-Control public;
        add_header        Cache-Control must-revalidate;
        expires           7d;
     }
}

推荐答案

为什么不向上移动图像然后拒绝所有图像?

Why not move the images up and then deny all?

location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
   add_header        Cache-Control public;
   add_header        Cache-Control must-revalidate;
   expires           7d;
}
location  / {
    deny all; 
}

没有不匹配正则表达式的语法.相反,匹配目标正则表达式并分配一个空块,然后使用location/匹配其他任何内容. -从 http://wiki.nginx.org/HttpCoreModule#location

从位置/"中删除了"= 引用文档:

Removed "=" from "location /" To quote the docs:

location  = / {
  # matches the query / *only.* 
}
location  / {
  # matches *any query*, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be
  # matched first.
}

我的坏.

这篇关于nginx-只提供图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆