扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

nginx怎么配置跨域访问

扬州沐宇科技
2023-09-28 00:52:24
nginx

要配置Nginx实现跨域访问,可以在Nginx的配置文件中添加以下内容:

  1. 编辑Nginx的配置文件,一般位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/default.conf。

  2. 在http段中添加以下内容:

http {
...
# 开启跨域请求支持
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';
...
}
  1. 保存配置文件并重启Nginx服务。

这样配置后,Nginx将会添加相应的HTTP响应头,允许其他域名的请求跨域访问。

扫码添加客服微信