Nginx configuration

Nginx is installed in /usr/local/nginx. The default configuration files are in the conf directory of this directory, and the main configuration file nginx.conf is also in this directory. Subsequent use of Nginx is basically a modification of this configuration file, so in this blog post we will outline the structure of this configuration file.

Linux opens nginx in the same directory as Windows

Nginx. conf configuration file

1, nginx.conf body structure

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0. 01.:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http:/ / 127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0. 01.:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0. 01.:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Copy the code

# indicates the content of the comment. We removed all the paragraphs starting with # and condensed the content as follows:

 1 worker_processes  1;
 2 
 3 events {
 4     worker_connections  1024;
 5 }
 6 
 7 
 8 http {
 9     include       mime.types;
10     default_type  application/octet-stream;
11 
12 
13     sendfile        on;
14 
15     keepalive_timeout  65;
16 
17     server {
18         listen       80;
19         server_name  localhost;
20 
21         location / {
22             root   html;
23             index  index.html index.htm;
24         }
25 
26         error_page   500 502 503 504  /50x.html;
27         location = /50x.html {
28             root   html;
29         }
30 
31     }
32 
33 }

Copy the code

Open the nginx.conf body structure in Linux and uncomment the above

2. Global block

The contents from the configuration file to the Events block will mainly set some configuration instructions affecting the overall operation of the Nginx server, including the configuration of users (groups) running the Nginx server, the number of worker processes allowed to be generated, Process PID storage path, log storage path and type, and configuration file import. For example, in the first line above:

worker_processes  1;
Copy the code

This is a key configuration for Nginx server concurrent processing services. The larger the worker_processes value is, the more concurrent processing it can support is limited by hardware, software, and other devices, as described later.

3, the events

For example:

events {
    worker_connections  1024;
}

Copy the code

The instructions involved in the Events block mainly affect the network connection between the Nginx server and the user. The commonly used Settings include whether to enable serialization of network connections under the multi-work process, whether to allow multiple network connections to be received at the same time, and which event-driven model to process connection requests. Maximum number of connections that each Word process can support at the same time, etc.

The above example indicates that each work process supports a maximum of 1024 connections.Copy the code

This part of the configuration has a great impact on Nginx performance, and should be flexibly configured in practice.

4, HTTP block

 http {
      include       mime.types;
     default_type  application/octet-stream;
  
 
     sendfile        on;
  
      keepalive_timeout  65;
  
     server {
         listen       80;
         server_name  localhost;
 
        location / {
             root   html;
             index  index.html index.htm;
         }
 
         error_page   500 502 503 504/50x.html; location = /50x.html { root html; }}}Copy the code

This is the most frequent part of the Nginx server configuration, where most functions such as proxies, caching, and logging definitions are configured, as well as third-party modules.

Note that HTTP blocks can also include HTTP global blocks and server blocks.

HTTP global block

HTTP global block configuration instructions include file import, MIME-Type definition, log customization, connection timeout, maximum number of single link requests, and so on.

(2), the server

This and virtual host has a close relationship, virtual host from the user’s point of view, and an independent hardware host is exactly the same, the technology is produced in order to save the cost of Internet server hardware. The concept of virtual hosting will be described in more detail later.

Each HTTP block can contain multiple Server blocks, and each server block is equivalent to a virtual host.

Each Server block is also divided into global Server blocks and can contain multiple Locaton blocks simultaneously.

1. Global Server block

The most common configuration is the listening configuration of the host and the name or IP configuration of the host.

2, the location

A server block can be configured with multiple Location blocks.

The main purpose of this block is to process a specific request based on the request string received by the Nginx server (e.g. Server_name/URI-string), matching any string other than the virtual host name (or IP alias) (e.g. / URI-string). Address targeting, data caching and response control, as well as many third-party modules are configured here.