欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > nginx 配置允许跨域

nginx 配置允许跨域

2025/5/26 8:05:41 来源:https://blog.csdn.net/weixin_70367240/article/details/141342520  浏览:    关键词:nginx 配置允许跨域

当前端页面和后端应用的协议、IP、端口有任意一项不一样时,就会出现跨域问题,对于这种情况我们可以在后端应用前加一个nginx ,在server{location/{ …}}中配置可以跨域

完整配置文件如下:

worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       8090;server_name  localhost;location / {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';proxy_pass http://10.18.35.246:30001;if ($request_method = OPTIONS){add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Max-Age' 1728000;add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';proxy_pass http://10.18.35.246:30001;return 200;}}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}server {listen       8091;server_name  localhost;location / {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';proxy_pass http://10.18.35.246:31001;if ($request_method = OPTIONS){add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Max-Age' 1728000;add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';add_header 'Access-Control-Allow-Headers' 'reqid, nid, host, x-real-ip, x-forwarded-ip, event-type, event-id, accept, content-type';proxy_pass http://10.18.35.246:31001;return 200;}}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词