Using Nginx as proxy to avoid CORS Using Nginx as proxy to avoid CORS nginx nginx

Using Nginx as proxy to avoid CORS


One way is to tell nginx to actually proxy the calls to the api, so for example if you want to call api.domain.com/users you'll instead call example.com/users and nginx will proxy the /users call to the api since it's not present locally, of course this method is harder to maintain because you'll need to do the mapping your self.

server {  server_name example.com;  root /path/to/root;  location / {    try_files $uri @proxy_to_api;  }  location @proxy_to_api {    # add whatever proxy settings you want    proxy_pass http://api.domain.com;  }}

The other way would be telling nginx to tell your client that it's ok to do calls to the api server, this way your client can access api.domain.com by it self without help from nginx

add_header 'Access-Control-Allow-Origin' "api.domain.com";