Nginx配置跨域请求Access-Control-Allow-Origin
【摘要】 配置nginx.conf文件 server { listen 9800; server_name localhost; #后台接口配置 location ~ /quartz/ { proxy_pass http://192.168.X.XXX:9830; proxy_read_timeout ...
配置nginx.conf文件
server {
listen 9800;
server_name localhost;
#后台接口配置
location ~ /quartz/ {
proxy_pass http://192.168.X.XXX:9830;
proxy_read_timeout 360s;
proxy_send_timeout 360s;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Front-End-Https on;
add_header 'Access-Control-Allow-Methods' 'GET,POST';
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
}
}
location / {
root html;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
一、如何配置你的nginx.conf
添加一个server{},这是你的服务,listen参数是你要监听的端口,这个端口可以自定义,server_name localhost,这个一般就是A服务器的域名地址,记住这个地址+端口+/参数/(127.0.0.1:9800/quartz/) 是映射location,也就是请求的地址会被代理成 proxy_pass http://192.168.X.XXX:9830这个地址,A服务器就可以跨服务器请求B服务器的资源;
二、添加Access-Control-Allow-Methods请求头
add_header Front-End-Https on;
add_header 'Access-Control-Allow-Methods' 'GET,POST';
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)