zoukankan      html  css  js  c++  java
  • nginx*解决跨域

    /usr/local/nginx/conf/nginx.conf

    # 代理接口
    location ^~ /api/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers Origin,X-Requested-Width,Content-Type,Accept;
        proxy_set_header referer "http://shanghaimon.aegis-info.com";
        proxy_set_header host "shanghaimon.aegis-info.com";
        proxy_pass http://shanghaimon.aegis-info.com/api/;
    }

    location ^~ /qyapi/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers Origin,X-Requested-Width,Content-Type,Accept;
        proxy_set_header referer "https://qyapi.weixin.qq.com";
        proxy_set_header host "qyapi.weixin.qq.com";
        proxy_pass https://qyapi.weixin.qq.com/;
    }

    然后刷新配置

    /usr/local/nginx/sbin/nginx -s reload

    疑难杂症
    如果遇到
    'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed等等错误
    contains multiple values "*" 意思就是设置了2次跨域,但是只有一个是允许的。
    改动办法又很多,比如服务器直接允许option请求,服务器就直接可以被跨域访问了
    或者移除其中的任意一个就好了。如果服务器代码设置了允许跨域,使用Nginx代理里面就不需要了,或者移除服务器中的允许跨域

    location ^~ /country/ {
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            return 200;
        }
        proxy_set_header referer "http://114.116.25.112:18116";
        proxy_set_header host "http://114.116.25.112";
        proxy_pass http://114.116.25.112:18116/;
    }
  • 相关阅读:
    从客户端中检测到有潜在危险的 Request.Form 值
    SqlBulkCopy(批量复制)使用方法 && SqlDataAdapter Update
    SQL Server 2014 清理日志
    GitHub for Windows 2.0使用教程
    C#.Net使用正则表达式抓取百度百家文章列表
    c#设计模式-单例模式【转】
    抽象类(abstract)【转】
    C# 与Java初始化顺序及异同(转)
    [转]VisualSVN错误 Cannot query proxy blanket解决办法
    [转]TortoiseSVN客户端重新设置用户名和密码
  • 原文地址:https://www.cnblogs.com/dshvv/p/13729996.html
Copyright © 2011-2022 走看看