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;
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:
|
<<: Detailed explanation of JavaScript stack and copy
>>: Does the % in the newly created MySQL user include localhost?
1. Introduction People who are not used to Englis...
<br />Original source: http://www.a-xuan.cn/...
When it comes to tool-type websites, we first hav...
Win2008 R2 zip format mysql installation and conf...
1. First, the pure HTML file must have an entry i...
Table of contents What is native JavaScript A. Ch...
This article example shares the specific code of ...
When receiving this requirement, Baidu found many...
Recorded the installation of mysql-8.0.12-winx64 ...
Preface Query optimization is not something that ...
Windows 10 1903 is the latest version of the Wind...
<br />Table is an awkward tag in XHTML, so y...
Currently, almost all large websites and applicat...
Problem 1: Baidu Map uses tiled images (the map i...
Must read before operation: Note: If you want to ...