Difference between revisions of "Nginx"

From Colettapedia
Jump to navigation Jump to search
Line 8: Line 8:
 
* Master process - read and evaluate configuration, and maintain worker processes
 
* Master process - read and evaluate configuration, and maintain worker processes
 
* Worker processes - do actual processing of requests; # is defined in the configuration file
 
* Worker processes - do actual processing of requests; # is defined in the configuration file
 +
 +
 
==Configuration File==
 
==Configuration File==
 
* Comments start with hash #
 
* Comments start with hash #
 +
 
===Structure===
 
===Structure===
 
* nginx modules controlled by directives
 
* nginx modules controlled by directives
Line 15: Line 18:
 
** simple directive - directive name, followed by params, ends with a semicolon
 
** simple directive - directive name, followed by params, ends with a semicolon
 
** block directives ("context") - same as simple, but instead of semicolon ends with curly braces
 
** block directives ("context") - same as simple, but instead of semicolon ends with curly braces
 +
 
===Common directives===
 
===Common directives===
 
* Regular expressions parameter arguments for location directives always start with a tilde ~ and usually are between ^ and $ opperators.
 
* Regular expressions parameter arguments for location directives always start with a tilde ~ and usually are between ^ and $ opperators.
* <code>root path;</code>
+
 
** Sets the root directory for requests.
+
 
** SIMPLE mapping URI to filepath on server.
 
** Path to the file is constructed by merely adding a URI to the value of the root directive.
 
* <code>alias path;</code>
 
** Has all the functionality of root directive, plus rewrite capabilities
 
** If location URI has a regular expression in it, you can use the <code>$1</code>, <code>$2</code>, etc capture groups in the argument to the alias directive.
 
* <code>try_files file1, [file2, ...] uri|=code;</code>
 
** Checks the existence of files in the specified order and uses the first found file for request processing
 
** The path to a file is constructed from the file parameter according to the root and alias directives.
 
 
===Contexts===
 
===Contexts===
 +
 
====events====
 
====events====
 
* top-level "main" context, cannot be within another
 
* top-level "main" context, cannot be within another
 +
 
====http====  
 
====http====  
 
* top-level "main" context, cannot be within another
 
* top-level "main" context, cannot be within another
 +
 
====server====
 
====server====
 
* Within http context only
 
* Within http context only
Line 43: Line 42:
 
** <code>semanage port -a -t http_port_t -p tcp 3200</code>
 
** <code>semanage port -a -t http_port_t -p tcp 3200</code>
 
** <code>semanage port -m -t http_port_t -p tcp 3200</code>
 
** <code>semanage port -m -t http_port_t -p tcp 3200</code>
 +
 +
  
 
====location====
 
====location====
Line 49: Line 50:
 
* The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.  
 
* The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.  
 
* location can be prefix string or regular expression, e.g., <code>location URIPREFIXARG { root /path/to/static/files;}</code>
 
* location can be prefix string or regular expression, e.g., <code>location URIPREFIXARG { root /path/to/static/files;}</code>
 +
 +
=====URI tokens=====
 
* Prefacing URI with = token sets up exact match, with search terminating immediately, speeding up processing.
 
* Prefacing URI with = token sets up exact match, with search terminating immediately, speeding up processing.
 
* Prefacing URI with ~ token means case-sensitive matching
 
* Prefacing URI with ~ token means case-sensitive matching
Line 54: Line 57:
 
* Prefacing URI with ^~ token means TURN REGULAR EXPRESSION MATCHING OFF for the given prefix.
 
* Prefacing URI with ^~ token means TURN REGULAR EXPRESSION MATCHING OFF for the given prefix.
 
* The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection.
 
* The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection.
 +
 +
 +
=====location directives=====
 +
 +
* <code>root path;</code>
 +
** Sets the root directory for requests.
 +
** SIMPLE mapping URI to filepath on server.
 +
** Path to the file is constructed by merely adding a URI to the value of the root directive.
 +
 +
 +
* <code>alias path;</code>
 +
** Has all the functionality of root directive, plus rewrite capabilities
 +
** If location URI has a regular expression in it, you can use the <code>$1</code>, <code>$2</code>, etc capture groups in the argument to the alias directive.
 +
 +
 +
* <code>try_files file1, [file2, ...] uri|=code;</code>
 +
** Checks the existence of files in the specified order and uses the first found file for request processing
 +
** The path to a file is constructed from the file parameter according to the root and alias directives.
 +
 +
 +
* <code>proxy_pass</code>
 +
* <code>fastcgi_pass</code>
 +
* <code>fastcgi_index</code>
 +
* <code>fastcgi_param</code>
 +
* <code>fastcgi_pass</code>
  
 
==Setup==
 
==Setup==

Revision as of 14:11, 28 August 2019

General

  • HTTP and reverse proxy server, mail proxy server
  • Employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes
  • docs
  • Beginner's Guide
  • Alphabetical List of Directives
  • Alphabetical index of VARIABLES - start with dollar sign $, like $uri, $document_root etc...
  • Master process - read and evaluate configuration, and maintain worker processes
  • Worker processes - do actual processing of requests; # is defined in the configuration file


Configuration File

  • Comments start with hash #

Structure

  • nginx modules controlled by directives
  • two types of directives: simple and block directives
    • simple directive - directive name, followed by params, ends with a semicolon
    • block directives ("context") - same as simple, but instead of semicolon ends with curly braces

Common directives

  • Regular expressions parameter arguments for location directives always start with a tilde ~ and usually are between ^ and $ opperators.


Contexts

events

  • top-level "main" context, cannot be within another

http

  • top-level "main" context, cannot be within another

server

  • Within http context only
  • Can have multiple server names
  • Once nginx decides which server will process the request, it tests the URI specified in the request's header against the location directives defined in the server block.
  • Order of precedence is longest to shortest prefix, so location '/' will match last, THEN regular expressions terminating on the first match
  • listen on different ports
    • systemctl restart nginx
    • etstat -tlpn| grep nginx
    • semanage port -l
    • semanage port -a -t http_port_t -p tcp 3200
    • semanage port -m -t http_port_t -p tcp 3200


location

  • server context, plus nested inside another location directive is possible, i guess?
  • Performs mapping of URL to route to static files on server, or proxy, etc.
  • The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.
  • location can be prefix string or regular expression, e.g., location URIPREFIXARG { root /path/to/static/files;}
URI tokens
  • Prefacing URI with = token sets up exact match, with search terminating immediately, speeding up processing.
  • Prefacing URI with ~ token means case-sensitive matching
  • Prefacing URI with ~* token means case-INsensitive matching
  • Prefacing URI with ^~ token means TURN REGULAR EXPRESSION MATCHING OFF for the given prefix.
  • The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection.


location directives
  • root path;
    • Sets the root directory for requests.
    • SIMPLE mapping URI to filepath on server.
    • Path to the file is constructed by merely adding a URI to the value of the root directive.


  • alias path;
    • Has all the functionality of root directive, plus rewrite capabilities
    • If location URI has a regular expression in it, you can use the $1, $2, etc capture groups in the argument to the alias directive.


  • try_files file1, [file2, ...] uri|=code;
    • Checks the existence of files in the specified order and uses the first found file for request processing
    • The path to a file is constructed from the file parameter according to the root and alias directives.


  • proxy_pass
  • fastcgi_pass
  • fastcgi_index
  • fastcgi_param
  • fastcgi_pass

Setup

  1. yum install nginx
  2. systemctl start nginx
  3. systemctl enable nginx
  4. ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
  5. yum install mariadb-server mariadb
  6. systemctl start mariadb
  7. mysql_secure_installation
  8. systemctl enable mariadb
  9. yum install php php-mysql php-fpm
  10. vi /etc/php.ini
    1. cgi.fix_pathinfo=0
  11. vi /etc/php-fpm.d/www.conf
    1. listen = /var/run/php-fpm/php-fpm.sock
    2. listen.owner = nobody
    3. listen.group = nobody
    4. user = nginx
    5. group = nginx
  12. systemctl start php-fpm
  13. systemctl enable php-fpm
  14. Configure NGINX to to PHP with fpm
  15. make a <?php phpinfo(); ?> tag and check that it works


FastCGI

PHP-fpm

  • PHP FastCGI Process Manager

Perl