Proxy pass sub directory to different domain


I built an api for an invoice app thats not live yet. The app was to sit on and the api on And API calls were to be made with Javascript. Javascript has the

The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number[1] – to access each other’s DOM with no specific restrictions, but prevents access to DOM on different sites

Basically what that means is you App cant talk to your API unless they are on the same domain. So the solution if you are using nginx is below ( Proxy Pass ):

location /api {
    rewrite /api(.*) /$1 break;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

So now instead of a call to you make a call to

About the author

Talifhani Luvhengo

A collection of particles named Tali Luvhengo

1 comment

  • If you’re using modern browsers, can also use CORS, but we had a few issues with cross origin requests (damn you IE, especially you IE9!), so eventually ditched CORS and basically did what you’re doing

By Talifhani Luvhengo


Page optimized by WP Minify WordPress Plugin