Nginx配置解决跨域问题
server{
listen 8080;
server_name localhost;
location / {
root /test;
index index.html index.htm;
set $allow_cors 0;
# 判断不为空
if ($http_origin) {
set $allow_cors 1;
}
# 判断不在白名单内
if ($http_origin !~* "(10.10.10.10|www.abc.com|11.11.11.11)" ) {
set $allow_cors "${allow_cors}1";
}
# 判断不为空 且 不在白名单内,返回403
if ($allow_cors = "11") {
return 403;
}
}
location /api {
proxy_pass http://10.123.123.123:1200/api;
client_max_body_size 30m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $allow_cors 0;
# 判断不为空
if ($http_origin) {
set $allow_cors 1;
}
# 判断不在白名单内
if ($http_origin !~* "(10.10.10.10|www.abc.com|11.11.11.11)" ) {
set $allow_cors "${allow_cors}1";
}
# 判断不为空 且 不在白名单内,返回403
if ($allow_cors = "11") {
return 403;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
listen 8080;
server_name localhost;
location / {
root /test;
index index.html index.htm;
set $allow_cors 0;
# 判断不为空
if ($http_origin) {
set $allow_cors 1;
}
# 判断不在白名单内
if ($http_origin !~* "(10.10.10.10|www.abc.com|11.11.11.11)" ) {
set $allow_cors "${allow_cors}1";
}
# 判断不为空 且 不在白名单内,返回403
if ($allow_cors = "11") {
return 403;
}
}
location /api {
proxy_pass http://10.123.123.123:1200/api;
client_max_body_size 30m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $allow_cors 0;
# 判断不为空
if ($http_origin) {
set $allow_cors 1;
}
# 判断不在白名单内
if ($http_origin !~* "(10.10.10.10|www.abc.com|11.11.11.11)" ) {
set $allow_cors "${allow_cors}1";
}
# 判断不为空 且 不在白名单内,返回403
if ($allow_cors = "11") {
return 403;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}