Nginx
Overview
- Nginx has 1 master process, and multiple worker processes.
- The master's job is to read configuration files, and to manage the worker processes.
- The worker processes handle the requests.
- Nginx uses an event-based model to distribute requests among workers
- The # of workers is specified in the config file, and may either be fixed, or adjustable based on how many cores the CPU has.
Config file
- The nginx config file
nginx.confis stored either in/usr/local/nginx/conf,/etc/nginx, or/usr/local/etc/nginx. - nginx consists of modules which are controlled by directives specified in the configuration file
- Directives can either be simple directives or block directives
- simple ends with
;, block uses{}
- simple ends with
- Directives can either be simple directives or block directives
- If a directive can have other directives inside, it is called a Context
- ex.
events,http,server,location
- ex.
- If a directive is not placed within a Context, then it is considered to be in the Main Context.
- The
eventsandhttpdirectives reside in the Main Context,serverinhttp, andlocationinserver.
- The
Blocks
- Nginx divides the configurations meant to serve different content into Blocks, which live in a hierarchical structure.
- Each time a client request is made to the server, Nginx begins a process of determining which hierarchical block should be used to handle the request.
Server Block
- Defines a virtual server used to handle requests of a defined type
- each Server Block functions as a separate virtual web server instance
- Based on the domain name, port and IP address requested, we can configure multiple server blocks to handle each combination.
- The
server_nameandlistendirectives are used to determine which server block should be used to fulfill a request. They are used to bind to tcp sockets.- With
listen, we can use a lone IP, a lone port, or a combo of the two. If we only specify one, then defaults are used- default port: 80
- default IP: 0.0.0.0
- the
server_namedirective is only evaluated when nginx needs to distinguish between server blocks that match to the same level of specificity in thelistendirective. Put another way, it is a "specificity tie-breaker"- in other words, if
example.comis hosted on192.168.1.10:80, a request will always be served by a server block that specifieslisten 192.168.1.10, even if there is another server block that specifiesserver_name example.com
- in other words, if
- Finally, if further specificity is needed, then the Host header from the request (which contains the URI that the client was trying to reach) is used.
- When using wildcard matching, the longest match beginning with a wildcard is used
- ex. if the request has a Host header of
www.example.com, and we have 3 server blocks withserver_nameof*.example.com,www.example.*and*.com,*.example.comwould win out.
- ex. if the request has a Host header of
- When using wildcard matching, the longest match beginning with a wildcard is used
- With
- With server blocks, we can run more than one website on a single host
- in Apache, called VirtualHost
Location Block
- Lives within a Server Block (or nested in other location blocks).
- Determine how Nginx should handle the part of the request that comes after IP:port (ie. the URI).
- Similar to how Nginx has a specificity-based process for determining which server block will process the request, Nginx has an algorithm to determine which location block within the server should be used for handling requests.
- Location blocks take the following form:
location <optional_modifier> <location_match> {
}
- The
location_matchdefines what Nginx should check the request URI against. - The
optional_modifieraffects the way Nginx will attempt to match the location block.- ex. check for prefix match (default), check for exact match (
=), check for case-sensitive Regex (~)
- ex. check for prefix match (default), check for exact match (
- The URI specified after
locationwill be added to the path specified in the root directive- ex. if we specify
root /var/www/and the location block specifies/images/, then the path to the requested file on the local FS will be/var/www/images
- ex. if we specify
- ex. Imagine we had a server block:
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
in response to a request with URI starting with /images/, the server will send files from the /data/images directory.
Tasks of Nginx
Serving Static Content
- Nginx can be configured to serve static content, such as HTML and images.
- this involves setting up of a server block inside the http block with two location blocks.
- multiple server blocks are able to be added, each specifying a different port.
http {
server {
}
}
Reverse Proxy Server
- When Nginx proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client
- it is possible to proxy requests to another HTTP server (eg. another Nginx server) or to a non-HTTP server (eg. express.js)
- We use a specified protocol like FastCGI to do this
- it is possible to proxy requests to another HTTP server (eg. another Nginx server) or to a non-HTTP server (eg. express.js)
- We can establish a proxy server by using the
proxy_passdirective within a location block.- The value of
proxy_passis the address of the proxy server:
- The value of
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
- In this config, all requests processed to
/some/path/to be sent to the proxy server athttp://www.example.com/link/.- ex. the request with the URI of
/some/path/page.htmlwill be proxied tohttp://www.example.com/link/page.html
- ex. the request with the URI of
- to pass a request to a non-HTTP server, the appropriate
*_passdirective should be used- ex.
fastcgi_pass
- ex.
Debugging
- upon changing nginx.conf, we need to reload the nginx server with
nginx -s reload. - logs are stored at either
/usr/local/nginx/logsor/var/log/nginx
Gotchas
- A root directive should occur outside of the location block. We can then use another root directive within a location block if we want to override it.
- Conversely, if you were to add a root to every location block then a location block that isn’t matched will have no root. Therefore, it is important that a root directive occur prior to your location blocks, which can then override this directive if they need to.
- source
Children
Backlinks