Cannot Add Header in nginx x socket.io Environment

Asked 2 years ago, Updated 2 years ago, 71 views

I saw the description that it would be good to rewrite Access-Control-Allow-Origin for cross-domain support, so
I have written as follows, but there is no change.

var http=require('http'),
  // Creating a Server Instance
  server = http.createServer(function(req,res){
      res.setHeader("Access-Control-Allow-Origin", "*");
      res.writeHead(200, {'Content-Type':'text/html'});
      res.end('server connected');
  }),
  io=require('socket.io') .listen(server);
  server.listen (9090); // Boot to port 9090
  ・・・
  • Currently, Access-Control-Allow-Origin has the host name of the site loading socket.io.js
  • Using nginx reverse proxy
  • nginx also specified that Access-Control-Allow-Origin should be rewritten, but it should be added instead of rewritten.
  • cross-domain error

    XML HttpRequest cannot load http://xxx.xx.xx/socket.io/?EIO=3&transport=polling&t=123456789&sid=USkrjwl.No'Access-Control-Allow-Origin' header is present on the requested resource.Origin' http://hostsite.aa.bb/' is before not allowed access.The response had HTTP status.cusd.403
    

    *Parameter values and URLs are random.There might be a spelling error because I made a handwriting...

Cross-domain error

XML HttpRequest cannot load http://xxx.xx.xx/socket.io/?EIO=3&transport=polling&t=123456789&sid=USkrjwl.No'Access-Control-Allow-Origin' header is present on the requested resource.http://hostsite.aa.bb/'

*Parameter values and URLs are random.There might be a spelling error because I made a handwriting...

If anyone knows where the problem is, please let me know.

node.js nginx websocket socket.io

2022-09-30 17:00

1 Answers

It's solved.
Failed to add/change header from Node server, so I rewritten nginx settings.
Until now, I have written in the following way.

add_header Access-Control-Allow-Origin*
proxy_set_header Access-Control-Allow-Origin*

However, I was able to specify "Access-Control-Allow-Origin" in the following way.
Is it because I use reverse proxy?

proxy_set_header Origin*

However, another error occurred when I used an asterisk (*), so I have specified the hostname for now.


2022-09-30 17:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.