0
votes

I have a problem with customizing my nodejs app on the server (https, socket.io). I using: pm2, nginx(works on port :3000), nodejs Prev I customized nginx for :80 port(http) - works fine!. But when I inserted ssl certificate and customized him I get errors in the console:

"Access to XMLHttpRequest at 'https://example:2053/socket.io/?EIO=3&transport=polling&t=NAPoEIt' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

My nginx settings:


server {
    listen 80 default_server;
    server_name example.com www.example.com;
    return 301 http://skinsgaben.com$request_uri;
}

server {
    listen 443 ssl;

    server_name site.com www.example.com;
    ssl_certificate /var/www/site/ssl/example.crt;
    ssl_certificate_key /var/www/site/ssl/example.key;

    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:3000/;
        proxy_redirect off;

        # Socket.IO Support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

        location /socket.io/ {
                proxy_pass             http://localhost:3000/;
                proxy_redirect off;

                proxy_http_version      1.1;

                proxy_set_header        Upgrade                 $http_upgrade;
                proxy_set_header        Connection              "upgrade";

                proxy_set_header        Host                    $host;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
    }

    error_log /var/www/site-log/error.log;
    access_log /var/www/site-log/access.log;
}
1
Hey Maxim, did u also make changes on nodejs's server creation?halilcakar
No, I just customized nginxMaxim Strutinskiy
Yea you also need to change the creation of server. You can check nodejs.org/en/knowledge/HTTP/servers/…halilcakar

1 Answers

0
votes

I'm mostly using this pattern for local and remote nodejs apps.

let host = process.env.host || 'your host';
let PORT = process.env.port || 'your port';

let protocol = 'http';
let options = {};
if (APP_ENV === 'production') {
    protocol = 'https';
    options = {
        key: fs.readFileSync(SSL.KEY),
        cert: fs.readFileSync(SSL.CERT)
    };
}

const server = require(protocol).createServer(options, app);
server.listen({ host, port: PORT }, () => {
    console.log(`Server listening on `);
});