2012-04-25 48 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