Skip to content

Instantly share code, notes, and snippets.

@ryaninvents
Created February 1, 2016 16:29
Show Gist options
  • Save ryaninvents/c5698e8ade40524a4eff to your computer and use it in GitHub Desktop.
Save ryaninvents/c5698e8ade40524a4eff to your computer and use it in GitHub Desktop.
Proxying an app that doesn't want to be proxied

There's an app running at app.test.example.com that I want to make changes to, but I don't want to go through the hassle of getting the source code and getting it set up on my machine. Not when I can fake it with nginx much quicker!

app.test.example.com depends on a service svc.test.example.com. The main JS looks for the environment in the page URL; if I run locally at app.local.example.com it will declare the environment to be "local" and look for a service at svc.local.example.com.

I just have to make sure the service sees what it wants to see: a Host of svc.test.example.com and an Origin of http://app.test.example.com. I also need to keep the browser happy by blocking the Access-Control-Allow-Origin value the service is passing down and using my local one instead.

Works like a charm! Now I can use a try_files directive to selectively replace JS files from the app, and fetch them from the proxied app if they don't exist locally.

# add to the end of /etc/hosts
127.0.0.1 app.test.example.com svc.test.example.com
server {
listen 80;
server_name app.local.example.com;
location / {
proxy_pass http://app.test.example.com;
proxy_set_header Host app.test.example.com;
}
}
server {
listen 80;
server_name svc.local.example.com;
location / {
proxy_pass http://svc.test.example.com;
proxy_hide_header Access-Control-Allow-Origin;
proxy_set_header Host svc.test.example.com;
proxy_set_header Origin http://app.test.example.com;
proxy_set_header Referer http://app.test.example.com;
add_header Access-Control-Allow-Origin http://app.local.example.com;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment