Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable CORs for all upstream server locations

Tags:

nginx

NginX Newbie.

I want to use NginX as a reverse proxy for websphere libery appserver on the same machine running on port 9080.

I want all requests to come thru NginX and all responses to enable CORs.

I got this to work but there is a lot of repetition in my nginx conf. How do I re-use CORs config across all locations?

server {
    listen 80;
    server_name $host;
    proxy_pass http://localhost:9080;

    location = / {
        [ CORs configuration ]
    }

    location /two/ {
        [ CORs configuration repeated ]
    }

    location /three/ {
        [ CORs configuration repeated again ]
    }
}
like image 384
DarVar Avatar asked Sep 08 '25 01:09

DarVar


1 Answers

You can set cors options in the server block so you don't have to repeat it for every location:

server {
    listen 80;
    server_name $host;
    proxy_pass http://localhost:9080;
    add_header 'Access-Control-Allow-Origin' '*';

location = / {...

Excerpt from the nginx documentation:

Syntax: add_header name value [always];

Default: —

Context: http, server, location, if in location

like image 94
VF_ Avatar answered Sep 10 '25 17:09

VF_