Don't Ask Backend Devs to Help With Cors
Webpack proxy
This small config helps setup a proxy for projects bundled with Webpack:
{
devServer: {
proxy: {
'/api': {
target: 'https://www.mbelsky.com',
changeOrigin: true,
cookieDomainRewrite: 'localhost',
}
}
}
}
Nginx proxy
This config works for apps served by Nginx:
http {
server {
# …
server_name localhost;
location /api/ {
proxy_cookie_domain www.mbelsky.com $server_name;
proxy_http_version 1.1;
proxy_pass https://www.mbelsky.com;
proxy_ssl_server_name on;
}
location / {
# serve frontend app's static
}
}
}
For other cases setup Nginx proxy on another port, hide the original response header with proxy_hide_header
and set the Access-Control-Allow-Origin
header’s value with add_header
:
location /api/ {
# …
proxy_hide_header Access-Control-Allow-Origin;
add_header Access-Control-Allow-Origin * always;
}