I'm trying to provide different assets that are in different docker containers. I have two containers where one of these container is example.com and the other is example.com/site2 and I have another container that is a nginx container (proxy).
In this nginx container I have a config file that manage the site paths correctly to access pages, but the images, css and javascripts from site2 can not be found (404 status to all assets from site2).
This is my config file:
server {
listen 80 default_server;
server_name _;
location ~ /\. {
deny all;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;
location / {
proxy_pass http://production-site1/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /site2 {
proxy_pass http://production-site2/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I remove the following locations all images, css and javascripts are found in both sites:
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
if ($request_uri ~* "site2") {
proxy_pass "http://production-site2";
}
if ($request_uri !~* "site2") {
proxy_pass "http://production-site1";
}
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
but I want to keep these locations. There is some solution to my problem? I tried many solutions that I found at stackoverflow and other links, but none of these solutions works.