微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在nginx中启用CORS

我卡住了,我不知道如何在Nginx中启用CORS?老实说,我已经找到了很多在Nginx中启用CORS的解决方案,其中一个https://enable-cors.org/server_nginx.html,但我在/etc/Nginx/Nginx.conf中添加了这些代码并重新启动了Nginx服务器.但我再次尝试了内部邮递员并跟随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;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐