Detailed explanation of the process of building an image server with nginx (the difference between root and alias)

Detailed explanation of the process of building an image server with nginx (the difference between root and alias)

The installation process is omitted (I installed it directly using yum -y install nginx; ).

start up

Start (restart) nginx, the following two commands are OK:

systemctl restart nginx;
# Note: nginx here is not a directory, it is /usr/sbin/nginx -s reload;

Generally, if there is no error, it means the startup is successful.

Verify whether nginx is started on the page

Enter the IP address in the browser and return to the CentOS page. Is this wrong?
Look at the configuration file:

root /usr/share/nginx/html;

The content of index.html in this directory is the content of the centos homepage, which means there is no problem and nignx is started (you can also change the title of index.html to confirm it).

Preparation

mkdir -p /data/images; # Create a directory for storing image files chmod -R 755 /data/images; # Authorize cd /data/images; 
Then use the rz command to upload a picture 01.png

vim /etc/nginx.conf, add configuration:

location /images {
 root /data;
 autoindex on;
}

Browser input:
111.222.333.444/data/images/01.png;
A picture appears, indicating success.

Using alias configuration

Of course, you can also use aliases:

location /images {
 alias /data/images; # It is said that the '/' must be added at the end, but in practice, autoindex on can be run without it;
}

The following writing has problems:

location /images {
 alias /data; 
 autoindex on; 
}

What’s the problem?
Entering /images/01.png in the address bar will not get it.
Because /images will be mapped to /data,
But the actual directory is /data/images/01.png,
So there is one less image.

Misconfiguration

In reality, it may not be that smooth and there will be many pitfalls.

Incorrect configuration example 1 (root)

location /images {
 root /data/images;
 autoindex on;
}

Enter ip/data/images/01.png in the address bar and find that it cannot be obtained. Why?

Expected address:
/data/images/01.png
Physical Address:
/data/images/images/01.png

Found it, there is one more image,
Because root is used, address = root + location

other

The difference between root and alias

root alias
address root + location If no match is found, alias + location
If a match is found, the part of alias that matches locaiton will be replaced

There are other differences. It is said that the alias path must be followed by / , but this has not been verified.

What does autoindex on do?

autoindex Set up directory browsing.
on: The file list will be displayed when the address bar goes to images
off: The full path of the file is required, and the page that only goes to the directory will prompt 403 forbidden

location /images {
 	alias /data;
 	# Directory browsing function, on: the address bar will display the file list when it reaches images off: the full path of the file is required, and it will only prompt 403 forbidden when it reaches the directory page
 autoindex on; 
}

ps: The difference between root and alias in nginx configuration

For example: when accessing the directory http://127.0.0.1/download/*, let it go to the directory /opt/app/code to find it.

Method 1 (using the root keyword):

location / {
root /usr/share/nginx
}
location /download {
gzip_static off;
tcp_nopush off;
root /opt/app/code;
}

Result: When accessing, he went to the directory /opt/app/code/download/ to search. That is: it will add another layer of /download directory in this directory

Method 2 (using the alias keyword):

location / {
root /usr/share/nginx
}
location /download {
gzip_static off;
tcp_nopush off;
alias /opt/app/code;
}

Result: When accessing, go directly to the /opt/app/code/ directory.

Summarize

This is the end of this article about building an image server with nginx (the difference between root and alias). For more information about building an image server with nginx, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of configuring Nginx+RTMP+HLS+HTTPFLV server in Ubuntu 18.04 to realize on-demand/live broadcast/recording functions
  • Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14
  • Nginx-rtmp realizes real-time streaming effect of live media
  • Nginx uses nginx-rtmp-module module to realize the live broadcast room function
  • Detailed steps to build nginx+rtmp live server on Mac
  • How to set up URL link in Nginx server
  • Use Nginx to build a streaming media server to realize live broadcast function
  • How to use nginx to access local static resources on Linux server
  • Nginx builds rtmp live server implementation code

<<:  Detailed explanation of Javascript string methods

>>:  Implementation of MySQL5.7 mysqldump backup and recovery

Recommend

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

How to solve the error of PyCurl under Linux

Solution to "Could not run curl-config"...

Installation and use of mysql on Ubuntu (general version)

Regardless of which version of Ubuntu, installing...

MySQL master-slave replication configuration process

Main library configuration 1. Configure mysql vim...

Example code of layim integrating right-click menu in JavaScript

Table of contents 1. Effect Demonstration 2. Impl...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

Detailed steps for installing nodejs environment and path configuration in Linux

There are two ways to install nodejs in linux. On...

Native js implementation of magnifying glass component

This article example shares the specific code for...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

CSS to achieve chat bubble effect

1. Rendering JD Effect Simulation Effect 2. Princ...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

Ubuntu installation graphics driver and cuda tutorial

Table of contents 1. Uninstall the original drive...