0
点赞
收藏
分享

微信扫一扫

Nginx - 一招快速解决跨域(CORS)问题


跨域场景

  1. 在访问的地址链路里面又有一个新的地址发起访问
  2. 连续两次经过跨域配置也会报跨域错误

# 允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;


# 允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';


# 允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;


# 允许请求的header
add_header 'Access-Control-Allow-Headers' *;

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

upstream api {
server XXX.XXX.XXX.XXX:18080;
server XXX.XXX.XXX.XXX:18081;
}

server {

#允许跨域请求的域,*代表所有
#add_header 'Access-Control-Allow-Origin' *;

#允许带上cookie请求
#add_header 'Access-Control-Allow-Credentials' 'true';

#允许请求的方法,比如 GET/POST/PUT/DELETE
#add_header 'Access-Control-Allow-Methods' *;

#允许请求的header
#add_header 'Access-Control-Allow-Headers' *;

listen 80;
server_name 127.0.0.1;

location /index {
alias /usr/share/nginx/html;
index index.html index.htm;
}

location / {
proxy_pass http://api;
}

#location /PJO3eZpW2z.txt {
#alias /usr/share/nginx/html/PJO3eZpW2z.txt;
#}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

举报

相关推荐

0 条评论