Common Nginx configuration allows cross-domain server { listen 11111; server_name localhost; location ~ /xxx/xx { if ($request_method = 'OPTIONS') { return 204; } 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'; proxy_pass http://1.2.3.4:5678; } } Specify Access-Control-Allow-Origin as '*', which is the simplest and most violent way to allow all cross-domain access. Allow Cookies In some scenarios, cookies are required. In this case, Nginx needs to add a sentence add_header Access-Control-Allow-Credentials 'true';, but at this time, the browser will report an error, saying that when this parameter is true, allow origin cannot be set to '*'. If multiple domain names are manually specified, the browser will also prompt an error, saying that allow origin cannot be set to multiple. These are restrictions at the protocol level. Using map In Nginx, you can use map to get a custom variable. For simple usage, please refer to the official documentation. In the scenario mentioned above, you can filter the origin in the request and put the request domain name that meets the requirements into a variable. When setting allow origin, you can use this variable to implement a dynamic, multiple cross-domain domain names. An example configuration is as follows: map $http_origin $allow_origin { default ""; "~^(https?://localhost(:[0-9]+)?)" $1; "~^(https?://127.0.0.1(:[0-9]+)?)" $1; "~^(https?://172.10(.[\d]+){2}(:[0-9]+)?)" $1; "~^(https?://192.168(.[\d]+){2}(:[0-9]+)?)" $1; } server { listen 11111; server_name localhost; location ~ /xxx/xx { if ($request_method = 'OPTIONS') { return 204; } add_header Access-Control-Allow-Origin $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'; add_header Access-Control-Allow-Credentials 'true'; proxy_pass http://1.2.3.4:5678; } } Explanation: $http_origin is an internal variable of Nginx, used to get the origin in the request header $allow_origin is a customizable variable name Summarize This is the end of this article on how to use map to implement Nginx to allow multiple domain names across domains. For more relevant map implementation of Nginx to allow multiple domain names across domains, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Details on using JS array methods some, every and find
>>: Example of converting timestamp to Date in MySQL
Table of contents Precautions Necessary condition...
Table of contents Variable type and storage space...
1. Use contrasting colours. The contrast here ref...
The use of vue3 Teleport instant movement functio...
This article mainly introduces the process of imp...
Today's article mainly introduces the reload ...
In the process of writing the project page, I enc...
Linux remote deployment of MySQL database, for yo...
Recently, I used html-webapck-plugin plug-in for ...
The before/after pseudo-class is equivalent to in...
Table of contents Overview Static type checking C...
React multiple ways to get the value of the input...
Preface In Java programming, most applications ar...
Preface In this article, we will use Docker to bu...
Table of contents 1. Using Set()+Array.from() 2. ...