Example of how to configure cross-domain failure repair in nginx

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take effect as follows

server {
  listen 80;
  server_name localhost;
  
  # Interface forwarding location /api/ {
   # Allow cross-domain request addresses * as a wildcard add_header 'Access-Control-Allow-Origin' '*';
   # Set the request method to cross domain add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
   # Set whether to allow cookie transmission add_header 'Access-Control-Allow-Credentials' 'true';
   # Setting the request header. Why don't you set a wildcard character here? * Because add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token' is not supported.
   # Set reverse proxy proxy_pass 127.0.0.1:8081/;
  }
 }

The nginx cross-domain configuration on the Internet is mainly the above version. However, many people just copy it and don’t really practice it. So I wrote this article to remind those who need it not to copy it blindly and learn to analyze it.

Nginx changes the following configuration to take effect

server {
  listen 80;
  server_name localhost;
  
  # Interface forwarding location /api/ {
   # Allow cross-domain request addresses * as a wildcard add_header 'Access-Control-Allow-Origin' '*';
   # Set the request method to cross domain add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
   # Set whether to allow cookie transmission add_header 'Access-Control-Allow-Credentials' 'true';
   # Setting the request header. Why don't you set a wildcard character here? * Because add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token' is not supported.
   
   # Set options request processing if ( $request_method = 'OPTIONS' ) { 
    return 200;
   }
   # Set reverse proxy proxy_pass 127.0.0.1:8081/;
  }
 }

The main difference between the two codes is the following line of code

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

Because the browser will send an options pre-check request for the post request, which mainly sends the request header to the server. If the server allows it, it will send the real post request. Therefore, f12 shows that post often sends two requests. Because the backend Java code does not process the options request, the options interface request reports 403 forbidden. Here, nginx directly returns 200 to the options request, without reaching the interface layer, and directly allows the post response header, which can make the above invalid configuration effective.

A little knowledge point

proxy_pass 127.0.0.1:8081/;

Regarding the issue of whether to add or not in the reverse proxy;

Visit http://localhost/api/user/login;

  • If you add /, the actual access is 127.0.0.1:8081/user/login;
  • Without /, the actual access is 127.0.0.1:8081/api/user/login;

Adding a slash means that all /api requests will be forwarded to the root directory, that is, /api will be replaced by /. At this time, the interface path changes, with one less layer of /api. What about without the slash? This means that the /api path will not be lost when forwarding to the domain name 127.0.0.1:8081.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Explanation of the configuration method for processing AJAX cross-domain requests in Nginx server
  • Detailed configuration of Nginx to use font files across domains
  • How to configure Nginx to use font files across domains
  • Nginx server configuration solves the cross-domain problem of ajax
  • The nginx server solves the cross-domain problem of API through configuration
  • Detailed explanation of Nginx reverse proxy cross-domain basic configuration and common misunderstandings
  • Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation
  • Nginx cross-domain access scenario configuration and anti-hotlinking details
  • How to implement cross-domain API proxy forwarding through Nginx proxy forwarding configuration

<<:  Detailed explanation of JavaScript stack and copy

>>:  Does the % in the newly created MySQL user include localhost?

Recommend

Detailed analysis and testing of SSD performance issues in MySQL servers

【question】 We have an HP server. When the SSD wri...

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Alibaba Cloud Server Linux System Builds Tomcat to Deploy Web Project

I divide the whole process into four steps: Downl...

How to draw a mind map in a mini program

Table of contents What is a mind map? How to draw...

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...

You may not know these things about Mysql auto-increment id

Introduction: When using MySQL to create a table,...

How to implement scheduled backup of MySQL database

1. Create a shell script vim backupdb.sh Create t...

Details of function nesting and closures in js

Table of contents 1. Scope 2. Function return val...