2012-04-25 5 views
11

"static.domain.com"이미지 만 제공 할 수 있도록 nginx를 설정하려고합니다. 이것이 내가 생각해 낸 것이지만, 더 효율적으로 할 수 있다는 것을 알고 있습니다. 누군가가 .htm, .php, 디렉토리 (다른 파일이 없습니까?) 파일에 액세스하려고하면 403.html을 제공하고 싶습니다. 물론, 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

간단합니다. 그 밖의 것은 필요하지 않습니까? –

+0

물론 내가 거기서 쓴 것을 실행하지 않았으므로 확실히 테스트해야합니다. 위치 ~ * \. (jpg | jpeg | gif | png ...)는 파이프로 구분 된 모든 유형과 일치합니다. 일치하지 않으면 "/"는 모든 것과 일치하므로 "/"와 일치합니다. –

+0

파이어 폭스에서 액세스하는 모든 .php 파일은 사용자가 파일을 다운로드 할 수 있도록하기 때문에 (다운로드 프롬프트 표시) 좋아요. –

관련 문제