1

There comes socket connection error of CORS with nginx proxy in Ubuntu whereas Http request is working fine with it.

Here is my Nginx config file. This config works fine with Windows and Mac but not working with Ubuntu.

server 
{
    listen 7000 default_server;
    listen [::]:7000 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name localhost;
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
             proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://nodes;
    }
}

upstream nodes {
    # enable sticky session based on IP
    ip_hash;

    server localhost:3000;
    server localhost:3001;
    server localhost:3003;
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0
server{
        listen 7000;
        server_name www.whatever.com;
        location / {
                add_header 'Access-Control-Allow-Origin' 'http://localhost:7000' always;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin,x-auth' always;
                default_type application/json;
                proxy_pass //node;
                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;





    }


}

May be this can help and replace the address with the appropriate one.

Rajat Seeddharth
  • 197
  • 1
  • 3
  • 19
  • 1
    A new error has been generated after implementing the code suggested by you. It has solved the previous error. New Error is : "The 'Access-Control-Allow-Origin' header has a value 'http://localhost:7000' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access." Can you help me how to change supply origin? – Swati Mavani Aug 28 '18 at 11:01
  • On what port are you running your server?Is it 7000? – Rajat Seeddharth Aug 29 '18 at 06:11
  • @SwatiMavani That error message cited in your comment seems to indicate that maybe you’re testing right now by running your frontend code from a file on your local computer. You need to test it by running it on a web server instead – sideshowbarker Aug 29 '18 at 08:12
  • @sideshowbarker is right.Please try to test it by running on a web server instead. – Rajat Seeddharth Aug 29 '18 at 10:52