简体   繁体   中英

Need help fixing my nginx server

My console looks like this

控制台输出

This is my nginx cors setup

add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';        
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri/ =404;

              if ($request_method = 'OPTIONS') {
                 add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
                 add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
                 add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
                 add_header 'Access-Control-Allow_Credentials' 'true';
                 add_header 'Access-Control-Max-Age' 1728000;
                 add_header 'Content-Type' 'text/plain charset=UTF-8';
                 add_header 'Content-Length' 0;
                 return 204;
              }


             proxy_redirect off;
             proxy_pass http://localhost:5000;
             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;
}

my node server looks like this

var allowCrossDomain = function(req, res, next) {

  if('GET' == req.method){ 
     res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
     res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
  }

  if('POST' == req.method){ 
     res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
     res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
  }

 if('OPTIONS' == req.method){ 
   res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
   res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
   res.sendStatus(200); 
 }

next();

On my browser console I have been getting multiple headers, only one accepted. How do I fix this? I really need someone to help me out.

Access-Control-Allow-Origin header should only contain one value as the allowed origin or you can use * to allow all origins.

As @TarunLalwani commented, you are adding http://beloveddais.com as a value to the Access-Control-Allow-Origin header both in your NGINX configuration and in your code for the OPTIONS requests:

add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';

AND

res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');

Remove one of them depending on your choice of design and it should be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM