


Nginx configuration cross-domain request error Access-Control-Allow-Origin * How to solve
Preface
When a 403 cross-domain error occurs no 'access-control-allow-origin' header is present on the requested resource
, you need to give nginx server configuration response header 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 in depth, just look here =-=
2. Explanation
1. access-control-allow-origin
The server is not allowed to cross domain by default. After configuring `access-control-allow-origin *` for the nginx server, it means that the server can accept all request sources (origin), that is, it accepts 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 us initiating an "application/json" type request. This involves a concept: preflight request. Please see the introduction of "preflight request" below.
3. access-control-allow-methods is to prevent the following error:
content-type is not allowed by access-control -allow-headers in preflight response.
4. Add a 204 return to options to handle the error that nginx still refuses access when sending a post request
When sending a "preflight request", the method options needs to be used, 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-domain resource sharing (cross -origin resource sharing), which was proposed to solve cross-domain requests.
The cross-domain resource sharing (cors) standard adds a new set of http header fields that allow the server to declare which origin sites have permission to access which resources. In addition, the specification requires that for those 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 ( preflight request) to learn whether the server allows the cross-domain request. After the server confirms permission, it initiates the actual http request. In the return of the preflight request, the server can also notify the client whether it needs to carry identity credentials (including cookies and http authentication related data).
In fact, requests with the content-type field of application/json are the above-mentioned post requests with certain mime types. Cors stipulates that content-type does not belong to the following mime types. , all belong to preflight requests:
application/x-www-form-urlencoded
multipart/form-data
text/plain
So the application/json request will add a "preflight" request before formal communication. This "preflight" request will bring 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 ... 省略了一些
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. That is, the following error occurs:
request header field content-type is not allowed by access-control-allow-headers in preflight response.
The above is the detailed content of Nginx configuration cross-domain request error Access-Control-Allow-Origin * How to solve. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
