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
This article example shares the specific code of ...
<br />Structure and hierarchy reduce complex...
To summarize the form submission method: 1. Use t...
When the front-end requests the interface, it is ...
Table of contents 1. Mapped Types 2. Mapping Modi...
Table of contents 1. v-text (v-instruction name =...
Description Solution VMware 15 virtual machine br...
<div align="center"> <table sty...
Table of contents Preface Implementation ideas Im...
The default MySQL version under the Alibaba Cloud...
1. Dynamic Components <!DOCTYPE html> <h...
Preface ActiveMQ is the most popular and powerful...
Introduction The default source of Ubuntu is not ...
In the process of learning CSS3, I found that man...
Table of contents Concept Introduction Logical ru...