Nginx configuration file detailed explanation and optimization suggestions guide

Nginx configuration file detailed explanation and optimization suggestions guide

1. Overview

Today I will explain the Nginx configuration file in detail and give some configuration suggestions, hoping it will be helpful to everyone.

2. nginx.conf

1) Configuration file location

In the conf folder of the nginx installation directory, for example: /usr/local/nginx/conf/nginx.conf.

You can also place the configuration file anywhere and specify the location of the configuration file when starting Nginx, for example: ./nginx -c /home/nginx.conf

2) worker_processes

Set the number of workers. Nginx's process model uses the master and worker mode. One master is responsible for coordination, and multiple workers are responsible for interacting with the client.

Just set it to auto here.

3) events

Set the model to use and the number of connections per worker.

The model in the Linux operating system recommends using epoll.

The number of worker connections is usually set to 10240. Of course, if the hardware resources are very good, you can increase it appropriately.

4) include

Include the contents of another file, placing the contents of the other file at the tag.

You can write multiple includes for multiple files, or use wildcards to match multiple files.

5) sendfile and tcp_nopush

sendfile: Set whether efficient file transfer is enabled, enabled by default.

tcp_nopush: It is only effective when the sendfile value is on. When tcp_nopush is set to on, it means that the data packets will be sent when they reach a certain size, which helps to improve the efficiency of file transfer.

It is recommended to set both to on.

6) keepalive_timeout

The timeout period for the client to connect to the server. Before the connection is disconnected, the client interacts with the server again and can reuse this connection. There is no need to re-establish a connection, which reduces resource overhead.

If it is set to 0, it means disconnecting immediately after the interaction. This value can remain the default.

7) gzip

If set to on, the data will be compressed before transmission, which will increase transmission efficiency and save bandwidth, but will affect the performance of the server CPU.

To enable this configuration, you also need to configure some additional properties.

Here you can weigh whether to save bandwidth or improve CPU performance. It is recommended to enable it and configure it according to actual conditions.

Copy the code as follows:
gzip on;gzip_min_length 512; # Minimum compression limit, in bytes. If it is less than this value, it will not be compressed. gzip_comp_level 5; # Compression level, the value is 1 to 9. The higher the level, the greater the compression ratio and the more CPU consumption. gzip_types text/plain application/javascript text/css image/jpeg image/gif image/png application/json; # File types that need to be compressed

8) server

A server block is a virtual service.

In the server block, you can specify the virtual service's port, service name, routing rules and other information.

There can be multiple servers.

There can be multiple locations on a server.

server {
        listen 90; # port server_name localhost; # service name, which can be an IP address or domain name. When the ports are the same, the routing rule will be selected according to the service name location / { # root path routing rule root html; # corresponds to the html folder under the nginx installation target, and can also be set to an absolute path, for example: root /home/html;
            index hello.html; #Specify the default homepage as hello.html
        }
     location /hello { root /home/hello; # index is omitted, indicating no default page} 
             error_page 500 502 503 504 /50x.html; # Specify the error page to which these status codes jump location = /50x.html {
            root html;
        }
    }

9) Detailed explanation of location matching rules

The server block contains the location block. There can be multiple location blocks under a server, which are mainly used to configure the routing rules of requests.

Nginx matches the requested resource path with the location block, and then forwards the route according to the location configuration.

Location supports multiple matching rules, which are described in detail below.

Exact Match

location / { # Root path routing rule root html; # Corresponding to the html folder under the nginx installation target, it can also be set to an absolute path, for example: root /home/html;
            index hello.html; #Specify the default homepage as hello.html
        }
    location /hello {
     root /home/hello;
     # index omitted, indicating no default page}

Regular expression matching

    location ~* \.(GIF|PNG|jpg|bmp|jpeg) { # * represents case insensitive root /home/img;
    }

Matches requests starting with a certain path

    location ^~ /server/page/ {
        root /home/page;
    }

3. Overview

Today we have explained Nginx configuration in detail. Some advanced applications of Nginx will be introduced separately later. I hope we can communicate more and grow together.

This is the end of this article about the detailed explanation and optimization of Nginx configuration file. For more relevant Nginx configuration file optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief analysis of the writing and use of variables in the Nginx configuration file
  • Chinese comments on Nginx's nginx.conf configuration file
  • Detailed explanation of nginx configuration file in Chinese
  • Introduction to Nginx configuration and configuration files under Windows
  • Nginx server configuration file complete analysis
  • Detailed explanation of how to find the location of the nginx configuration file
  • Detailed explanation of Nginx configuration file
  • Detailed explanation of Nginx configuration file

<<:  Why MySQL does not recommend using null columns with default values

>>:  Introducing multiple custom fonts in CSS3

Recommend

How does JS understand data URLs?

Table of contents Overview Getting started with d...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

Summary of experience in using div box model

Calculation of the box model <br />Margin + ...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...

Detailed explanation of Docker common commands Study03

Table of contents 1. Help Command 2. Mirror comma...

Control the light switch with js

Use js to control the light switch for your refer...

vue3+ts+EsLint+Prettier standard code implementation

Table of contents use Use of EsLint Add a profile...

vue.js downloads pictures according to picture url

Recently, when I was working on a front-end vue.j...

js realizes the image cutting function

This article example shares the specific code of ...

Two ways to create SSH server aliases in Linux

Preface If you frequently access many different r...

Detailed explanation of the usage of DECIMAL in MySQL data type

Detailed explanation of the usage of DECIMAL in M...