User Tools

Site Tools


nginxarchitecture

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nginxarchitecture [2017/03/24 05:58] – [PHP-FPM Config and Optimize] adminnginxarchitecture [2024/10/27 02:55] (current) – [Basic Nginx Configuration] admin
Line 26: Line 26:
   * Since Nginx comes only with core features that are required for a web server, **it is lightweight when compared to Apache**.   * Since Nginx comes only with core features that are required for a web server, **it is lightweight when compared to Apache**.
   * The performance and scalability of Nginx is not completely dependent on hardware resources, whereas the performance and scalability of the Apache is dependent on underlying hardware resources like memory and CPU.   * The performance and scalability of Nginx is not completely dependent on hardware resources, whereas the performance and scalability of the Apache is dependent on underlying hardware resources like memory and CPU.
 +===== Basic Nginx Configuration =====
 +Some basic directives:
 +  * Location (refer: https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/)<code ini>
 +user nobody; # a directive in the 'main' context
 +
 +events {
 +    # configuration of connection processing
 +}
 +
 +http {
 +    # Configuration specific to HTTP and affecting all virtual servers
 +
 +    server {
 +        # configuration of HTTP virtual server 1
 +        location /one {
 +            # configuration for processing URIs starting with '/one'
 +        }
 +        location /two {
 +            # configuration for processing URIs starting with '/two'
 +        }
 +    }
 +
 +    server {
 +        # configuration of HTTP virtual server 2
 +    }
 +}
 +
 +stream {
 +    # Configuration specific to TCP/UDP and affecting all virtual servers
 +    server {
 +        # configuration of TCP virtual server 1
 +    }
 +}
 +</code>
 +  * request_filename (refer: https://docs.nginx.com/nginx/admin-guide/web-server/web-server/)<code>
 +if (!-e $request_filename) { rewrite ^ /path/index.php last; }
 +</code> => This checks whether there is a file, directory, or symbolic link that **matches the $request_filename**. If such a file is **not found**, the connection is being **redirected to /path/index.php**.
 +  * try_files (refer https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/)<code>
 +location / {
 +    try_files $uri $uri/ $uri.html =404;
 +}
 +</code><code>
 +location / {
 +    try_files $uri $uri/ /test/index.html;
 +}
 +</code> => you probably understand:<code>
 +location / => matches all locations**
 +try_files $uri =>try $uri first, for example http://example.com/images/image.jpg nginx will try to check if there's a file inside /images called image.jpg if found it will serve it first.
 +$uri/=> which means if you didn't find the first condition $uri try the URI as a directory
 +</code>
 ===== Optimize nginx configuration for performance and benchmark ===== ===== Optimize nginx configuration for performance and benchmark =====
 refer: refer:
 +  * http://www.freshblurbs.com/blog/2015/11/28/high-load-nginx-config.html
   * http://tweaked.io/guide/nginx/   * http://tweaked.io/guide/nginx/
   * http://wiki.nginx.org/FullExample   * http://wiki.nginx.org/FullExample
Line 138: Line 189:
   * benchmark load speed from other countries from http://www.webpagetest.org/   * benchmark load speed from other countries from http://www.webpagetest.org/
 => with above benchmarch for static file ab -n 20 -c 4 http://shop.babies.vn/media/js/af1ae2e07ff564e3d1499f7eb7aecdf9.js(size 400k), we can see the problem for performance because the network or bandwidth => with above benchmarch for static file ab -n 20 -c 4 http://shop.babies.vn/media/js/af1ae2e07ff564e3d1499f7eb7aecdf9.js(size 400k), we can see the problem for performance because the network or bandwidth
 +
 +===== Nginx Rewrite Rules and Regular Expression =====
 +refer: https://blog.nginx.org/blog/creating-nginx-rewrite-rules
 +<code>
 +# -----------------------------------------------------------------------------------------
 +#         : Enable regex mode for location (in regex ~ mean case-sensitive match)
 +#     ~*    : case-insensitive match
 +#         : Or
 +#     ()    : Match group or evaluate the content of ()
 +#         : the expression must be at the end of the evaluated text 
 +#             (no char/text after the match) $ is usually used at the end of a regex 
 +#             location expression. 
 +#     ?     : Check for zero or one occurrence of the previous char ex jpe?g
 +#     ^~    : The match must be at the beginning of the text, note that nginx will not perform 
 +#             any further regular expression match even if an other match is available 
 +#             (check the table above); ^ indicate that the match must be at the start of 
 +#             the uri text, while ~ indicates a regular expression match mode.
 +#             example (location ^~ /realestate/.*)
 +#             Nginx evaluation exactly this as don't check regexp locations if this 
 +#             location is longest prefix match.
 +#         : Exact match, no sub folders (location = /)
 +#         : Match the beginning of the text (opposite of $). By itself, ^ is a 
 +#             shortcut for all paths (since they all have a beginning).
 +#     .*    : Match zero, one or more occurrence of any char
 +#         : Escape the next char
 +#         : Any char 
 +#         : Match zero, one or more occurrence of the previous char
 +#         : Not (negative look ahead)
 +#     {}    : Match a specific number of occurrence ex. [0-9]{3} match 342 but not 32
 +#             {2,4} match length of 2, 3 and 4
 +#         : Match one or more occurrence of the previous char 
 +#     []    : Match any char inside
 +# --------------------------------------------------------------------------------------------
 +</code>
 ===== PHP-FPM Config and Optimize ===== ===== PHP-FPM Config and Optimize =====
 refer:  refer: 
   * https://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/   * https://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/
-  * http://jeremymarc.github.io/2013/04/22/nginx-and-php-fpm-for-performance/+  * https://tweaked.io/guide/nginx/
  
 **global** config for all pools:<code> **global** config for all pools:<code>
Line 203: Line 288:
 /etc/php-fpm.d/blog.conf /etc/php-fpm.d/blog.conf
 /etc/php-fpm.d/forums.conf /etc/php-fpm.d/forums.conf
-</code> +</code> 
 +Or config in php-fpm.conf<code> 
 +; Relative path can also be used. They will be prefixed by: 
 +;  - the global prefix if it's been set (-p argument) 
 +;  - /onec/php otherwise 
 +;include=etc/fpm.d/*.conf 
 +</code>(Create directory **/onec/php/etc/fpm.d/**)
 Just example configurations for every pool:  Just example configurations for every pool: 
 +  * default pool [www](listen on port 9000)<code>
 +[www]
 +
 +; Per pool prefix
 +; It only applies on the following directives:
 +; - 'access.log'
 +; - 'slowlog'
 +; - 'listen' (unixsocket)
 +; - 'chroot'
 +; - 'chdir'
 +; - 'php_values'
 +; - 'php_admin_values'
 +; When not set, the global prefix (or /onec/php) applies instead.
 +; Note: This directive can also be relative to the global prefix.
 +; Default Value: none
 +;prefix = /path/to/pools/$pool
 +
 +; Unix user/group of processes
 +; Note: The user is mandatory. If the group is not set, the default user's group
 +;       will be used.
 +user = nobody
 +group = nobody
 +
 +; The address on which to accept FastCGI requests.
 +; Valid syntaxes are:
 +;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
 +;                            a specific port;
 +;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
 +;                            a specific port;
 +;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
 +;                            specific port;
 +;   '[::]:port'            - to listen on a TCP socket to all addresses
 +;                            (IPv6 and IPv4-mapped) on a specific port;
 +;   '/path/to/unix/socket' - to listen on a unix socket.
 +; Note: This value is mandatory.
 +listen = 127.0.0.1:9000
 +
 +</code>
   * /etc/php-fpm.d/site.conf<code>    * /etc/php-fpm.d/site.conf<code> 
 [site] [site]
nginxarchitecture.1490335089.txt.gz · Last modified: 2022/10/29 16:15 (external edit)