Reputation: 9
I have 2 services backend and frontend (nodejs) in docker that processed via nginx (also in docker).
Nginx config:
server {
listen 80;
listen 443 http2;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
server_name example.com;
location /backend/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend-admin:2082/;
}
location ^~ / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://frontend-admin:8080;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|ttf|svg|js)$ {
expires 2d;
add_header Cache-Control public;
}
}
I use nginx location /backend/
in order to proxy all request to example.com/backend/...
to example.com:2082/...
that nodejs listening.
Main problem is my static files from proxy_pass backend-admin:2082
nginx doesn't want to process.
I have path for uploaded images in my backend service /uploads/events/1.jpg
if I open it like http://example.com:2082/uploads/events/1.jpg
it works. But via nginx it doesn't http://example.com/backend/uploads/events/1.jpg
. I think here nginx event doesn't try to reach image via proxy_pass.
Any ideas?
Upvotes: 1
Views: 716
Reputation: 5537
The reqular expression for the static files takes precedence over the /backend/
as it's longer match. The location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|ttf|svg|js)$
means, match any query that ends with the listed suffixes. The .+
is the greedy part of the regex which will match anything that is written before the suffix is reached. Therefore anything that has the suffix from the list will be mathced as a longest match and sent to read from the local files, instead of sending the request to the '/backend/'.
There are multiple ways to fix this problem, depending on what you actually want to achieve. One approach is to add another location, just for the static files that are served from the container as follows:
location ~* ^/backend/.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|ttf|svg|js)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
rewrite ^/backend/(.*) /backend/$1 break;
proxy_pass http://backend-admin:2082;
expires 2d;
add_header Cache-Control public;
}
This fix will allow you to still have local files cached, but divert all other files that have /backend/
prefix to the container.
To better understand how the matching is done, you can use some of the online matching simulators. Here is the one I use Nginx location match tester
Upvotes: 1