「Nginx」Nginx配置详解

参考文章

1、Nginx反向代理
2、nginx配置详解
3、Nginx服务器之负载均衡策略(6种)

1、正向代理和方向代理

正向代理
方向代理

2、指定域名允许跨域

map $http_origin $allow_cors {
    default 1;
     #以下为提供参考的正则表达式
     "~^https?://.*?\.guizhou.gov\.cn.*$" 1;
     "~^(https?://(shequ.guizhou.gov.cn)?)$" 1;
     "~https://shequ.guizhou.gov.cn" 1;
     "~*" 0;
}

在server中判断

        location /api/ {
        if ($allow_cors = 0){
            return 403;
        }
        client_max_body_size 100M;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://Gateway/;
        }

这段配置的含义是:根据请求头中的 Origin 值,如果是以 “https://.*.guizhou.gov.cn”、“https://shequ.guizhou.gov.cn” 或精确匹配 “https://shequ.guizhou.gov.cn” 开头的情况,将 $allow_cors 变量映射为 1,表示允许跨域;否则,将 $allow_cors 映射为 0,表示不允许跨域。