user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #默认加载的配置 include /etc/nginx/conf.d/*.conf; }
时间: 2024-03-23 20:42:16 浏览: 480
这份nginx配置文件中,并没有添加任何关于跨域请求的配置。所以,如果您想要开启nginx服务器支持跨域请求,需要在http或者server节中添加如下配置:
```
http {
...
# 设置跨域请求的响应头信息
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers "Authorization, Content-Type";
...
}
```
或者:
```
server {
...
# 设置跨域请求的响应头信息
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers "Authorization, Content-Type";
...
}
```
以上配置将允许来自任何域名的请求访问资源,并允许使用 GET、POST、PUT、DELETE、OPTIONS 方法以及 Authorization、Content-Type 头信息,如果您需要更加严格的配置,可以根据实际情况设置。
配置完成后,保存配置文件并重新加载nginx,就可以解决跨域请求的问题了。
阅读全文