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

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

JavaScript data transmission between different pages (URL parameter acquisition)

On web pages, we often encounter this situation: ...

Summary of commonly used performance test scripts for VPS servers

Here is a common one-click performance test scrip...

Beginners learn some HTML tags (2)

Related article: Beginners learn some HTML tags (1...

Problems and solutions for MYSQL5.7.17 connection failure under MAC

The problem that MYSQL5.7.17 cannot connect under...

Basic usage examples of listeners in Vue

Table of contents Preface 1. Basic usage of liste...

WeChat applet development chapter: pitfall record

Recently, I participated in the development of th...

Eclipse configures Tomcat and Tomcat has invalid port solution

Table of contents 1. Eclipse configures Tomcat 2....

Summary of HTML horizontal and vertical centering issues

I have encountered many centering problems recent...

MySQL index principle and usage example analysis

This article uses examples to illustrate the prin...