Summary of common Nginx techniques and examples

Summary of common Nginx techniques and examples

1. Priority of multiple servers

For example, if each server block listens to port 80, www.pm.com corresponds to the server IP address, and the matching order is:

①The block where server_name is exactly the same as www.pm.com.

②The block where server_name is *.pm.com. (wildcard first)

③The block where server_name is pm.*. (wildcard after)

④server_name uses a regular expression and can match the block of www.pm.com.

⑤If no match is found, the block of listen 80 default_server; will be selected.

⑥If there is no item 5, select the first server block of the first configuration file.

When you visit the domain name www.pm.com, the above server will be given priority.

If you access it with IP at this time, the value of the Host field in the request header is 192.168.1.7, and it cannot match server_name. It will match the server below according to the rules.

2. Prohibit IP access

Prohibiting IP access can prevent others from maliciously resolving the domain name to their own IP.

Two forms:

Note: The first method must use default_server. If you need to use default_server elsewhere, you can use the second method.

3. Use include more often (write good comments)

Configuring multiple server websites on one server will make the main configuration file nginx.conf very bloated and difficult to read. In this case, you can use the include method more often to simplify the main configuration file and make it easier for operation and maintenance personnel to maintain it.

4. Use alias to protect the real directory structure of the website

When we access the image at http://www.pm.com/image/logo.png, the visitor will know the path to the image.

location /image {

root /pm_code;

}

The corresponding path is /pm_code/image/

When using alias:

location /image {

alias /pm_code;

}

At this time, the actual location of the image is under /pm_code, /image is a virtual directory, and the alias is followed by the absolute directory path.

5. Error_page configuration

Simulate 404 error

When you enter a non-existent URL, such as http://www.pm.com/jfkdjfk, it will jump to /pm_code/www/404.jpg

Simulate 502 error

Enter www.pm.com in the browser, the link will time out, and /pm_code/www/500.jpg will be displayed

NOTE: The format is different between the two. 500 You must write another location.

6.try_files configuration

A simple example:

When you enter www.pm.com in the browser, it will first look for 400.html, and if it is not there, it will then look for index.html

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:
  • 18 Nginx proxy cache configuration tips that operators must know (which ones do you know?)
  • Summary of some common configurations and techniques of Nginx
  • Sharing tips on limiting the number of connections in nginx
  • A brief introduction to some tips for optimizing Nginx servers
  • Nginx Rewrite rules and usage introduction and skills examples
  • Implementation of nginx proxy port 80 to port 443
  • Nginx forwarding based on URL parameters
  • Detailed explanation of several error handling when Nginx fails to start

<<:  React+ts realizes secondary linkage effect

>>:  How to declare a cursor in mysql

Recommend

Add unlimited fonts to your website with Google Web Fonts

For a long time, website development was hampered...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

Example code for implementing dynamic skinning with vue+element

Sometimes the theme of a project cannot satisfy e...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...

How to build your own Angular component library with DevUI

Table of contents Preface Creating a component li...

XHTML introductory tutorial: Web page Head and DTD

Although head and DTD will not be displayed on th...

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...

How to migrate mysql storage location to a new disk

1. Prepare a new disk and format it with the same...

Docker starts MySQL configuration implementation process

Table of contents Actual combat process Let's...

A small question about the execution order of SQL in MySQL

I encountered a sql problem at work today, about ...

A brief discussion on JavaScript throttling and anti-shake

Table of contents Throttling and anti-shake conce...

JavaScript flow control (loop)

Table of contents 1. for loop 2. Double for loop ...