Nginx local directory mapping implementation code example

Nginx local directory mapping implementation code example

Sometimes you need to access some static resources on the server, such as mounting images on other devices to a local directory, and the local directory is not in the nginx root directory. At this time, you need to simply do a directory mapping to solve it. For example, if you want to access the system directory /image_data/2016/04/29/10/abc.jpg through the browser upload/2022/web/abc.jpg, you need to add a location rule under the corresponding server {} in nginx.conf. The configuration is as follows:

location /image/ {
      root /;
      rewrite ^/image/(.*)$ /image_data/$1 break;
    }

location /image/ sets the server to intercept requests containing /image/. The actual range is determined by the actual situation.

root /; sets the root directory to be used. rewrite is the real jump rule. It sets the request starting with image to jump to the root directory starting with /image_data. The following parameters are added intactly, thus realizing the directory mapping.

Using the above rules, you can easily map to multiple local directories for static access.

After saving, use ./nginx -s reload to reload the configuration file and access the specified URL.

In addition, you can also flexibly configure precise proxies, such as proxying a certain access path. For example, when we access http://ip/abc/bcd/123.ico, we map it to /web/host1/123.ico, then you can configure the following rules:

location /abc/bcd/123.ico {
  root /;
  rewrite ^/abc/bcd/123.ico$ /web/host1/123.ico break;
}

The above implements precise path proxy. Note that /web/host1 is a file system path, not a virtual directory of the web host, because the root setting above is the root directory. If root is not specified, the global root value specified in the previous server block will prevail. The scope of the attributes in each location block is only within this block.

In addition, multiple proxy paths can be accessed through the real web path or through the proxy path, so that the proxy can be set according to different business needs. Then nginx itself can realize many practical and easy-to-use functions.

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:
  • Implementation of multi-port mapping of nginx reverse proxy
  • Nginx port mapping configuration method
  • How to set directory whitelist and IP whitelist in nginx
  • Solve the problem of "Welcome to nginx on Fedora!" after installing nginx on Centos7, and there is no default.conf file in the conf.d directory
  • Find the running nginx directory in Linux system
  • Example of how to install nginx to a specified directory
  • How to redirect nginx directory path
  • Detailed explanation of Vue deployment in subdirectories or secondary directories through NGINX

<<:  MySql sets the specified user database view query permissions

>>:  MySQL login and exit command format

Recommend

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

Example code of how CSS matches multiple classes

CSS matches multiple classes The following HTML t...

Simple principles for web page layout design

This article summarizes some simple principles of...

Detailed tutorial on MySql installation and uninstallation

This article shares the tutorial of MySql install...

How to view available network interfaces in Linux

Preface The most common task after we install a L...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

How to build and deploy Node project with Docker

Table of contents What is Docker Client-side Dock...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...

Implementation of Docker CPU Limit

1. --cpu=<value> 1) Specify how much availa...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

3 different ways to clear the option options in the select tag

Method 1 Copy code The code is as follows: documen...

How to Apply for Web Design Jobs

<br />Hello everyone! It’s my honor to chat ...