请问 Nginx 怎么添加 CORS 白名单? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
whereFly
V2EX    NGINX

请问 Nginx 怎么添加 CORS 白名单?

  •  
  •   whereFly 238 天前 2931 次点击
    这是一个创建于 238 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如 a.com 下的图片、js 文件等等,只允许 b.comc.com 调用。 其他域名调用的话就显示 404 或者 403 。

    13 条回复    2025-04-02 09:08:52 +08:00
    Chaidu
        1
    Chaidu  
       238 天前   5
    你根本不知道 CORS 是什么意思!
    Chaidu
        2
    Chaidu  
       238 天前
    以后有问题去问 AI 就行,不要发帖。问题:Nginx 防盗链
    wu00
        3
    wu00  
       238 天前
    Access-Control-Allow-Origin
    wzhaoyang
        4
    wzhaoyang  
       238 天前   1
    img 、js 不存在跨域
    zx9481
        5
    zx9481  
       238 天前   1
    location /images/ {
    valid_referers none blocked www.yourdomain.com yourdomain.com;
    if ($invalid_referer) {
    return 403; # 返回 403 Forbidden 错误
    # 或者可以重定向到特定的图片或页面
    # return 302 http://www.yourdomain.com/blocked.png;
    }
    }
    paopjian
        6
    paopjian  
       238 天前
    没啥用, 想用你的东西再不行拿反代拉, 你资源真重要就得走登录控制
    duzhuo
        7
    duzhuo  
       238 天前
    @paopjian 确实,就算登录控制之后日志也得存好
    hackroad
        8
    hackroad  
       238 天前   1
    #
    # Wide-open CORS config for nginx
    #
    location / {
    if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    #
    # Custom headers and headers various browsers *should* be OK with but aren't
    #
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    #
    # Tell client that this pre-flight info is valid for 20 days
    #
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=';
    add_header 'Content-Length' 0;
    return 204;
    }
    if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    }
    lisongeee
        9
    lisongeee  
       238 天前   1
    把账号密码私钥给我,我开 ssh 上去免费给你弄
    wangtian2020
        10
    wangtian2020  
       238 天前   3
    跨域是指,接口没带 CORS 跨域返回头,你的浏览器认为自己不配访问。postman 调用接口是能拿到东西的。
    防盗链是指,NGINX 觉得对方的请求头有问题不配拿到任何东西。
    YeatsOnce
        11
    YeatsOnce  
       238 天前
    @lisongee #9 127.0.0.1:22 , root/password123 。谢谢老哥
    daimaosix
        12
    daimaosix  
       238 天前 via Android
    开 ssh 我上去看看
    EthZhang
        13
    EthZhang  
       237 天前
    跨域是针对浏览器的名词,只存在于浏览器,正如上面老哥说的,防盗链一行命令就能加,后端程序里现在也有 CORS 插件很方便就能引入。但也仅仅局限于浏览器,
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4258 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 01:01 PVG 09:01 LAX 17:01 JFK 20:01
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86