我正在使用NGINX来提供静态文件。
每当一个文件不存在时,我希望NGINX能够尝试asynchronous检索该文件的nodejs后端服务。
后端服务需要3个参数: GUID ,文件的大小和扩展名 。 所有这些参数都是使用正则expression式从原始请求中检索的。
这是我目前的NGINXconfiguration文件:
server { listen 80; server_name .example.com; root /var/www; ## Serves file (matching pattern: /<size>/<MEDIA>/<file na-me><.ext>) location / { location ~* ^/(\d+x\d+)/(([\w])([\w])([\w])[-\w]+)/[^\.]+\.(\w+)$ { try_files /$3/$4/$5/$2/$1.$6 @backend/$2/$1/$6; } } ## backend service location @backend { proxy_pass http://127.0.0.1:8080; } }
但是我一直得到这个错误:
2012/01/23 11:53:31 [error] 28354#0: *1 could not find named location "@backend/ed3269d1-f9ef-4Ffc-dbea-5982969846c0/200x240/jpg", client: XXX.XXX.XXX.XXX, server: example.com, request: "GET /200x240/ed3269d1-f9ef-4Ffc-dbea-5982969846c0/my%20fil.jpg HTTP/1.1", host: "3.example.com"
任何想法如何让NGINX“代理”到后端服务的请求,而不是寻找一个文件?
如果您使用命名捕获进行捕获,则可以使用它们在指定位置重写请求:
server { listen 80; server_name .example.com; root /var/www; ## Serves file (matching pattern: /<size>/<MEDIA>/<file na-me><.ext>) location / { ## ?<name> assigns the capture to variable $name location ~* ^/(?<size>\d+x\d+)/(?<guid>([\w])([\w])([\w])[-\w]+)/[^\.]+\.(?<ext>\w+)$ { try_files /$3/$4/$5/$2/$1.$6 @backend; } } ## backend service location @backend { ## rewrite ... break; just sets $uri and doesn't perform a redirect. rewrite ^ /$guid/$size/$ext break; proxy_pass http://127.0.0.1:8080; } }