I
I
id____0_02020-12-23 17:13:13
Nginx
id____0_0, 2020-12-23 17:13:13

Error from CORS No 'Access-Control-Allow-Origin' header?

At me on openserver'e on the computer - everything works. I rented a VPS, for the first time I installed not Apache, but nginx, and I constantly run into problems. Currently - error Access to XMLHttpRequest at ' darkshaper.com:778/socket.io/?EIO=3&transport=poll... ' from origin ' darkshaper.com ' has been blocked by CORS policy: No 'Access-Control- Allow-Origin' header is present on the requested resource.

As soon as I didn’t try to google, and what options I just didn’t try to insert into this finished nginx config - I didn’t succeed. I apparently did not fully understand where exactly I have a problem. Probably in my head. I will be glad to help. There is no one to even ask..

nginx code:

server {
    listen 80;
    server_name  darkshaper.com;
    charset utf-8;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.php index.html;
        if (!-e $request_filename){
                rewrite ^(.*)$ /$1.php;
        }

        #error_page 404 /404.php;
        #error_page 403 /403.php;
    }

    location ~ \.php$ {
        root   /var/www/html;
        index index.php;
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME
        $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ .(htaccess|htpasswd|ini|phps|fla|psd|log|sh|txt)$ {
        deny all;
    }

     location ~ ^/(node|socket\.io) {
        proxy_pass http://localhost:778;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    autoindex off;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


Server code:

var app = require('express')();
var cors = require('cors');
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:778');
    //
    //         // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    //
    //                 // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    //
    //                         // Set to true if you need the website to include cookies in the requests sent
    //                             // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);
    //
    //                                     // Pass to next layer of middleware
    next();
   });
app.get('/Game', function(req, res){
  res.sendFile('/var/www/html/Game/index.html');
});
var server  = require('http').Server(app);
var io      = require('socket.io')(server);
var fs      = require('fs');

server.listen(778);


Client code in browser:
//let socket = io.connect("http://darkshaper.com:778");
let socket = io.connect();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2020-12-23
@ky0

Remove the port to your sockets - and everything will work. It is completely redundant in this case. Proxy it through the same nginx, just in a separate location.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question