2014-09-19 92 views
2

我正在一個網站上有不同大小的相同圖像的副本,例如Nginx發送丟失圖像的請求到後端

/images/200x100/someimage.jpg

/images/400x200/someimage.jpg

上的圖像由Nginx的,只有PHP請求直接送達獲取傳遞到F​​astCGI 。

如果找不到圖像,我想將請求傳遞給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這個工作很好(我複製後端塊和圖像塊到其他的conf文件) - 唯一的區別是,其工作方式之一是使用FastCGI的替代阿帕奇。

+0

嘗試添加在'改寫最後的關鍵字@後端「,然後刪除這些行。這應該在image.php上重新創建一個新的位置,通過'location〜\ .php $' – regilero 2014-09-19 19:39:58

+0

獲得。「謝謝,但我想我早些時候在工作時嘗試過。我今天晚上在不同的虛擬機上(我的筆記本電腦上)一直在研究這個問題,並且它完美地工作。這兩個conf文件唯一的區別是,工作的代理是代理回到Apache而不是fastcgi – John 2014-09-19 23:11:08

+0

我認爲只有在文件存在的情況下才會使用緩存頭,並且如果try_files使用了後端,那麼這將會打破位置塊放入後端塊。這是不正確的? – John 2014-09-19 23:25:06

回答

3

問題只是一個錯字。

我試圖改寫爲/image.php它應該是/images.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; 
}