nginx配置文件了解_了解NGINX配置文件

nginx配置文件了解

It’s very important to understand the structure of the NGINX configuration file to get the best performance from your server. Further, by applying directives on right context in a NGINX configuration will enable you to eliminate repetitive codes and manage your infrastructure better.

了解NGINX配置文件的结构以从服务器中获得最佳性能非常重要。 此外,通过在NGINX配置中的正确上下文中应用指令将使您能够消除重复的代码并更好地管理基础架构。

Therefore to help you understand the structure of the NGINX configuration file, this article will focus on describing the structure of the NGINX configuration file in details.

因此,为了帮助您理解NGINX配置文件的结构,本文将重点介绍NGINX配置文件的详细结构。

什么是Nginx上下文? (What is Nginx Context?)

Let us start with checking the location of the NGINX configuration file. The core configuration file (nginx.conf) can be found in /etc/nginx. The location will vary depending on the installation procedure of NGINX.

让我们开始检查NGINX配置文件的位置。 核心配置文件( nginx.conf )可以在/etc/nginx 。 该位置将根据NGINX的安装过程而有所不同。

Open the core NGINX configuration file in a text editor. The very first thing that you will notice that the configurations are organized in a tree-like structure surrounded by curly braces { and }. These locations surrounded by braces is known as Context for placing configuration directive. Moreover, the configuration directives along with their parameters end with a semicolon.

在文本编辑器中打开核心NGINX配置文件。 首先,您会注意到配置是用花括号{和}包围的树状结构组织的。 用括号括起来的这些位置称为用于放置配置指令的上下文。 此外,配置指令及其参数以分号结尾。

In NGINX, contexts can be nested within other contexts thereby creating a hierarchy. In the following example, the HTTP context declares settings for HTTP protocol. Virtual host settings are declared in the server context and are contained in the http context. Locations contexts that are used to store settings for URLs are contained within a server context.

在NGINX中,上下文可以嵌套在其他上下文中,从而创建层次结构。 在以下示例中,HTTP上下文声明了HTTP协议的设置。 虚拟主机设置在服务器上下文中声明,并包含在http上下文中 。 服务器上下文中包含用于存储URL设置的位置上下文。

# Global context
 ...
 ...
 # http context
http{
     ...
     ...
     # Server context
     server {
              listen 80;
              server_name example.com;
              ...
              ...
              # Location context
              location / {            
                          root /var/www/html;            
                          try_files $uri $uri/ =404;        
                          ...
                          ...
             }
    }
    # Server context
    server {
             ...
             ...
             # Location context
             location / { 
                         ...
                         ...
             }
    }
    ...
    ...
}

NGINX配置文件–主要上下文 (NGINX Config File – Main context)

The main context is used to set the settings for NGINX globally and is the only context that is not surrounded by curly braces. Generally, The main context is placed at the beginning of the core NGINX configuration file. The directives for the main context cannot be inherited in any other context and therefore cannot be overridden.

主要上下文用于全局设置NGINX的设置,并且是唯一不包含花括号的上下文。 通常,主上下文位于核心NGINX配置文件的开头。 主上下文的指令不能在任何其他上下文中继承,因此不能被覆盖。

user nginx;
worker_processes auto;
pid /run/nginx.pid;
...
...

NGINX配置文件–事件上下文 (NGINX Config File – Events context)

The events context is used to set global options for connection processing. The events context is included in the main context and there can be only one such context. The directives that you can place in this context are setting connection processing methods, the number of connections along with a few other directives.

事件上下文用于设置连接处理的全局选项。 事件上下文包含在主上下文中,并且只能有一个这样的上下文。 您可以在此上下文中放置的指令是设置连接处理方法,连接数以及其他一些指令。

# main context

events {
        # events context
      	worker_connections 768;	
      	multi_accept on;
}
...
...

NGINX配置文件– HTTP上下文 (NGINX Config File – HTTP context)

The HTTP context holds directives for handling HTTP and HTTPS traffic. The HTTP context is the child of the main context and is absolutely needed in every server context where virtual hosts settings are declared for your domains.

HTTP上下文包含用于处理HTTP和HTTPS流量的指令。 HTTP上下文是主上下文的子级,在为您的域声明了虚拟主机设置的每个服务器上下文中,绝对需要HTTP上下文。

user nginx;
worker_processes auto;
pid /run/nginx.pid;
...
...
events {
        # events context
        worker_connections 768;	
        multi_accept on;
      	...
      	...
}
http {
       sendfile on;	
       tcp_nopush on;	
       tcp_nodelay on;	
       keepalive_timeout 65;
       ...
       ...
}

NGINX配置文件–服务器上下文 (NGINX Config File – Server context)

The NGINX virtual host settings are defined in the server contexts which in turn are contained in the HTTP context. There can be multiple server contexts inside the HTTP context. The directives inside server context govern the processing of requests for resources associated with a particular domain or IP address.

NGINX虚拟主机设置在服务器上下文中定义,而服务器上下文又包含在HTTP上下文中。 HTTP上下文中可以有多个服务器上下文。 服务器上下文中的指令控制与特定域或IP地址关联的资源请求的处理。

user nginx;
worker_processes auto;
pid /run/nginx.pid;
...
...
events {
         # events context
         worker_connections 768;	
      	 multi_accept on;
      	 ...
      	 ...
 }
http {
       sendfile on;	
       tcp_nopush on;	
       tcp_nodelay on;	
       keepalive_timeout 65;
       ...
       ...

       server {
                listen 80;
                server_name domain1.com;
                root /var/www/html/wordpress;
                ...
     
       }
       server {
                listen 80;
                server_name domain2.com;
                root /var/www/html/drupal;
                ...
     
       }
}

NGINX配置文件–位置上下文 (NGINX Config File – Location context)

Location contexts are used to define directives to handle client request. When a request for resource arrives at NGINX, it will try to match the URI to one of the locations and handle it accordingly.

位置上下文用于定义指令以处理客户端请求。 当对资源的请求到达NGINX时,它将尝试将URI与位置之一进行匹配并进行相应的处理。

Multiple Location contexts can be defined inside server blocks. Moreover, a location context can also be nested inside another location context.

可以在服务器块内定义多个位置上下文。 此外,位置上下文也可以嵌套在另一个位置上下文中。

http {
       ...
       ...

       server {
                listen 80;
                server_name domain1.com;
                root /var/www/html/wordpress;
                ...
                location /some_url {  
                # configuration for processing URIs starting with /some_url
                }
                location /another_url {  
                # configuration for processing URIs starting with /another_url
                }    
       }
       server {
                listen 80;
                server_name domain2.com;
                root /var/www/html/drupal;
                ...
                location /some_url {  
                # configuration for processing URIs starting with /some_url  
                }
                location /some_other_url {  
                # configuration for processing URIs starting with /some_other_url
                }  
     
       }
}

上游环境 (Upstream context)

The upstream context is allowed to define a pool of back-end servers that NGINX can proxy the request. The context enables NGINX to perform load balancing while proxying the request. The upstream context is defined inside the HTTP context and outside any server context.

允许上游上下文定义NGINX可以代理请求的后端服务器池。 上下文使NGINX能够在代理请求时执行负载平衡。 上游上下文在HTTP上下文内部和任何服务器上下文外部定义。

Once upstream servers have been defined, the name of the same is available within the server context to pass the request to the pool of back-end servers.

一旦定义了上游服务器,就可以在服务器上下文中使用相同的名称,以将请求传递给后端服务器池。

http{
     ...
     ...
     upstream backend_servers {
                                server host1.example.com;
                                server host2.example.com;
                                server host3.example.com;
     }

server {
             listen 80;
             server_name example.com;
             location / {
                           proxy_pass https://backend_servers;
             }
  }
}

邮件内容 (Mail context)

NGINX can proxy the IMAP, POP3, and SMTP protocols to one of the upstream mail servers or to an external mail service where the mail accounts are hosted. To accomplish it, NGINX uses mail context that allows configuration directives for all aspects of proxying mail connections.

NGINX可以将IMAP,POP3和SMTP协议代理到上游邮件服务器之一或托管邮件帐户的外部邮件服务。 为此,NGINX使用邮件上下文,该上下文允许在代理邮件连接的所有方面使用配置指令。

The mail context is defined in the main context i.e at the same level as the HTTP context.

邮件上下文是在主上下文中定义的,即与HTTP上下文处于同一级别。

# main context
mail {
       server_name mail.example.com;
       auth_http   localhost:9000/cgi-bin/nginxauth.cgi;
       proxy_pass_error_message on;
       ...
}
http {

}
...
...

NGINX配置文件–如果上下文 (NGINX Config File – If context)

The if-context allows conditional execution of directives defined within it. The context is same as an if statement in any other programming languages. The if context should be avoided as far as possible due to some limitations of it which is discussed here.

if-context允许有条件地执行其中定义的指令。 上下文与任何其他编程语言中的if语句相同。 该上下文是否应避免尽可能因为这是讨论它的一些局限在这里

http {
        server {
                     location /some_url {
                     if (test_condition) {
                          # do some stuff here
                   }

         }
    }
}

Limit_except上下文 (Limit_except Context)

The limit_except context allows preventing the use of all HTTP methods except the ones that you explicitly allow. In the location context, you can restrict the usage of certain HTTP methods like forbidding clients from sending POST requests using limit_except context.

limit_except上下文允许阻止使用除明确允许的所有HTTP方法之外的所有HTTP方法。 在位置上下文中,您可以限制某些HTTP方法的使用,例如禁止客户端使用limit_except上下文发送POST请求。

Just define two elements in a limit_except context, first one is the methods that are allowed and second is the audience those are affected by the restriction.

只需在limit_except上下文中定义两个元素,第一个是允许的方法,第二个是受限制影响的受众。

...
...
location /wp-admin/ { 
    limit_except GET { 
      allow 127.0.0.1; 
      deny all; 
    } 
}
...
...

The above limit_except example allows all visitors to use the GET header in the location /wp-admin. The other HTTP headers are allowed if it is originated from local address only.

上面的limit_except示例允许所有访问者在/ wp-admin位置使用GET标头。 如果其他HTTP标头仅源自本地地址,则允许使用其他HTTP标头。

NGINX配置文件的其他上下文 (NGINX Config File Miscellaneous Contexts)

Apart from the context those we have discussed so far, there are few other contexts available in NGINX and are described below. These contexts are dependent on optional modules and are used very rarely.

除了到目前为止我们已经讨论过的上下文,NGINX中几乎没有其他可用的上下文,下面将进行描述。 这些上下文取决于可选模块,很少使用。

  • split_clients: The split_client context is used to split clients request into two or more categories. This context is defined inside http context and are mainly used for A/B testing.

    split_clients :split_client上下文用于将客户端请求分为两个或多个类别。 此上下文在http上下文内部定义,主要用于A / B测试。
  • geo: The geo context is used to categorize client IP addresses. It maps the value of a variable depending on the connecting IP address.

    geo :geo上下文用于对客户端IP地址进行分类。 它根据连接的IP地址映射变量的值。
  • charset_map: The charset_map context is used to add specific charset to the “Content-Type” response header field. In addition, using the context one can convert data from one charset to another with some limitations.

    charset_map :charset_map上下文用于将特定的字符集添加到“ Content-Type”响应头字段。 此外,使用上下文可以将数据从一种字符集转换为另一种字符集,但有一些限制。
  • map: The map context creates variables whose values depend on values of other variables and are defined in the http context.

    map :map上下文创建变量,其值取决于其他变量的值,并在http上下文中定义。
  • perl / perl_set: The perl context is used to implement location and variable handlers in Perl and insert Perl calls into SSI. Further, using perl_set context one can install a Perl handler for a specific variable.

    perl / perl_set :perl上下文用于在Perl中实现位置和变量处理程序,并将Perl调用插入SSI。 此外,使用perl_set上下文可以为特定变量安装Perl处理程序。
  • types: The types context is used to map MIME types with correct file extensions. The types context may appear in the http, server or location context. The default mime types are loaded through NGINX core configuration file nginx.conf.

    types :类型上下文用于映射具有正确文件扩展名的MIME类型。 类型上下文可以出现在http,服务器或位置上下文中。 默认的mime类型通过NGINX核心配置文件nginx.conf加载。

摘要 (Summary)

Now that you have a basic idea of NGINX configuration file, use the context described above to fine tune the configuration in your environments. While defining configuration for NGINX, always define the common directives in the higher context if it is available and if necessary override them in the lower context. This will avoid unnecessary code repetition in the configuration file.

既然您对NGINX配置文件有了基本的了解,就可以使用上述上下文在您的环境中微调配置。 在为NGINX定义配置时,请始终在较高的上下文中定义公共指令(如果可用),并在必要时在较低的上下文中覆盖它们。 这样可以避免配置文件中不必要的代码重复。

Lastly using If context may result in an unexpected result that is already mentioned earlier. NGINX provides a lot of directives and using it you can get the same result without using if context. If you really want to use if-context then deploy the configuration after heavy testing.

最后使用If上下文可能会导致前面已经提到的意外结果。 NGINX提供了很多指令,使用它可以不使用if上下文而获得相同的结果。 如果您确实要使用if-context,请在大量测试之后部署配置。

翻译自: https://www.journaldev.com/26618/nginx-configuration-file

nginx配置文件了解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值