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

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

7 skills that great graphic designers need to master

1》Be good at web design 2》Know how to design web p...

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...

Implementation of FIFO in Linux process communication

FIFO communication (first in first out) FIFO name...

Introduction to JavaScript strict mode use strict

Table of contents 1. Overview 1.1 What is strict ...

HTML drawing user registration page

This article shares the specific implementation c...

MySQL derived table (Derived Table) simple usage example analysis

This article uses an example to describe the simp...

Disable IE Image Toolbar

I just tried it on IE6, and it does show the toolb...

A Deep Dive into the MySQL InnoDB Storage Engine

Preface In MySQL, InnoDB belongs to the storage e...

How to set background blur with CSS

When making some pages, in order to make the page...

JavaScript to achieve fireworks effects (object-oriented)

This article shares the specific code for JavaScr...

The use and difference between vue3 watch and watchEffect

1.watch listener Introducing watch import { ref, ...

Two box models in web pages (W3C box model, IE box model)

There are two types of web page box models: 1: Sta...