全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 1159|回复: 16
打印 上一主题 下一主题

docker,CORS跨域请求的问题

[复制链接]
跳转到指定楼层
1#
发表于 2022-9-14 10:54:39 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
用docker部署了一个github上的项目,python写的,

在自己另外一个网站嵌套访问这个docker项目的时候,一直提示CORS错误。。

在宿主机nginx配置了CORS,但是docker上的项目肯定是不起作用的。

docker中容器中又没有找到nginx配置。。

大佬们,这个到底要咋配置?
17#
 楼主| 发表于 2022-9-14 18:02:59 | 只看该作者

感谢大佬,我搞定了。。
16#
发表于 2022-9-14 16:22:32 | 只看该作者
igoogle 发表于 2022-9-14 16:01
大佬我明白你的意思了,

不反代的话,访问站点,返回头cors配置没问题,

直接看这里
  1. server {
  2.     listen 80;
  3.     listen 443 ssl http2;
  4.     server_name api.nikm.cn menu.nikm.cn;

  5.     if ($server_port !~ 443){
  6.         rewrite ^(/.*)$ https://$host$1 permanent;
  7.     }

  8.     ssl_certificate    /ssl/pem.txt;
  9.     ssl_certificate_key    /ssl/key.txt;
  10.     ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  11.     ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  12.     ssl_prefer_server_ciphers on;
  13.     ssl_session_cache shared:SSL:10m;
  14.     ssl_session_timeout 10m;
  15.     add_header Strict-Transport-Security "max-age=31536000";
  16.     error_page 497  https://$host$request_uri;

  17.     location / {
  18.         proxy_pass http://my-admin-new.my:18081;
  19.         proxy_http_version 1.1;
  20.         proxy_set_header Host $host;
  21.         proxy_set_header X-Real-IP $remote_addr;
  22.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23.         proxy_set_header Cookie $http_cookie;
  24.         proxy_set_header Upgrade $http_upgrade;
  25.         proxy_set_header Connection "upgrade";
  26.     }

  27.     if ($request_method = "OPTIONS") {
  28.         return 204;
  29.     }
  30.     add_header Access-Control-Allow-Origin * always;
  31.     add_header Access-Control-Allow-Headers * always;
  32.     add_header Access-Control-Allow-Methods * always;

  33.     access_log  /var/log/nginx/api.nikm.cn.log;
  34.     error_log  /var/log/nginx/api.nikm.cn.error.log;
  35. }
复制代码


其实就一个always
15#
发表于 2022-9-14 16:19:47 | 只看该作者
igoogle 发表于 2022-9-14 16:01
大佬我明白你的意思了,

不反代的话,访问站点,返回头cors配置没问题,

很简单,加header头的时候追加always
14#
发表于 2022-9-14 16:02:51 | 只看该作者
python 里面加 或者 nginx里加,
13#
 楼主| 发表于 2022-9-14 16:01:18 | 只看该作者
geekjam 发表于 2022-9-14 15:33
说了,最简单的先加个http试试,看到底返不返回Http头,http头都不返回还折腾啥,肯定就错了 ...

大佬我明白你的意思了,

不反代的话,访问站点,返回头cors配置没问题,
但是一反代,再访问站点,返回头就没了cors配置

这是怎么回事啊。。
12#
 楼主| 发表于 2022-9-14 15:43:05 | 只看该作者
geekjam 发表于 2022-9-14 15:33
说了,最简单的先加个http试试,看到底返不返回Http头,http头都不返回还折腾啥,肯定就错了 ...

你说的加http啥意思?直接打开反代站点看返回头吗?

我直接访问,响应头为:

Cache-Control: no-cache
Connection: keep-alive
Content-Length: 12
Date: Wed, 14 Sep 2022 07:40:48 GMT
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Server: nginx

说明cors根本没起作用。。
所以我才像是不是要在docker里面配置
11#
发表于 2022-9-14 15:33:08 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
10#
 楼主| 发表于 2022-9-14 15:26:20 | 只看该作者
geekjam 发表于 2022-9-14 14:47
肯定就配的方法方式不对,先随便加个http头看能不能加成功,http头都能加成功cors更不用说 ...

大佬帮我看下,nginx反代docker,配置cors

我的配置这样的:


  1. #PROXY-START/

  2. location ^~ /
  3. {
  4.     proxy_pass http://172.17.0.2:8080;
  5.     proxy_set_header Host $host;
  6.     proxy_set_header X-Real-IP $remote_addr;
  7.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  8.     proxy_set_header REMOTE-HOST $remote_addr;
  9. add_header Access-Control-Allow-Origin *;
  10. add_header 'Access-Control-Allow-Credentials' 'true';
  11. add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With';
  12. add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';

  13.     #Persistent connection related configuration

  14.     add_header X-Cache $upstream_cache_status;

  15.     #Set Nginx Cache
  16.    
  17.    
  18.     set $static_file8y2m6bpV 0;
  19.     if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
  20.     {
  21.             set $static_file8y2m6bpV 1;
  22.             expires 12h;
  23.         }
  24.     if ( $static_file8y2m6bpV = 0 )
  25.     {
  26.     add_header Cache-Control no-cache;
  27.     }
  28. }

  29. #PROXY-END/
复制代码


问题出在哪儿啊
9#
发表于 2022-9-14 14:47:23 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-9-21 21:54 , Processed in 0.063186 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表