![](/img/trans.png)
[英]how to make NGINX serve static content like .js, .css, .html?
[英]Nginx serve images from multiple static directories under one alias
我正在從apache過渡到nginx,並且我無法獲取別名來提供圖像。 閱讀nginx文檔,在Google潛水以及對我沒有產生任何可接受結果的東西;(
圖片位於C:/xampp/htdocs/content/XYZ/thumbn/image.jpg
XYZ
部分是動態變化的。
Nginx設置為從root C:/nginx/public_html/domain.dev/
提供內容
在apache中,我只是將Alias /CDN "C:\\xampp\\htdocs\\content"
這樣,當我www.domain.dev/CDN/XYZ/thumbn/image.jpg
請求www.domain.dev/CDN/XYZ/thumbn/image.jpg
我都會從別名目錄中獲取圖像。
所以我試圖在nginx中復制相同的內容
location /CDN {
alias C:/xampp/htdocs/content;
autoindex on; # just there to see if it works.
}
不幸的是,當我訪問圖像www.domain.dev/CDN/XYZ/thumbn/image.jpg
我得到404。在Nginx日志中,我看到以下記錄:
2017/07/04 16:43:32 [error] 10532#6488: *4 CreateFile() "C:/nginx/public_html/domain.dev/CDN/XYZ/thumbn/two.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: www.domain.dev, request: "GET /CDN/XYZ/thumbn/two.jpg HTTP/1.1", host: "www.domain.dev"
它是列出目錄內容的方式。
現在,如果我將位置更改為
location ^~ /CDN/ {
alias C:/xampp/htdocs/content/;
}
我在日志中遇到了相同的錯誤,只有我設法使其正常工作(但不是我需要的方式)是使用此位置塊時
location ^~ /CDN/ {
alias C:/xampp/htdocs/content/XYZ/thumbn/;
}
然后它顯示圖像,但是圖像的URL不同,並且對我的項目無效。
現在的問題是:如何僅以相同的別名提供來自多個靜態目錄的圖像? 圖像位於以下結構中
C:/xampp/htdocs/content/one/thumbn/image.jpg
C:/xampp/htdocs/content/two/thumbn/cats.jpg
C:/xampp/htdocs/content/three/thumbn/dogs.jpg
C:/xampp/htdocs/content/another_random_direcotry/thumbn/random.jpg
並應按如下所示通過URL訪問它們
www.domain.dev/CDN/one/thumbn/image.jpg
www.domain.dev/CDN/two/thumbn/cats.jpg
www.domain.dev/CDN/three/thumbn/dogs.jpg
www.domain.dev/CDN/another_random_direcotry/thumbn/random.jpg
Nginx甚至有可能嗎? 那么我該如何實現呢? 在這種情況下需要使用正則表達式嗎?
完整的配置文件在這里
嘗試這個
location /CDN/ {
alias C:/xampp/htdocs/content/;
}
問題是由
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d;
}
當我注釋掉該位置指令時,我可以從別名目錄訪問圖像。
這將如何刪除圖像的到期標頭,並且啟用此功能后,我將無法訪問圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.