2012-04-25 33 views
11

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

任何想法如何才能妥善保护这一点?

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

回答

18

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

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

存在用于不匹配正则表达式没有语法。相反,匹配目标正则表达式并分配空白块,然后使用位置/来匹配其他任何内容。 - 从http://wiki.nginx.org/HttpCoreModule#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. 
} 

我的坏。

+0

就这么简单?没有其他需要? – 2012-04-25 04:40:19

+0

应该是,当然我会测试它,因为我没有运行我在那里写的东西。位置〜*。(jpg | jpeg | gif | png ...)匹配所有列在那里的管道类型,如果不匹配,则匹配“/”,因为“/”匹配所有内容。 – 2012-04-25 04:47:53

+0

好吧,这将不起作用,因为在FireFox中,任何访问的.php文件都允许用户下载文件(显示下载提示)。 – 2012-04-25 05:19:04