General purpose web server. Uses asynchronous event-driven approach to handling requests. Can handle high volumes of connections, with low memory footprint.
Nginx is often set up as a reverse proxy solution, scaling infrastructures or passing requests to other servers not designed to handle large client loads.
Nginx is designed to handle many concurrent connections.
Proxying is accomplished by passing requests to other servers for actual processing, which relays back through Nginx to the client.
nginx -s signal
where signals arestop
fast shutdown, quit
graceful shutdown, reload
conf, reopen
log files;
event
context sets global options that affect how Nginx handles connectionshttp
context holds all configuration regarding http connections (as a web server/reverse proxy)server
context is declared within the http
contextlocation
contexts are nested in server
, catch subsets of traffic and directs it to the proper files to be served/data/www
contains html files and other assetsserver
block inside the http
blockserver
blocks, distinguished by ports (listen
, default 80) and server_name
(domain name)Example:
http{
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
Nginx uses blocks to build hierarchical configuration structure.
Nginx determines which server block to use with listen
and server_name
listen
directive can be set to an IP address/port combo, lone IP address with default port 80, lone port listening to every interface on that port, or the path to a unix socketserver_name
directive is only evaluated if it needs to distinguish between server blocks that match the same level of specificity in the listen
directivelocation directives look like: location <optional_modifier> <location_match> {}
=
, the location match will be considered if matching exactly~
, the location will be interpreted as a regex~*
, case-insensitive regex^~
, if selected as the best non-regular expression match, regex matching will not take place (modifies the specific prefix-matching location context have precedence over regular expression locations)Nginx evaluates the possible location contexts by comparing the request URI to each of the locations.
=
), if found, it is used immediately^~
modifier, then Nginx will immediately use that locationChmod 777
. On web-servers, this give anyone rwx permissions on all filesroot
directive inside a location blockindex
under http
block)if
directive inside a location contextthe only safe things inside if in location context are return
and rewrite
if ($something) {
return 418;
}
if($host ... )
, inefficient, checks host header for every requestserver_name
directive to differentiate responses based on the Host headerif
to check files, use try_file
directivetry_files $uri $uri/ /index.html;
check $uri
, then $uri/
, otherwise use /index.html
rewrites
rewrite ^/(.*)$ http://example.com/$1 permanent;
rewrite ^ http://example.com$request_uri? permanent;
return 301 http://example.com$request_uri;
listen
directive, use known IP addresses if possible instead/etc/nginx/mime.types
holds media types (derrived from Multipurpose internet mail extensions)default_type
directive defines what to send when you have no file extensionsuser
directive defines user and group credentials used by worker processesdebug_connection
directive enables debugging log for a given clienterror_log
defines path and severitysendfile