How to use map to allow multiple domain names to cross domains in Nginx

How to use map to allow multiple domain names to cross domains in Nginx

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:
  • Detailed explanation of the configuration and use of the map module in the Nginx server
  • Using Nginx's map command to redirect pages
  • How to use nginx to solve cross-domain access of cookies
  • Nginx solves the cross-domain problem when forwarding addresses
  • Vue packaging uses Nginx proxy to solve cross-domain problems
  • Detailed explanation of using Nginx reverse proxy to solve cross-domain problems
  • How to use nginx to solve cross-domain problems (taking flask as an example)

<<:  Details on using JS array methods some, every and find

>>:  Example of converting timestamp to Date in MySQL

Recommend

Detailed explanation of JS variable storage deep copy and shallow copy

Table of contents Variable type and storage space...

Several ways to improve the readability of web pages

1. Use contrasting colours. The contrast here ref...

Detailed explanation of how to use the vue3 Teleport instant movement function

The use of vue3 Teleport instant movement functio...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...

Explore the truth behind the reload process in Nginx

Today's article mainly introduces the reload ...

Detailed steps for remote deployment of MySQL database on Linux

Linux remote deployment of MySQL database, for yo...

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

Detailed explanation of common usage of pseudo-classes before and after in CSS3

The before/after pseudo-class is equivalent to in...

An Incomplete Guide to JavaScript Toolchain

Table of contents Overview Static type checking C...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

Implementation of Docker building Maven+Tomcat basic image

Preface In Java programming, most applications ar...

Complete steps to build a Laravel development environment using Docker

Preface In this article, we will use Docker to bu...

Seven ways to implement array deduplication in JS

Table of contents 1. Using Set()+Array.from() 2. ...