iT邦幫忙

2

nginx 無法添加 header {已解決}

  • 分享至 

  • xImage

我在做CORS的回應header,location的部份需要用到proxy_pass,但我一直沒辦法順利添加header

nginx設定檔

log_format logdata $Host _ $request_uri | $http_origin | $cors | $pos | $allow_origin _ $allow_methods _ $allow_headers ;

map $http_origin $cors {

  '~*^https?://(account\.|asset\.|)dcreater\.com$' 'true';
}
map $cors $allow_origin {

  'true' $http_origin;
}
map $cors $allow_methods {

  'true' 'GET, OPTIONS, POST';
}
map $cors $allow_headers {

  'true' 'User-Agent,Keep-Alive,Content-Type,Pragma,Cache-Control,Upgrade-Insecure-Requests';
}

server {
  listen	80;
  server_name asset.dcreater.com;
  access_log /var/log/nginx/access.log logdata;
  error_log /var/log/nginx/error.log;
  location / {
    if ($request_method = 'OPTIONS') {
      return 204;
    }
    
    resolver 127.0.0.11;
    proxy_pass http://app:8001;
	
    set $pos "71";   # for debug
    add_header "Access-Control-Allow-Origin" $allow_origin;
    add_header "Access-Control-Allow-Methods" $allow_methods;
    add_header "Access-Control-Allow-Headers" $allow_headers;
  }
}

log的回應看起來也一切正常

asset.dcreater.com_/static/js/editor.js|http://dcreater.com|true|71|http://dcreater.com_GET, OPTIONS, POST_User-Agent,Keep-Alive,Content-Type,Pragma,Cache-Control,Upgrade-Insecure-Requests

在瀏覽器的回應與請求
https://ithelp.ithome.com.tw/upload/images/20200721/20124291OTBrWDgzNl.png
https://ithelp.ithome.com.tw/upload/images/20200721/20124291h0Azo4DUif.png

感謝

linyanbin iT邦新手 5 級 ‧ 2020-07-22 03:29:02 檢舉
最後查明是後端gin的gin-cors惹的問題詳情可以看這篇https://blog.rexskz.info/bug-caused-by-gin-cors.html
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答