Sunday, October 15, 2017

Nginx—Knowing the Basics

Nginx is a lightweight, high performance web server designed to deliver large amounts of static content quickly with efficient use of system resources. nginx’s strong point is its ability to efficiently serve static content, like plain HTML and media files. Some consider it a less than ideal server for dynamic content.[1]


Concepts of Niginx

  • Nginx Process
    • Nginx has one master process and several worker processes.
      • Master process (1)
        • To read and evaluate configuration, and maintain worker processes
      • worker processes (N)
        • To do actual processing of requests
        • Each worker can handle thousands of concurrent connections. It does this asynchronously with one thread, rather than using multi-threaded programming.
  • Content Handling
    • Static Content
      • Nginx’s strong point is its ability to efficiently serve static content, like plain HTML and media files. 
    • Dynamic Content
      • Rather than using the embedded interpreter approach, nginx hands off dynamic content to CGI, FastCGI, or even other web servers like Apache, which is then passed back to nginx for delivery to the client.
  • Request distribution
    • Unlike Apache, which uses a threaded or process-oriented approach to handle requests, nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. 
      • which provides more predictable performance under load.

Configuration 


The way nginx and its modules work is specified in the configuration file.
  • nginx.conf
    • By default, the main configuration file is named nginx.conf and placed in the directory:
      • /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx
    • Can be also configured by a compile-time option:
      • --conf-path=/u01/data/config/nginx/nginx.conf
  • Need to reload configuration after changes
    • Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. 
    • To reload configuration, execute:
      • nginx -s reload
To learn how to start, stop, and reload configuration in more details, read here.


Configuration—Syntax and Semantics 


The syntax and semantics of nginx's configuration files are described below:
  • Comments
    • All lines preceded by a pound sign or hash (#) are comments
  • Directives 
    • nginx consists of modules which are controlled by directives specified in the configuration file.
      • Directives describe the basic behavior of the web server
    • Directives are divided into 
      • Simple directives 
        • All statements end with a semi-colon (;)
      • Block directives
        • Blocks are variables having arguments that are themselves sub-directives enclosed in brackets ({ })
        • Examples
          • http{} block — Universal Configuration
          • server{} block — Virtual Domains Configuration
            • Configures multiple servers virtually on different ports or with different server names
              • The server_name directive, which is located in the server block, lets the administrator provide name-based virtual hosting. 
          • upstream{} block
            • Defines a cluster that you can proxy requests to
              • Commonly used for defining either a web server cluster for load balancing, or an app server cluster for routing / load balancing
  • Include statement
    • Can be used to include directives from a separate file.
    • Anything written in the file is interpreted as if it was written inside the enclosing block. 
  • Context
    • If a block directive can have other directives inside braces, it is called a context (examples: events, http,server, and location).
    • Examples
      • Directives placed in the configuration file outside of any contexts are considered to be in the main context. 
        • The events and http directives reside in the main context, server in http, and location in server.
      • access_log directive sets the location of the nginx access log
        • which can be set in either
          • http block, or 
            • which can be used to log all access to a single file, or as a catch-all for access to virtual hosts that don’t define their own log files
          • server block
            • which sort the output specific to each virtual domain into its own file

Http Request Handling


When clients send http requests to nginx, it distributes requests based on the following information:
  • Request URL
  • Request Headers (e.g., Host  header)
and processes them in the following sequence:
  1. Receives data from clients
  2. Parses the http request
  3. Finds the virtual server
    • Configured by server {} blocks
  4. Finds the location
    • Specified by the location directive 
      • Directs requests to specific files and folders
  5. Runs phase handlers
  6. Generates the http response
  7. Filters response headers
  8. Filters the response body
  9. Sends out the output to the client
Note that nginx always fulfills request using the most specific match.

1 comment:

gream said...

The time of playing HD online casino games is an interesting game, so you have the opportunity to enter the entertainment system variety. Golden slot Online Casinos It is an interesting game, so you have to bet on the phone very well. Play with us more easily, the fun can play our mobile games as best. Gclub69 is an easy-to-play, life-changing game that will always be a real game. Meet all needs through good communication devices. You can bet on us at any time. Fun without a break We have continuous entertainment. Play casino games are constantly. We welcome and create opportunities for our online gamblers to enjoy their favorite online gambling online casino. It is another of the gambling games that will make us enjoy the good of online casino games 24 hours a day. สล็อตออนไลน์