nginx - 仅提供图像

Bri*_*ith 12 nginx

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

我有什么想法可以保证这一点吗?

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;
     }
}
Run Code Online (Sandbox Code Playgroud)

And*_*w T 19

为什么不向上移动图像然后否认所有图像?

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; 
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:从"location /"中删除"="引用文档:

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.
}
Run Code Online (Sandbox Code Playgroud)

我的错.