nginx如何配置跨域

分享 未结 3 1731
Mark
Mark 管理员 2018-03-23
悬赏:20积分
之前经常有人问我,怎么解决跨域问题,主要是利用CORS解决跨域,nginx的配置如下 [pre] server { listen 80; server_name demo.open.renren.io; location /renren-fastplus { if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,token'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Expose-Headers' '*'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Expose-Headers' '*'; } proxy_pass http://124.232.163.3:8080; client_max_body_size 1024m; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_redirect off; } } [/pre]
回帖
  • Mark
    2018-03-23
    renren-fast无需配置,项目已支持跨域
    0 回复
  • @Mark security呢 我想把admin的界面换成element-ui
    0 回复
  • Mark
    2018-05-16
    @小小的大雄 配置nginx吧
    0 回复