簡體   English   中英

設置nginx以提供靜態文件(如果存在),或者讓服務器創建它(如果不存在)

[英]Setup nginx to serve a static file if exists, or let the server create it if not

預NGINX

在我的網站中,我設置了圖像,以便您可以請求自定義尺寸。

file.jpg?s=wXh //w and h are numbers in pixels

如果file.jpg的大小為wXh,我會從S3提供服務​​。

如果file.jpd不存在該大小。 我以正確的大小創建它,將其流式傳輸到客戶端,然后將其保存到S3,以便下次存在。

現在這是NGINX的一個相當復雜的情況,我猜。

如果文件存在,如何告訴nginx從S3提供它,或者如果文件不存在,將請求轉發給我的節點服務器?

謝謝

更新:

試圖使用@Alexey Ten建議的方法,看起來很有希望,我相信這是正確的方法,但我仍然遇到麻煩。

這是我在conf文件中使用的代碼:

   # domain.com/pubstore is where we have node route to s3
   # the format of the string we use is the path on the s3 server.
   # domain.com/pubstore/folder1/folder2/file.ext will tell node to 
   # fetch the file from [bucket]/folder1/folder2/file.ext
   # location /pubstore/ {
   # proxy_pass http://bucketName.s3.amazonaws.com/;
   # proxy_intercept_errors on;
   # error_page 404 = @nodejs;
   # }


   # location @nodejs {
   # proxy_pass http://localhost:8080/pubstore/;
   #    proxy_http_version 1.1;
   #    proxy_set_header Upgrade $http_upgrade;
   #    proxy_set_header Connection 'upgrade';
   #    proxy_set_header Host $host;
   #    proxy_cache_bypass $http_upgrade;
   #    gzip_disable msie6;
   #    gzip on;
   #    gzip_vary on;
   #    gzip_types image/svg+xml image/x-icon;
   #    gzip_min_length 256;
   # }

當我嘗試執行nginx時出現此錯誤:

 Starting nginx: nginx: 
 [emerg] "proxy_pass" cannot have URI part in location given by 
 regular expression, or inside named location, or inside "if" statement,
 or inside "limit_except" block 

糾正這個的想法?

您應該將請求傳遞給S3,如果它返回404錯誤,您應該將其重新發送到節點。 像這樣的東西:

location /path/to/file/ {
    proxy_pass http://s3.domain/path/to-file/on/s3/;
    proxy_intercept_errors on;
    error_page 404 = @nodejs;
}

location @nodejs {
    proxy_pass http://node.server;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM