Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation

Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation

Preface

When a 403 cross-origin error occurs No 'Access-Control-Allow-Origin' header is present on the requested resource parameters:

1. Solution

You only need to configure the following parameters in the Nginx configuration file:

location / { 
 add_header Access-Control-Allow-Origin *;
 add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
 add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

 if ($request_method = 'OPTIONS') {
  return 204;
 }
}

The above configuration code can solve the problem. If you don't want to study it in depth, just read here =-=

II. Explanation

1. Access-Control-Allow-Origin

By default, servers are not allowed to cross domains. After configuring `Access-Control-Allow-Origin *` for the Nginx server, it means that the server can accept all request sources (Origin), that is, accept all cross-domain requests.

2. Access-Control-Allow-Headers is to prevent the following errors:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

This error indicates that the value of the current request Content-Type is not supported. In fact, it was caused by the "application/json" type request we initiated. There is a concept involved here: preflight request. Please see the introduction of "preflight request" below.

3. Access-Control-Allow-Methods is to prevent the following errors:

Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

4. Adding a 204 response to OPTIONS is to handle the error that Nginx still refuses access when sending a POST request

When sending a "preflight request", the method OPTIONS is required, so the server needs to allow this method.

3. Preflight request

In fact, the above configuration involves a W3C standard: CROS, the full name is Cross-origin resource sharing, which was proposed to solve cross-origin requests.

The Cross-Origin Resource Sharing (CORS) standard adds a set of HTTP header fields that allow servers to declare which origins have access to which resources. In addition, the specification requires that for HTTP request methods that may have side effects on server data (especially HTTP requests other than GET, or POST requests with certain MIME types), the browser must first use the OPTIONS method to initiate a preflight request to learn whether the server allows the cross-domain request. The actual HTTP request is initiated only after the server confirms the permission. In the response to the pre-check request, the server can also notify the client whether it needs to carry identity credentials (including Cookies and HTTP authentication related data).

In fact, the request whose Content-Type field type is application/json is the POST request with certain MIME types mentioned above. According to CORS, Content-Type that does not belong to the following MIME types is a pre-check request:

application/x-www-form-urlencoded
multipart/form-data
text/plain

Therefore, the application/json request will add a "pre-check" request before the formal communication. This "pre-check" request will carry the header information Access-Control-Request-Headers: Content-Type:

OPTIONS /api/test HTTP/1.1
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type
... some omitted

When the server responds, if the returned header information does not contain Access-Control-Allow-Headers: Content-Type, it means that non-default Content-Type is not accepted. The following error occurs:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

Reference articles:

  • Ruan Yifeng [Detailed explanation of cross-origin resource sharing CORS]
  • MDN web docs [HTTP access control (CORS)]

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Use jQuery and JSONP to easily solve the problem of cross-domain access
  • Use jsonp to perfectly solve cross-domain problems
  • jQuery $.getJSON() cross-domain request
  • 3 common methods of js cross-domain request data
  • 8 solutions for js front-end to solve cross-domain problems (latest and most complete)
  • How to use nginx to solve cross-domain access of cookies
  • Solution to invalid Nginx cross-domain setting Access-Control-Allow-Origin
  • Explanation of the configuration method for processing AJAX cross-domain requests in Nginx server
  • Detailed explanation of Java cross-domain problem handling
  • Two ways to solve cross-domain requests in java
  • How to solve cross-domain problems when debugging with vue+Java backend
  • How to use CORS to implement JavaWeb cross-domain request issues
  • Detailed example of solution to java request cross-domain problem
  • Detailed explanation of JS cross-domain (Access-Control-Allow-Origin) front-end and back-end solutions

<<:  Writing methods that should be prohibited in native JS

>>:  MySQL tutorial on how to deploy multiple instances on a single machine using mysqld_multi

Recommend

Docker installation of Nginx problems and error analysis

question: The following error occurred when insta...

How to configure Nginx to support ipv6 under Linux system

1. Check whether the existing nginx supports ipv6...

Encoding problems and solutions when mysql associates two tables

When Mysql associates two tables, an error messag...

A brief discussion on three methods of asynchronous replication in MySQL 8.0

In this experiment, we configure MySQL standard a...

Alibaba Cloud Server Ubuntu Configuration Tutorial

Since Alibaba Cloud's import of custom Ubuntu...

How to view the IP address of the Docker container

I always thought that Docker had no IP address. I...

How to modify the root password of mysql under Linux

Preface The service has been deployed on MySQL fo...

Analysis of the process of simply deploying nginx in Docker container

1. Deploy nginx service in container The centos:7...

Basic usage tutorial of MySQL slow query log

Slow query log related parameters MySQL slow quer...

Simplify complex website navigation

<br />Navigation design is one of the main t...

Implementation steps of mysql master-slave replication

Table of contents mysql master-slave replication ...

Simple implementation of mini-vue rendering

Table of contents Preface Target first step: Step...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...

HTML table mouse drag sorting function

Effect picture: 1. Import files <script src=&q...