Web server cluster - Nginx dynamic website architecture (note)

1, Fastcgi & PHP FPM

Static website: static elements that the Nginx server can handle html .jpg .mp4 .css

1.1 Nginx

ngx_fastcgi_modul

  • fastcgi fast universal Gateway Interface
  • Interface for handling dynamic requests
  • Nginx through NGX_ fastcgi_ The module links PHP FPM to handle dynamic requests

1.2 PHP

php-fpm

  • Php-fpm (fastcgi process manager) is a PHP GastCGI manager
  • PHP accepts the dynamic access request of the foreground nginx through PHP FPM. For example, after making a query request to the backend MySQL, PHP returns the query result to the foreground nginx

1.3 PHP-MySQL

  • php MySQL is an interface program between php and mysql

1.4 MySQL

  • Store data

1.5 interview questions

  • What is FastCGI
  • Operation principle of Nginx+FastCGI
  • The operation principle of LNMP: the user requests from the website. When requesting static elements, Nginx will independently return to the user. When it is dynamic and static, it will connect the php program through fastcgi module to convey the dynamic request. After the php program receives its request through fpm interface manager, it will store data to MySQL database through php MySQL program (oral description).

2, PHP-FPM optimization

2.1 understand PHP FPM related configuration files

2.1.1 core configuration file

[root@lnmp ~]# vim /etc/php.ini
;date.timezone =                    date.timezone=PRC set up PHP Time zone
;open_basedir =                     oepn_basedir
  • open_basedir: sets the directory that PHP scripts are allowed to access
  • open_basedir limits the files that php can open to the specified directory tree, including the file itself, when the program wants to use, such as fopen() or file_ get_ When contents () opens a file, the location of the file will be checked. When the file is outside the specified directory tree, the program will refuse to open it

2.1.2 global configuration file

[root@lnmp ~]# vim /etc/php-fpm.conf
pid = /run/php-fpm/php-fpm.pid          #pid storage location
error_log = /var/log/php-fpm/error.log  #Log storage location
;log_level = notice                     #log level
daemonize = yes                         #Daemon, turn fpm to the background, and allow default no

2.1.3 extension profile

[root@lnmp ~]# vim /etc/php-fpm.d/www.conf
user = nginx
listen.allowed_clients = 127.0.0.1  #Monitor who is allowed to access, such as separate deployment, and fill in the IP address of the other party
listen = 127.0.0.1:9000             #fpm listening port, that is, the address processed by php in nginx. It is generally the default value. The available format is' IP: port '
slowlog = /var/log/php-fpm/$pool-slow.log #Slow query log

pm = dynamic                      #Dynamic mode process management on
pm.start_servers = 5              #How many processes were initially started
pm.min_spare_servers = 5          #The minimum number of redundant processes and the minimum number of idle processes. User access will consume processes. In order to meet the follow-up services, start the processes at any time and keep the number of idle processes at 5
pm.max_children = 50              #Maximum number of processes, which limits the maximum number of processes of php program
pm.max_spare_servers = 10         #When there are more than 35 idle processes, kill the redundant processes immediately, and only keep 35 processes
pm.max_requests = 500            # Each subprocess can kill more than 500 requests immediately

2.2 initializing PHP FPM

[root@lnmp ~]# vim /etc/php-fpm.d/www.conf
#Set common values in the generation environment
pm = dynamic               #Start dynamic management mode
pm.start_servers = 32      #Start 32 processes initially
pm.max_children = 512      #Maximum number of processes (child processes will vary between the maximum and minimum decimal range)

pm.min_spare_servers = 32  #With the increase of user access, 32 idle processes are maintained
pm.max_spare_servers = 64  #As users leave, kill a large number of processes to save resources
;pm.max_requests = 1500    #Each subprocess handles 1500 requests, and the default is unlimited (1024)
[root@lnmp ~]# systemctl restart php-fpm

2.2.1 after initialization

[root@lnmp ~]# ps -aux | grep php | wc -l

2.3 start php dynamic monitoring page function

2.3.1 start test page function

[root@lnmp ~]# vim /etc/php-fpm.d/www.conf 
;pm.status_path = /status                   #Remove comments
pm.status_path = /php_status                #Change to as shown before

2.3.2 Nginx configuration page forwarding

[root@lnmp ~]# vim /etc/nginx/nginx.conf   #Add page forwarding function in Server
...
        location = /php_status{
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; 
        include fastcgi_params;
        }
...
[root@lnmp ~]# systemctl restart nginx php-fpm

2.3.3 accessing the test page

http://192.168.100.10/php_status

3, Nginx Location

Location only affects the following paths!

Enable the authentication module to be configured into the Server and apply the global

1) Generate account password file

[root@lnmp ~]# yum -y install httpd-tools #Install password generation tool
[root@lnmp ~]# htpasswd -cm /etc/nginx/conf.d/passwd user10

2) Enable identity authentication and apply it to the server

[root@lnmp ~]# vim /etc/nginx/nginx.conf
    server {
        auth_basic "nginx access test!";               #Prompt message
        auth_basic_user_file /etc/nginx/conf.d/passwd; #Reference to certification documents
...
[root@lnmp ~]# systemctl restart nginx

Add authentication to status

[root@lnmp ~]# vim /etc/nginx/nginx.conf
    server {

        location = /php_status{
        auth_basic "nginx access test!";
        auth_basic_user_file /etc/nginx/conf.d/passwd;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        include fastcgi_params;
        }
...
[root@lnmp ~]# systemctl restart nginx

You can access the home page normally
PHP after access location restrictions_ The status page needs to enter the account

3.1 Location syntax rules

location [=|~|~*|!~|!~*|^~] /uri/{
	module;
	module;
}


=   Indicates exact matching, and the priority is the highest
~   Case sensitive regular matching
~*  Case insensitive regular matching
/   Universal matching, any request will be matched
^~  Start with some strings
!~ Non (case sensitive matching regular)
!~* Non (case insensitive regular)

Precise priority matching > Fuzzy matching > Regular matching > match

3.2 Location example

Objective: To observe the priority between expressions through different expressions

[root@lnmp ~]# vim /etc/nginx/nginx.conf
#        root         /usr/share/nginx/html;
#        location / {
#       index index.php index.html;
#        }                                             #Comment the main profile section

root /abcd;                                            #Add the following test page
index index.html;

location = / { index a.html; }
location ~ / { index b.html; }
location / { index c.html; }
[root@lnmp abcd]# systemctl restart nginx

#location = / { index a.html; }
[root@lnmp abcd]# systemctl restart nginx

#location ~ / { index b.html; }
[root@lnmp abcd]# systemctl restart nginx

#location / { index c.html; }
[root@lnmp abcd]# echo index > index.html
[root@lnmp abcd]# systemctl restart nginx

Keywords: Linux

Added by kumar_ldh on Wed, 02 Feb 2022 04:38:59 +0200