为所有上游服务器位置启用CORs



NginX新手。

我想使用NginX作为websphereliberatiappserver的反向代理在同一台机器上运行端口9080

我希望所有的请求都通过NginX和所有的响应来启用CORs。

我得到了这个工作,但在我的nginx配置中有很多重复。我如何在所有位置重用CORs配置?

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 ]
    }
}

你可以在服务器块中设置cors选项,这样你就不必在每个位置都重复了:

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

节选自nginx文档:

语法:add_header name value [always];

默认值:-

上下文:http, server, location, if in location

最新更新