I'm attempting to set up a reverse proxy using proxy_pass in NGINX. The page loads fine but all the static assists (js, css, imgs) that use relative urls break because it prepends the host name of the parent server. Is there a way to use a reverse proxy while maintaining the proxy host name for all the static resources?
parent.conf
server {
listen 80;
server_name parent.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name parent.mydomain.com;
root /var/www/parent;
index index.html;
try_files $uri.html $uri $uri/ =404;
# error and access out
error_log /var/log/error.log;
access_log /var/log/access.log;
ssl on;
ssl_certificate /etc/ssl/myssl.pem;
ssl_certificate_key /etc/ssl/myssl-key.pem;
# Here is the important part. Any url matching
# this pattern needs to proxy_pass to child.
location ~ "^/[a-z0-9]{24}$" {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass https://child.mydomain.com;
proxy_redirect off;
}
}
child.conf
server {
listen 80;
server_name child.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name child.mydomain.com;
root /var/www/child;
ssl on;
ssl_certificate /etc/ssl/myssl.pem;
ssl_certificate_key /etc/ssl/myssl-key.pem;
error_log /var/log/child.log;
access_log /var/log/child.log;
# this is a single page app so all request
# should be routed through index.html.
location / {
try_files $uri /index.html;
}
}
Now if I hit https://parent.mydomain.com/54570f77968d6e492b0d68af
the index page from child.mydomain.com
loads fine but none of my static files do since they're trying to load under https://parent.mydomain.com
instead of where they are actually located https://child.mydomain.com/js/main.js
.
child.mydomain.com