search
HomeBackend DevelopmentPHP TutorialParse Nginx configuration file

After Nginx is installed, the corresponding installation directory will be generated. According to the previous installation path, the Nginx configuration file path is /opt/nginx/conf, where nginx.conf is the main configuration file of Nginx. Here we focus on the nginx.conf configuration file.
                                Nginx configuration file is mainly divided into four parts: main (global settings), server (host settings), upstream (load balancing server settings) and location (URL matches settings for a specific location). The instructions set in the main part will affect all other settings; the instructions in the server part are mainly used to specify the host and port; the upstream instructions are mainly used for load balancing and setting up a series of back-end servers; the location part is used to match the location of the web page. The relationship between the four: server inherits main, location inherits server, and upstream will neither inherit other settings nor be inherited.中 Among these four parts, each part contains several instructions. These instructions mainly include Nginx's main module instructions, event module instructions, HTTP core module instructions, and each part can also use other HTTP module instructions, such as instructions, such as Http SSL module, HttpGzip Static module and Http Addition module, etc.
The following uses an Nginx configuration example to introduce the meaning of each instruction in nginx.conf in detail. In order to have a clearer understanding of the structure of Nginx and the meaning of each configuration option, the Nginx configuration file is divided into 7 parts according to the function points and explained one by one. The following is an introduction around these 7 parts.
1. Global configuration of NginxThe following content is the global attribute configuration of Nginx. The code is as follows:



[html] view plaincopy

    user nobody nobody;
  1. worker_processes 4;
  2. error_log logs/error.log notice;
  3. pid Logs/nginx.pid;
  4. worker_rlimit_nofile 65535;
  5. events{
  6. use epoll;
  7. worker_connections 65536;
  8. }
The meaning of each configuration option in the above code is explained as follows:
user is a master Module directive, specifying the user and user group for running the Nginx Worker process. By default, it is run by the nobody account.
worker_processes is a main module instruction that specifies the number of processes to be opened by Nginx. Each Nginx process consumes an average of 10M~12M of memory. According to experience, it is generally enough to specify one process. If it is a multi-core CPU, it is recommended to specify the same number of processes as the number of CPUs.
error_log is a main module directive used to define global error log files. Log output levels include debug, info, notice, warn, error, and crit to choose from. Among them, debug output log is the most detailed, while crit output log is the least.
pid is a main module instruction, used to specify the storage file location of the process id.
worker_rlimit_nofile is used to specify the maximum number of file descriptors that an nginx process can open. Here it is 65535. You need to use the command "ulimit -n 65535" to set it. The
events command is to set the working mode of Nginx and the upper limit of the number of connections.

[html] view plaincopy

    events{
  1. use epoll;
  2. worker_connections 65536;
  3. }

  4. use is an event module command used to specify the working mode of Nginx. The working modes supported by Nginx are select, poll, kqueue, epoll, rtsig and /dev/poll. Among them, select and poll are standard working modes, and kqueue and epoll are efficient working modes. The difference is that epoll is used on the Linux platform, while kqueue is used on the BSD system. For Linux systems, the epoll working mode is the first choice.
    worker_connections is also an event module directive, used to define the maximum number of connections for each Nginx process. The default is 1024. The maximum number of client connections is determined by worker_processes and worker_connections, that is, Max_client=worker_processes*worker_connections, when acting as a reverse proxy , max_clients becomes: max_clients = worker_processes * worker_connections/4.
    The maximum number of connections of a process is limited by the maximum number of open files of the Linux system process. The setting of worker_connections can only take effect after executing the operating system command "ulimit -n 65536".

    2. HTTP server configuration
    Next, start the HTTP server settings.
    The following content is Nginx’s configuration of HTTP server-related attributes. The code is as follows:

    [html] view plaincopy

    1. http{
    2. include conf/mime.types;
    3. default_type application/octet-stream;
    4. log_formatmain '$remote_addr - $remote_user [$time_local] '
    5. '"$request" $status $bytes_sent ' '
    6. '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"';
    7. log_form at download '$remote_addr - $remote_user [ $time_local] '
    8. '"$request" $status $bytes_sent '
    9. '"$http_referer" "$http_user_agent" '
    10. '"$http_range" "$sent_http_content_range" ';
    11. client_max_body_size 20m;
    12. client_header_buffer_size 32K;
    13. large_client_header_buffers 4 32k;
    14. Sendfile on;
    15. tcp_nopush on;
    16. tcp_nodelay on;
    17. keepalive_timeout 60; client_header_timeout 10;
    18. client_body_timeout 10;
    19. send_timeout 10;
    20. The following is a detailed introduction to each configuration option in this code meaning. include is a main module instruction that enables the setting of files included in the configuration file, which can reduce the complexity of the main configuration file. Similar to the include method in Apache. default_type belongs to the HTTP core module directive. Here, the default type is set to binary stream, which is used when the file type is not defined. For example, when the PHP environment is not configured, Nginx will not parse it. At this time, use Browse When the PHP file is accessed by the server, a download window will appear.
    21. The code below implements the setting of the log format.


    [html] view plaincopy MLog_Format Main' $ Remote_ADDR-$ Remote_user [$ Time_local] '


    ' "$ Request" $ Status $ Bytes_SENT '

    ' "$ http_referr" $ http_user_agent "'
    1. '"$gzip_ratio"';
    2. log_format download '$remote_addr - $remote_user [$time_local] '
    3. '"$request" $status $bytes_sent '
    4. '"$http_referer" " $http_user_agent" '
    5. '"$http_range" "$sent_http_content_range"';

    6. log_format is Nginx’s HttpLog module instruction, used to specify the output format of Nginx logs. mainThe name of this log output format, which can be referenced in the access_log directive below.
      client_max_body_size is used to set the maximum number of bytes of a single file allowed to be requested by the client.
      client_header_buffer_size is used to specify the headerbuffer size from the client request header. For most requests, a buffer size of 1K is sufficient. If you customize the message headers or have larger cookies, you can increase the buffer size. This is set to 32K.
      large_client_header_buffers is used to specify the maximum number and size of caches for larger message headers in client requests. "4" is the number, "128K" is the size, and the maximum cache amount is 4 128K.
      The sendfile parameter is used to enable efficient file transfer mode. Set the tcp_nopush and tcp_nodelay instructions to on to prevent network blocking.
      keepalive_timeout sets the timeout for client connections to stay alive. After this time has elapsed, the server will close the connection.
      client_header_timeout sets the client request header reading timeout. If this time is exceeded and the client has not sent any data, Nginx will return a "Request time out (408)" error.
      client_body_timeout sets the client request body reading timeout. If this time is exceeded and the client has not sent any data, Nginx will return a "Request time out (408)" error. The default value is 60.
      send_timeout specifies the timeout period for responding to the client. This timeout is limited to the time between two connection activities. If this time is exceeded without any activity on the client, Nginx will close the connection.

      3.HttpGzip module configuration
      Configure Nginx’s HttpGzip module below. This module supports online real-time compression of output data streams. To check whether this module is installed, you need to use the following command:

      [html] view plaincopy

      1. [root@localhost conf]# /opt/nginx/sbin/nginx -V
      2. nginx version: nginx/0.7.65
      3. configure arguments: --with-http _stub_status_module -- with-http_gzip_static_module --prefix=/opt/nginx

      Use the /opt/nginx/sbin/nginx -V command to view the compilation options when installing Nginx. From the output, we can see that we have installed it HttpGzip module.
      The following are the relevant attribute settings of the HttpGzip module in the Nginx configuration:

      [html] view plaincopy

      1. gzip on;
      2. gzip_min_length 1k;
      3. gzip_buffers 4 16k;
      4. g zip_http_version 1.1;
      5. gzip_comp_level 2;
      6. gzip_types text/plain application/x -javascript text/css application/xml;
      7. gzip_vary on;

      gzip is used to set the gzip module on or off. "gzip on" means turning on GZIP compression and compressing the output data stream in real time.
      gzip_min_length sets the minimum number of bytes of the page allowed for compression. The number of page bytes is obtained from the Content-Length of the header. The default value is 0, which compresses the page regardless of its size. It is recommended to set the number of bytes to be greater than 1K. If it is less than 1K, the number of bytes may become larger and larger.
      gzip_buffers means applying for 4 units of 16K memory as the compression result stream cache. The default value is to apply for the same memory space as the original data size to store the gzip compression result.
      gzip_http_version is used to set and identify the HTTP protocol version. The default is 1.1. Currently, most browsers already support GZIP decompression, so just use the default.
      gzip_comp_level is used to specify the GZIP compression ratio. 1 has the smallest compression ratio and the fastest processing speed; 9 has the largest compression ratio and fast transmission speed, but the slowest processing and consumes more CPU resources.
      gzip_types is used to specify the compression type. Regardless of whether it is specified or not, the "text/html" type will always be compressed.
      The gzip_vary option allows the front-end cache server to cache GZIP-compressed pages, such as using Squid to cache Nginx-compressed data.

      4. Load balancing configuration
      Set the load balancing server list below.

      [html] view plaincopy

      1. upstream ixdba.net{
      2. ip_hash;
      3. server 192.168.12.133:80;
      4. server 1 92.168.12.134:80 down;
      5. server 192.168.12.135: 8009 max_fails=3 fail_timeout=20s;
      6. server 192.168.12. 136:8080;
      7. }

      upstream is from Nginx HTTP Upstream module, this module uses a simple scheduling algorithm to achieve load balancing from the client IP to the back-end server. In the above settings, the name of a load balancer ixdba.net is specified through the upstream directive. This name can be specified arbitrarily and can be called directly where needed later.
      Nginx’s load balancing module currently supports 4 scheduling algorithms, which are introduced below. The last two are third-party scheduling methods.
      Polling (default). Each request is assigned to different back-end servers one by one in chronological order. If a back-end server goes down, the faulty system is automatically eliminated so that user access is not affected.
      Weight. Specify the polling weight. The larger the Weight value, the higher the access probability allocated. It is mainly used when the performance of each back-end server is uneven.
      ip_hash. Each request is allocated according to the hash result of the accessed IP, so that visitors from the same IP can access a back-end server, which effectively solves the session sharing problem of dynamic web pages.
       fair. A more intelligent load balancing algorithm than the above two. This algorithm can intelligently perform load balancing based on page size and loading time, that is, allocate requests based on the response time of the back-end server, and prioritize those with short response times. Nginx itself does not support fair. If you need to use this scheduling algorithm, you must download the upstream_fair module of Nginx.
      url_hash. Distributing requests according to the hash result of the accessed URL so that each URL is directed to the same backend server can further improve the efficiency of the backend cache server. Nginx itself does not support url_hash. If you need to use this scheduling algorithm, you must install the Nginx hash software package.
      In the HTTP Upstream module, you can specify the IP address and port of the backend server through the server command, and you can also set the status of each backend server in load balancing scheduling. Commonly used states are:
       down, which means that the current server does not participate in load balancing for the time being.
       backup, reserved backup machine. The backup machine will be requested when all other non-backup machines fail or are busy, so this machine has the least pressure.
       max_fails, the number of allowed request failures, defaults to 1. When the maximum number of times is exceeded, the error defined by the proxy_next_upstream module is returned.
      fail_timeout, the time to suspend the service after max_fails failures. max_fails can be used together with fail_timeout.
      Note: When the load scheduling algorithm is ip_hash, the status of the backend server in load balancing scheduling cannot be weight and backup.

      5.server virtual host configuration
      The following introduces the configuration of the virtual host. It is recommended to write the configuration content of the virtual host into another file and then include it through the include directive, which makes maintenance and management easier.

      [html] view plaincopy

      1. server{
      2. listen 80;
      3. server_name 192.168.12.188 www.ixdba.net;
      4. index index.html index.htm index.jsp;
      5. root /web/wwwroot/www.ixdba.net
      6. charset gb2312;

      The server flag defines the start of the virtual host, listen is used to specify the service port of the virtual host, server_name is used to specify the IP address or domain name, and multiple domain names are separated by spaces. Index is used to set the default home page address for access, and the root command is used to specify the web page root directory of the virtual host. This directory can be a relative path or an absolute path. Charset is used to set the default encoding format of web pages.
      access_log logs/www.ixdba.net.access.log main;
      access_log is used to specify the access log storage path of this virtual host, and the last main is used to specify the output format of the access log.

      6.URL matching configuration
      URL address matching is the most flexible part of Nginx configuration. Location supports regular expression matching and conditional judgment matching. Users can use the location directive to implement Nginx filtering of dynamic and static web pages.
      The following setting uses the location directive to analyze and process the web page URL. All static files with extensions ending in .gif, .jpg, .jpeg, .png, .bmp, and .swf are handed over to nginx for processing, and expires Used to specify the expiration time of static files, here it is 30 days.

      [html] view plaincopy

      1. location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
      2.                                                                
      3.                                                                                                                      这}
      4. The following settings are handed over to all files under UPLOAD and HTML to Nginx for processing. Of course, the UPLOAD and HTML directory are included in /web/wwwroot/www.ixdba.net directory middle.
      5.                                                                                             plaincopy


      location ~ ^/(upload|html)/ {

                                                                                  expires 30d;
      1. In the last setting, location is the filtering process for dynamic web pages under this virtual host, that is, all files with the .jsp suffix are handed over to the 8080 port of the local machine for processing.
      2. [html] view plaincopy
      3. location ~ .*.jsp$ {
      4. index index.jsp;

      proxy_pass http://localhost:8080;


      }

      1. 7. StubStatus module configuration The StubStatus module can obtain the working status of Nginx since the last startup. This module is not a core module and needs to be manually specified when Nginx is compiled and installed to use this function.
      2. The following command actually specifies to enable the function of obtaining Nginx working status.
      3. ​​​​​
      4. [html] view plaincopy
        1. location /NginxStatus {
        2. stub_status on;
        3. access_log logs/NginxStatus.log;
        4.                                           htpasswd;
        5. }
        stub_status is set to "on" to enable the working status statistics function of StubStatus. access_log is used to specify the access log file of the StubStatus module. auth_basic is an authentication mechanism of Nginx. auth_basic_user_file is used to specify the password file for authentication. Since Nginx's auth_basic authentication uses a password file that is compatible with Apache, you need to use Apache's htpasswd command to generate a password file. For example, to add a webadmin user, you can use the following method to generate a password file. :
        /usr/local/apache/bin/htpasswd -c /opt/nginx/conf/htpasswd webadminYou will get the following prompt message:New password:

        After entering the password, the system will ask you to enter the password again. After confirmation, the user is added successfully.


        To check the running status of Nginx, you can enter http://ip/ NginxStatus, then enter the username and password you just created to see the following information:
        Active connections: 1
        server accepts handled requests

        393411 393411 393799

        Reading: 0 Writing: 1 Waiting: 0


        Active connections indicates the number of currently active connections. The three numbers in the third line indicate that Nginx has currently processed a total of 393411 connections, successfully created 393411 handshakes, and processed a total of 393799 requests. . Reading in the last line indicates the number of client header information read by Nginx. Writing indicates the number of header information returned to the client by Nginx. "Waiting" indicates the number of resident connections that Nginx has completed processing and is waiting for the next request instruction.

        In the last setting, the error message return page of the virtual host is set. The return page of various error messages can be customized through the error_page command. By default, Nginx will search for the specified return page in the HTML directory of the home directory. It is particularly important to note that the size of the return page for these error messages must exceed 512K, otherwise it will be replaced by the IE browser's default Error page.



        [html] view plaincopy 404.html;

        location
          = /50x.html {
        1. root html;
        2.                                                                                                                                                                    Technology Makes Dreams. 1/790611
        3. The above introduces the parsing of Nginx configuration files, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
解决方法:您的组织要求您更改 PIN 码解决方法:您的组织要求您更改 PIN 码Oct 04, 2023 pm 05:45 PM

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows 11 上调整窗口边框设置的方法:更改颜色和大小Windows 11 上调整窗口边框设置的方法:更改颜色和大小Sep 22, 2023 am 11:37 AM

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

如何在 Windows 11 上更改标题栏颜色?如何在 Windows 11 上更改标题栏颜色?Sep 14, 2023 pm 03:33 PM

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题Jul 16, 2023 pm 03:29 PM

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

Windows 11 上启用或禁用任务栏缩略图预览的方法Windows 11 上启用或禁用任务栏缩略图预览的方法Sep 15, 2023 pm 03:57 PM

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

Windows 11 上的显示缩放比例调整指南Windows 11 上的显示缩放比例调整指南Sep 19, 2023 pm 06:45 PM

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

10种在 Windows 11 上调整亮度的方法10种在 Windows 11 上调整亮度的方法Dec 18, 2023 pm 02:21 PM

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

如何修复Windows服务器中的激活错误代码0xc004f069如何修复Windows服务器中的激活错误代码0xc004f069Jul 22, 2023 am 09:49 AM

Windows上的激活过程有时会突然转向显示包含此错误代码0xc004f069的错误消息。虽然激活过程已经联机,但一些运行WindowsServer的旧系统可能会遇到此问题。通过这些初步检查,如果这些检查不能帮助您激活系统,请跳转到主要解决方案以解决问题。解决方法–关闭错误消息和激活窗口。然后,重新启动计算机。再次从头开始重试Windows激活过程。修复1–从终端激活从cmd终端激活WindowsServerEdition系统。阶段–1检查Windows服务器版本您必须检查您使用的是哪种类型的W

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version