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

How to implement distributed transactions in MySQL XA

Table of contents Preface XA Protocol How to impl...

Best Practices Guide for MySQL Partitioned Tables

Preface: Partitioning is a table design pattern. ...

The principle and direction of JavaScript this

How to determine what this points to? ①When calle...

How to use the EXPLAIN command in SQL

In daily work, we sometimes run slow queries to r...

The difference and usage of distinct and row_number() over() in SQL

1 Introduction When we write SQL statements to op...

Nginx dynamic and static separation implementation case code analysis

Separation of static and dynamic Dynamic requests...

React-native sample code to implement the shopping cart sliding deletion effect

Basically all e-commerce projects have the functi...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

Detailed explanation of Linux copy and paste in VMware virtual machine

1. Linux under VMware Workstation: 1. Update sour...

Element Plus implements Affix

Table of contents 1. Component Introduction 2. So...

Example code of html formatting json

Without further ado, I will post the code for you...

mysql add, delete, modify and query basic statements

grammar Here is the generic SQL syntax for INSERT...

Some tips for writing high-performance HTML applications

How can you improve web page performance? Most de...