gpt4 book ai didi

nginx - 如何在 nginx 中启用 CORS

转载 作者:行者123 更新时间:2023-12-01 03:13:09 25 4
gpt4 key购买 nike

我被卡住了,我不知道如何启用 CORSnginx ?老实说,我找到了很多解决方案来启用 CORS在 nginx 中,其中之一是 https://enable-cors.org/server_nginx.html但我已将这些代码添加到我的 /etc/nginx/nginx.conf 中并重新启动 nginx服务器。但是我再次在 postman 内部尝试过,并且出现以下错误 nginx .

<html>
<head>
<title>405 Not Allowed</title>
</head>
<body bgcolor="white">
<center>
<h1>405 Not Allowed</h1>
</center>
<hr>
<center>nginx/1.12.1</center>
</body>
</html>

请让我知道如何修复它。谢谢。
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/test/app/;

# Load configuration files for the default server block.
include /etc/nginx/default/*.conf;

add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Methods' 'GET, POST';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

location / {
}

最佳答案

这绝不是一个安全的解决方案......但这是我目前在我的设置中所拥有的并且正在运行。也许您可以根据需要对其进行修改。欢迎大家告诉我这是多么错误,也许我们可以为每个人找到更好的解决方案。

location / {

dav_methods PUT DELETE MKCOL COPY MOVE;

# Preflighted requestis
if ($request_method = OPTIONS) {
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD, DELETE";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}

# CORS WHITELIST EVERYTHING
# This is allowing everything because I am running
# locally so there should be no security issues.
if ($request_method = (GET|POST|OPTIONS|HEAD|DELETE)) {
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
}

try_files $uri $uri/ /index.php$is_args$args;
}

关于nginx - 如何在 nginx 中启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com