2014-09-19 4 views
2

나는 같은 크기의 이미지가있는 웹 사이트에서 작업하고 있습니다.Nginx가 누락 된 이미지 요청을 백엔드에 전송합니다.

/images/200x100/someimage.jpg

등 /images/400x200/someimage.jpg

이미지는 Nginx에 만 PHP 요청에 의해 직접 제공되는이 FastCGI를 전달받을 .

이미지를 찾을 수없는 경우 올바른 크기의 이미지 버전을 생성 한 다음 반환 할 수 있도록 요청을 fastcgi에 전달하고 싶습니다.

그냥 작동하지 않을 수 있습니다. 이미지가 누락되면 원하는 스크립트 (그냥 404를 반환하는 대신)를 호출 할 수 있지만 PHP 스크립트의 소스를 반환합니다.

이 내 conf의 파일의 관련 부분입니다 :

location ~ (^|/)\. { 
       return 404; 
    } 

    location /imgs { 
      location ~ \.php$ {return 403;} 
    } 

    #Static Contents 
    location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ { 
     try_files $uri /$uri @backend; 
     add_header Pragma "public"; 
     add_header Cache-Control "public"; 
     expires  1y; 
     access_log off; 
     log_not_found off; 
    } 

    # Static Contents 
    location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ { 
     add_header Pragma "public"; 
     add_header Cache-Control "public"; 
     expires  1y; 
     access_log off; 
     log_not_found off; 
    } 

    location/{ 
       # Check if a file exists, or route it to index.php. 
       try_files $uri $uri/ /index.php?$query_string; 
    } 

    location ~\.php$ { 
     try_files $uri =404; 
       fastcgi_pass unix:/dev/shm/apache-php.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       include /etc/nginx/fastcgi_params; 
    } 

    location @backend { 
     #return 410; 
     rewrite ^(.*)$ /image.php?url=$1; 
     fastcgi_pass unix:/dev/shm/apache-php.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include /etc/nginx/fastcgi_params; 
    } 

누락 된 이미지는 다음 location @backend에 통과 할 것이다 그러나 나는 스크립트에 $의 URI를 전달할 수 없습니다.

나는 모든 종류의 변형을 겪어 왔으며 나는 무엇을 잘못 했는가? 어떤 제안이라도 감사하게 생각합니다! 감사.

내가 완벽하게 다른 VM에서이 작업을 가지고

업데이트 (I 복사 백엔드 블록과 다른 conf의 파일에 이미지 블록) - 유일한 차이점 작동 한 대신 FastCGI를의 아파치를 사용하고 있다는 점이다 .

+0

시도가'의 재 작성의 마지막 키워드를 추가 @ 백엔드 (backend) '하고 그 이후의 행을 삭제하십시오. 이것은'location ~ \ .php $'에 잡힌 image.php의 새로운 위치로 전체 프로세스를 재실행해야합니다. – regilero

+0

고마워요,하지만 일찍 일했을 때 그걸 시도한 것 같아요. 나는 오늘 저녁에 다른 VM에서 (내 랩톱에서) 문제를 해결해 왔으며 완벽하게 작동합니다. 두 개의 conf 파일의 유일한 차이점은 작동하는 아파치가 fastcgi가 아닌 프록시로 다시 프록시한다는 것입니다. – John

+0

파일이 존재할 때만 캐시 헤더가 사용되며 try_files가 백엔드를 사용하면 다음과 같이됩니다. 위치 블록을 백엔드 블록에 추가합니다. 이것은 틀린가? – John

답변

3

문제는 오타였습니다.

나는 그것이 /images.php을 했어야 /image.php에 다시하려고했다

다음과 같이 위의 conf 파일의 작업 버전은 다음과 같습니다

location ~ (^|/)\. { 
      return 404; 
} 

location /imgs { 
     location ~ \.php$ {return 403;} 
} 

#Static Contents 
location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ { 
    try_files $uri /$uri @backend; 
    add_header Pragma "public"; 
    add_header Cache-Control "public"; 
    expires  1y; 
    access_log off; 
    log_not_found off; 
} 

# Static Contents 
location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ { 
    add_header Pragma "public"; 
    add_header Cache-Control "public"; 
    expires  1y; 
    access_log off; 
    log_not_found off; 
} 

location/{ 
      # Check if a file exists, or route it to index.php. 
      try_files $uri $uri/ /index.php?$query_string; 
} 

location ~\.php$ { 
    try_files $uri =404; 
    fastcgi_pass unix:/dev/shm/apache-php.sock; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include /etc/nginx/fastcgi_params; 
} 

location @backend { 

    rewrite ^(.*)$ /images.php?url=$1; 
} 
관련 문제