Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker 向nginx反向代理服务器添加CORS响应头_Docker_Nginx_Reverse Proxy - Fatal编程技术网

Docker 向nginx反向代理服务器添加CORS响应头

Docker 向nginx反向代理服务器添加CORS响应头,docker,nginx,reverse-proxy,Docker,Nginx,Reverse Proxy,我有一个简单的本地html和一些JS,我目前通过Docker nginx在localhost:8080上通过 docker run -it --name nginx_ol -v ${PWD}:/usr/share/nginx/html:ro -p 8080:80 -d nginx:alpine JS应该从(“上游”?)服务器获取JPG,如下所示 var url=http://example.com/foo/bar.jpg 获取(url,{method:'GET',mode:'cors'})

我有一个简单的本地html和一些JS,我目前通过Docker nginx在localhost:8080上通过

docker run -it --name nginx_ol -v  ${PWD}:/usr/share/nginx/html:ro -p 8080:80 -d nginx:alpine
JS应该从(“上游”?)服务器获取JPG,如下所示

var url=http://example.com/foo/bar.jpg
获取(url,{method:'GET',mode:'cors'})
服务器未设置为发送适当的CORS标头。尝试以这种方式获取图像会导致访问控制不允许源本地主机:8080,正如预期的那样

据我所知,我必须

  • 配置nginx将资源反向代理到本地主机,同时添加必要的CORS头
  • 改为向代理位置发出请求
  • 即,在
    http://example.com/foo/bar.jpg
    http://localhost/foo/bar.jpg
    并相应地调整获取URL

    在nginx/proxy方面,我完全是绿色的,我已经阅读了为服务器添加CORS头的文档,并看到了为代理服务器添加CORS头的示例

    如果我想将请求代理到
    localhost:8080/foo
    http://example.com/foo
    ,我希望下面这样的方法来完成这项工作:

     server {
       listen 80;
       server_name localhost;
    
       add_header 'Access-Control-Allow-Origin';
       add_header ...
    
        location / { 
    
          proxy_set_header Host $host;
    
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control...
          proxy_redirect off;
          proxy_set_header host $host;
          proxy_set_header X-real-ip $remote_addr;
          proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
    
          proxy_pass http://example.com/;
        }
    }
    
    对于请求URL
    localhost:8080/foo/bar.jpg
    ,我得到了
    GET 404(未找到)
    ,它告诉我没有发生类似的情况


    很抱歉,这是一个基本的误解,但我相信像我这样努力建立和运行代理的用户会从正确方向的任何推动中受益:)

    proxy\u passhttp://dummyimage.com/foo
    ?尝试代理通行证http://dummyimage.com/;
    foo
    。请参阅关于带和不带URI后缀的代理传递行为的回答。对于混淆,非常抱歉。为了获得更好的示例语法,我决定不使用dummyimage.com/etc,但未能更改此实例。代理通行证现在是
    http://example.com/
    url相应更改,但现在仍然很幸运,即404。查看
    错误日志
    ,是否提到
    没有定义解析程序来解析…