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

Enable CORs for all upstream server locations

NginX 新手。

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

我希望所有请求都通过 NginX 以及所有响应来启用 COR。

我成功了,但是我的 nginx conf 中有很多重复项。我如何在所有位置重复使用 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 文档:

Syntax: add_header name value [always];

Default: —

Context: http, server, location, if in location