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

Using js to implement simple switch light code

Body part: <button>Turn on/off light</bu...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...

Share CSS writing standards and order [recommended for everyone to use]

CSS writing order 1. Position attributes (positio...

Vue implements infinite loading waterfall flow

This article example shares the specific code of ...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

Automatic line breaks in html pre tags

At this time, you can use overflow:auto; (when the...

Detailed explanation of the pitfalls of nginx proxy socket.io service

Table of contents Nginx proxies two socket.io ser...

Fixed table width table-layout: fixed

In order to make the table fill the screen (the re...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

Front-end JavaScript Promise

Table of contents 1. What is Promise 2. Basic usa...

How to View All Running Processes in Linux

You can use the ps command. It can display releva...

Negative margin function introduction and usage summary

As early as in the CSS2 recommendations in 1998, t...

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...

Responsive layout summary (recommended)

Basic knowledge of responsive layout development ...