簡體   English   中英

需要幫助修復我的Nginx服務器

[英]Need help fixing my nginx server

我的控制台看起來像這樣

控制台輸出

這是我的nginx cors設置

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;
}

我的節點服務器看起來像這樣

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();

在我的瀏覽器控制台上,我得到了多個標頭,只有一個被接受。 我該如何解決? 我真的需要有人幫助我。

Access-Control-Allow-Origin標頭應僅包含一個值作為允許的來源,或者您可以使用*允許所有來源。

正如@TarunLalwani所評論的那樣,您要在NGINX配置和OPTIONS請求的代碼中將http://beloveddais.com作為值添加到Access-Control-Allow-Origin標頭中:

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

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

根據您對設計的選擇,刪除其中之一,應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM