欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx Deal with OPTIONS in HTTP Protocol

程序员文章站 2022-03-30 19:24:10
...
Nginx Deal with OPTIONS in HTTP Protocol

The idea is to add the OPTIONS in nginx configuration to deal with that.

location / {
      if ($request_method = OPTIONS ) {
          add_header Content-Length 0;
          add_header Content-Type text/plain;
          add_header OPTIONS, GET, HEAD, POST, PUT, DELETE;
          return 200;
      }
      try_files $uri @python_webapp;
}

if ($request_method = OPTIONS ) {
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Origin,X-Requested-With,Content-Type,Accept,Authorization';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 200;
}

Django 1.4 Deal with that
https://django.readthedocs.io/en/1.4.X/ref/contrib/csrf.html
https://github.com/adamchainz/django-cors-headers/tree/3b58d638ef08a644dc8ebfc68a7ffdf09835b839/#configuration



References:
https://*.com/questions/33548537/error-when-adding-add-header-to-nginx
https://*.com/questions/227939/handling-options-request-in-nginx
https://*.com/questions/11926908/how-to-respond-to-an-http-options-request