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

Implementing password box verification information based on JavaScript

This article example shares the specific code of ...

Recommend 60 paging cases and good practices

<br />Structure and hierarchy reduce complex...

HTML form submission method case study

To summarize the form submission method: 1. Use t...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

TypeScript Mapping Type Details

Table of contents 1. Mapped Types 2. Mapping Modi...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

Tutorial on installing and configuring MySql5.7 in Alibaba Cloud ECS centos6.8

The default MySQL version under the Alibaba Cloud...

Docker learning method steps to build ActiveMQ message service

Preface ActiveMQ is the most popular and powerful...

How to modify Ubuntu's source list (source list) detailed explanation

Introduction The default source of Ubuntu is not ...

CSS3 animation to achieve the effect of streamer button

In the process of learning CSS3, I found that man...

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...