List of common configurations of nginx server


Write in front

Nginx is an excellent representative of high-performance lightweight web server. Because it provides a series of important features such as HTTP proxy and reverse proxy, load balancing and caching, it is widely used in today’s Web back-end services, and major Internet companies are also heavily used. Therefore, as a developer, it is necessary to learn how to use and configure nginx.

In this article, we will start with an example configuration list to briefly sort out the functions and usage of various common configuration instructions of the nginx server.

Don’t talk much, serve!

This article is in the GitHub open source repositoryThe way of programming has been included in the, which I sorted outSelf study route of 6 major programming directions (posts) + knowledge pointsInterview siteMy resumeSeveral hard core PDF notes, andMy life as a programmer, welcome to appreciate.

The overall structure of the nginx configuration file

You can draw a picture directly here at a glance, and you can see several large configuration modules clearly.

List of common configurations of nginx server

As can be seen from the figure, it mainly includes the following parts:

1. Global block

This part of the configuration mainly affects the global of nginx, and usually includes the following parts:

  • Configure users (groups) running nginx servers
  • Number of worker processes
  • PID storage path of nginx process
  • Storage path of error log
  • Introduction of configuration files

2. Events block

This part of the configuration mainly affects the network connection between the nginx server and users, mainly including:

  • Sets the serialization of the network connection
  • Allow multiple network connections at the same time
  • Selection of event driven model
  • Configuration of maximum connections

3. HTTP block

  • Define Mimi type
  • Custom service log
  • Allow sendfile transfer
  • Connection timeout
  • Maximum number of single connection requests

4. Server block

  • Configure network port listening
  • Access log and error pages
  • Name based virtual host configuration
  • IP based virtual host configuration
  • Location block configuration

5. Location block

  • Location configuration
  • Request root configuration
  • Change the URI of location
  • Default homepage configuration

An example of configuration list

Here is a brief example of nginx configuration list:

List of common configurations of nginx server

The configuration code is as follows:

user  nobody  nobody;
worker_processes  3;
error_log  logs/error.log;
pid  logs/;

events {
    use epoll;
    worker_connections  1024;

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile  on;
    keepalive_timeout  65;

    server {
        listen       8088;
        server_name  codesheep;
        access_log  /codesheep/webserver/server1/log/access.log;
        error_page  404  /404.html;

        location /server1/location1 {
            root   /codesheep/webserver;
            index  index.server2-location1.htm;

        location /server1/location2 {
        root   /codesheep/webserver;
            index  index.server2-location2.htm;


    server {
        listen       8089;
        access_log  /codesheep/webserver/server2/log/access.log;
        error_page  404  /404.html;
        location /server2/location1 {
            root   /codesheep/webserver;
            index  index.server2-location1.htm;

        location /srv2/loc2 {
            alias   /codesheep/webserver/server2/location2/;
            index  index.server2-location2.htm;
        location = /404.html {
            root /codesheep/webserver/;
            index 404.html;


Next, we will analyze the meaning and usage of several main instructions in the configuration file in detail according to this example configuration list.

Nginx user (Group) configuration

Configuration item format:user user [group];

  • User: Specifies the user who can run nginx
  • Group: specify the user group that can run nginx (optional)

If the user command is not configured or configured asuser nobody nobody, all users can start the nginx process by default.

Worker process count configuration

This is the key configuration for the nginx server to realize concurrent processing. The configuration item format is:

worker_ Processes number;

  • Number: the maximum number of worker processes that can be generated by the nginx process
  • If set to auto, nginx will perform automatic detection

According to the experiment in the configuration list above, we give the worker_ The number of processes configured is: 3. After starting the nginx server, we can take a look at the nginx processes on the host in the background:

ps -aux | grep nginx

Obviously, I understandworker_processesThe meaning of this instruction is easy

List of common configurations of nginx server

Error log path configuration

Configuration item format:error_ Log file [optional log level];

  • File: Specifies the log output to a file file
  • Common optional log levels include info, debug, warn, error, etc

Configuration of PID storage path of nginx process

Since the nginx process runs in the background as a system daemon, this option is used to customize the save path of the configuration PID file.

Configuration item format:pid file;

  • File: specify its storage path + file name
  • If you do not specify a default pathlogs/

Event driven model configuration

Configuration item format:use model;

  • The optional items of model include: select, poll, kqueue, epoll, rtsig, etc

Maximum connections configuration

Configuration item format:worker_ Connections number;

  • The default value of number is 512, which indicates the maximum number of connections that each worker process is allowed to open at the same time.

Introduction of configuration files

This configuration is mainly used to import other or third-party nginx configuration files into the current master configuration file

Configuration item format:include conf_file;

Serialization configuration of network connection

Configuration item format:accept_mutex on;

  • The configuration defaults to on status, which means that multiple nginx worker processes will receive connections for serialization to prevent multiple worker processes from competing for connections.

When it comes to this instruction, we must first explain what is the so-called “surprise group problem”. When a new network connection arrives, multiple worker processes will wake up at the same time, but only one process can really get connected and handle it. If too many processes wake up each time, it will actually affect some performance.

So here, if accept_ Mutex on, then multiple workers will be processed in serial mode, and one worker will be awakened; Conversely, if accept_ Mutex off, then all workers will wake up, but only one worker can get a new connection, and the other workers will enter the sleep state again.

Whether this value is switched or not is actually linked to the specific scenario, which will affect the system throughput to a certain extent. Nginx opens accept by default_ Mutex is also a conservative approach.

Multi network connection receiving configuration

Configuration item format:multi_accept off;

  • This configuration defaults to off, which means that each worker process can only receive one newly arrived network connection at a time. If you want each nginx worker process to receive multiple network connections at the same time, you need to enable this configuration.

MIME type definition

MIME type refers to the media type of network resources, that is, the resource type requested by the front end.

Configuration item format:

include mime.types;

default_ Type type;
  • The include configuration is used to include the mime.types file

Can usecat mime.typesTo view the file content of mime.types, we find that it is a types structure, which contains MIME types recognized by various browsers and file suffixes of corresponding types, as shown below:

List of common configurations of nginx server

Access log configuration

Configuration item format:

access_log path [format];
  • Path: path + name of custom access log
  • Format: customize the format of the service log (optional).

Connection timeout configuration

Configuration item format:keepalive_timeout timeout [header_timeout];

  • Timeout indicates the holding time of the server side to the connection
  • header_ Timeout means to set the timeout in the keep alive field of the response message header. Optional.

Sendfile configuration

Configuration item format:

sendfile on;
  • Sendfile configuration is used to turn on or off. Sendfile() system call is used to transfer files. The default is off
  • Note: in many web servers, sendfile mechanism is introduced to realize high-performance file transfer.

Network address listening configuration

Configuration item format:

  • First: configure the IP address to listen to:listen IP[:PORT];
  • Second: configure the listening port:listen PORT;

Practical examples:

listen; #  Listen for connections on specific IP and ports
listen;      #  Listen for connections to all ports on a specific IP
listen 8080;                #  Listen for all IP connections on a specific port

Name or IP based virtual host configuration

Configuration item format:server_name name1 name2 ...

  • Name can have multiple parallel names, and the name here supports regular expression writing

Practical examples:

server_name ~^www\.codesheep\d+\.com$;

The IP based virtual host configuration is simpler:

Configuration item format:server_ Name IP address

Location configuration

The configuration item format is:location [ = | ~ | ~* | ^~ ] /uri/ {...}

  • The URI here can contain fuzzy matching of regular expressions.

The contents in square brackets before URI are optional. Several common situations are as follows:

  • “=”: used for standard URI to perform exact string matching
  • “~”: used for regular URIs, indicating case sensitive matching
  • “~ *”: used for regular URI, indicating case insensitive matching
  • “^ ~”: used for standard URI, ^ for prefix matching, ~ for case sensitivity

Root configuration

Configuration item format:root path;

  • Path: indicates the root directory path where nginx looks for resources after receiving the request

Of course, you can also change the URI request path received by location through the alias instruction. The instruction is:

alias path;  #  Path is the modified root path

Default home page configuration

Configuration item format:index index_file ......

  • index_ File can contain multiple file names separated by spaces. Which page to find first will respond with.


This article is in the GitHub open source repositoryThe way of programming has been included in the, which I sorted outSelf study route of 6 major programming directions (posts) + knowledge pointsInterview siteMy resumeSeveral hard core PDF notes, andMy life as a programmer, welcome to appreciate.

See you next!

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]