Nginx reverse proxy functionality

Article Directory

Nginx reverse proxy functionality

Reverse Proxy: a reverse proxy also called reverse proxy, the proxy refers to the external network to the internal user's request specifies web server, and return the data to the user a way, this is a way to more .

Nginx addition to providing a high-performance enterprise web services outside also may not have to forward the request itself by some other predefined protocol to the server process, a different protocol is a Nginx server to communicate with other servers specification species, mainly in the different scenarios for different functions of the following modules

  • ngx_http_proxy_module: the client's requests are forwarded to the http protocol to specify the server for processing
  • ngx_stream_proxy_module: The client requests forwarded to the specified server process to tcp protocol.
  • ngx_http_fastcgi_module: php client requests to be forwarded to a designated server assistant to fastcgi protocol.
  • ngx_http_uwsgi_module: Python client request for server processing is forwarded to the specified protocol to uwsgi.

Logical call relationship

mark

1, to achieve reverse proxy http

Requirements: The user forwards the request to the domain www.mage.net value of the back-end server processes, official documents

192.168.1.102 #Nginx 代理服务器
192.168.1.103 #后端web A,Apache部署
192.168.1.104 #后端web B,Apache部署

Requirements: The user forwards the request to the domain www.mage.net value of the back-end server processes, official documents

Deploy the back-end server Apache 1.1

root@z3:~# apt install apache2 -y
root@Z4:~# apt install apache2 -y
root@z3:~# echo "web1 192.168.1.103" > /var/www/html/index.html
root@Z4:~# echo "web2 192.168.1.104" > /var/www/html/index.html
root@z3:~# curl http://192.168.1.103
web1 192.168.1.103
root@z3:~# curl http://192.168.1.104
web2 192.168.1.104

1.2. Nginx http reverse proxy entry

Official Documents

1.2.1 reverse proxy configuration parameters

  1. proxy_pass;

    # Is used to set the client request to the backend server host forwarding, can be a host name, IP address: port mode, the agent can also be pre-set to the host group, you need module gx_http_upstream_module support.

    location /web {
        index index.html;
        proxy_pass http://192.168.1.103:80;
        
        #不带斜线将访问的/web,等于访问后端服务器 http://192.168.7.103:80/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问,这是一个追加/web到后端服务器http://servername:port/WEB/INDEX.HTML的操作
        
        proxy_pass http://192.168.1.103:80/;
        #带斜线,等于访问后端服务器的http://192.168.7.103:80/index.html 内容返回给客户端。通常不加斜杠
    }
    
  2. proxy_hide_header

    # Nginx as a reverse proxy for the time, the http response back to the client, when to hide the backend service information corresponding to the version of the head, may be provided in http / server or block location:

    Example: Hidden ETag information

    location /web {
        index index.html;
        proxy_pass http://192.168.1.103:80/;
        proxy_hide_header ETag;
    }
    
  3. proxy_pass_request_body on | off;
    #是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,默认即为开启
    
  4. proxy_pass_request_headers on | off;
    #是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
    
  5. proxy_set_header;
    #可以更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部,如下:
    
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header HOST $remote_addr;
    #添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址。
    
  6. proxy_hide_header field;
    #用于隐藏后端服务器特定的响应首部,默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等
    
  7. proxy_connect_timeout time;
    #配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下:
    proxy_connect_timeout 60s;
    #60s为自定义nginx与后端服务器建立连接的超时时间
    
  8. proxy_read_time time;
    #配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
    proxy_send_time time;
    #配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间,默认60s
    
  9. proxy_http_version 1.0;
    #用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
    
  10. proxy_ignore_client_abort off;
    #当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求并立即记录499日志,默认为off。
    
  11. proxy_headers_hash_bucket_size 64;
    #当配置了 proxy_hide_header和proxy_set_header的时候,用于设置nginx保存HTTP报文头的hash表的上限。
    
  12. proxy_headers_hash_max_size 512;
    #设置proxy_headers_hash_bucket_size的最大可用空间
    
  13. server_namse_hash_bucket_size 512;
    #server_name hash 表申请空间大小
    
  14. server_names_hash_max_szie 512;
    #设置服务器名称hash表的上限大小
    

1.2.2: Reverse Proxy example - a single web server

server {
    listen 80;
    server_name www.mage.net;
    location / {
        proxy_pass http://192.168.1.103:80;
    }
}

verification

root@z2:~# curl   http://www.mage.net/
web1 192.168.1.103

1.2.3: Reverse Proxy Example - designated location

server {
    listen 80;
    server_name www.magedu.net;
    location / {
        index index.html index.php;
        root /data/nginx/html/pc;
    }
    location /web {
        #proxy_pass http://192.168.1.103:80/; #注意有后面的/,
        proxy_pass http://192.168.1.104:80/;
    }
}
root@z2:~# curl  http://www.mage.net/web
web2 192.168.1.104

When there is no "/"

proxy_pass http://192.168.1.104:80

root@Z4:~# mkdir /var/www/html/web
root@Z4:~# echo "web2 page for apache" > /var/www/html/web/index.html
root@z3:~# mkdir /var/www/html/web
root@z3:~# echo "web1 page for apache" > /var/www/html/web/index.html

verification

root@z2:~# curl   http://www.mage.net/web
web2 page for apache

1.2.4: Reverse Proxy Example - caching function

Caching feature turned off by default

1. proxy_cache zone
proxy_cache zone | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
2. proxy_cache_key
proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;
3. proxy_cache_valid
proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
4. Define the proxy cache can be used for functions
Syntax:	proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default:	—
Context:	http

Example:

Cache configuration definition information at http

proxy_cache_path /var/cache/nginx/proxy_cache 
#定义缓存保存路径,proxy_cache会自动创建

levels=1:2:2 
#定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=1048576个目录

keys_zone=proxycache:20m 
#指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)

inactive=120s; 
#缓存有效时间

max_size=1g;
#最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
5. Call caching feature

Needs to be defined in the appropriate configuration section, such as server {...}; location, or the like

proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
6. proxy_cache_use_stale
proxy_cache_use_stale;
#在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端,proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |http_502 | http_503 | http_504 | http_403 | http_404 | off ; 
#默认是off
7. proxy_cache_methods
proxy_cache_methods GET | HEAD | POST ...;
#对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
8. proxy_set_header

​ Context: http, server, location

proxy_set_header field value;
#设定发往后端主机的请求报文的请求首部的值

example:

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Example 9. Cache: Cache opening comparison
9.1 pressure measurement scenario uncached
location / {
    proxy_pass http://192.168.1.104:80 ;
         }
root@z2:/data/nginx/html/pc# ll test.html 
-rw-r--r-- 1 root root 1086284 Mar 12 11:48 test.html
root@z2:/data/nginx/html/pc# scp test.html  192.168.1.104:/var/www/html/log.html
root@z2:~# ab -n20000 -c1000  http://www.mage.net/log.html
Total transferred:      9525336905 bytes
HTML transferred:       9521011012 bytes
Requests per second:    160.96 [#/sec] (mean)
Time per request:       6212.620 [ms] (mean)
Time per request:       6.213 [ms] (mean, across all concurrent requests)
Transfer rate:          74864.44 [Kbytes/sec] received
9.2 Preparation cache configuration
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
#配置在nginx.conf http配置段
location / { 
    proxy_pass http://192.168.1.104:80/;
    proxy_set_header clientip $remote_addr;
    proxy_cache proxycache;
	proxy_cache_key $request_uri;
	proxy_cache_valid 200 302 301 1h;
	proxy_cache_valid any 1m;
}
And then measuring 9.3
root@z2:~# ab -n20000 -c1000  http://www.mage.net/log.html
Total transferred:      21204383300 bytes
HTML transferred:       21198914710 bytes
Requests per second:    1828.46 [#/sec] (mean)
Time per request:       546.910 [ms] (mean)
Time per request:       0.547 [ms] (mean, across all concurrent requests)
Transfer rate:          1893128.21 [Kbytes/sec] received

Indeed enhance

root@z2:~# tree /data/nginx/proxycache/
/data/nginx/proxycache/
└── 2
    └── 9
        └── d
            └── 8af02a9e0af8a09cc963ec4cc0257d92

3 directories, 1 file

1.2.5 Add the message header information

nginx-based module ngx_http_headers_module can be achieved on the head packets Adds the specified key and value, official documents

Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location

Add custom headers, as follows,

add_header name value [always];
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
add_trailer name value [always];

Adding custom response trailer information after version 1.13.2 Support

nginx configuration

location /web {
    proxy_pass http://192.168.1.103:80/;
    proxy_set_header clientip $remote_addr;
    proxy_cache proxycache;
    proxy_cache_key $request_uri;
    proxy_cache_valid 200 302 301 1h;
    proxy_cache_valid any 1m;
    add_header X-Via $server_addr;
	add_header X-Cache $upstream_cache_status;
	add_header X-Accel $server_name;
}

mark

mark

1.3: Nginx http reverse proxy advanced applications:

In a section on the Nginx may forward the client's request to a single server, but can not be forwarded to the backend server a specific group, and can not provide the appropriate back-end server status monitoring server, but may be provided based ngx_http_upstream_module module Nginx server packet forwarding, weight distribution, advanced functional status monitoring, scheduling and other official documents

1.3.1: http upstream configuration parameters

upstream name {
}
#自定义一组服务器,配置在http内
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1。

max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制。

max_fails=number #对后端服务器连续监测失败多少次就标记为不可用。

fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒。

backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器。

down #标记为down状态。

resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx。
hash KEY [consistent];
#基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。所谓取模运算,就是计算两个数相除之后的余数,比如10%7=3, 7%4=3

hash $request_uri consistent; #基于用户请求的uri做hash
ip_hash
源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持,
least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器

1.3.2: Reverse Proxy Example - Multiple web servers

upstream webserver {
    #hash $request_uri consistent;
    #ip_hash;
    #least_conn;
    server 192.168.1.103:80 weight=1 fail_timeout=5s max_fails=3; #后端服务器状态监测
    server 192.168.1.104:80 weight=1 fail_timeout=5s max_fails=3;
    server 192.168.1.101:80 weight=1 fail_timeout=5s max_fails=3 backup;
}

server {
    listen 80;
    server_name www.magedu.net;
    location / {
        index index.html index.php;
        root /data/nginx/html/pc;
    }
    location /web {
        index index.html;
        proxy_pass http://webserver/;       
    }
}
web2 192.168.1.104
root@z2:~# curl http://www.mage.net/web/
web1 192.168.1.103
root@z2:~# curl http://www.mage.net/web/
web2 192.168.1.104
root@z2:~# curl http://www.mage.net/web
web1 192.168.1.103
root@z2:~# curl http://www.mage.net/web
web2 192.168.1.104

note:

If written proxy_pass http: // webserver of this without '' / 'words. curl http://www.mage.net/web status code 30X, curl -L http://www.mage.net/web job

1.3.3: Reverse Proxy Example - Client IP passthrough

location /web {
    index index.html;
    proxy_pass http://webserver/;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    #添加客户端IP到报文头部
}
vim /etc/apache2/apache2.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Then restart apache, visit http://ww.mage.net/web/ test

Apache format verification services

192.168.1.101 192.168.1.102 - - [14/Mar/2020:23:01:50 +0800] "GET /web/ HTTP/1.0" 200 267 "-" "curl/7.58.0"
192.168.0.1 192.168.1.102 - - [14/Mar/2020:23:03:11 +0800] "GET /web/ HTTP/1.0" 200 267 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"

If nginx as a web server, logformat without modification

Own

"$http_x_forwarded_for"

2 achieve Nginx tcp load balancing

Nginx in the domain name 1.9.0 version began to support tcp load balancing mode, the 1.9.13 version began to support the load udp protocol, udp mainly used for DNS resolution, its configuration and http proxy instructions and similar, based on ngx_stream_proxy_module module tcp achieve load, to achieve additional back-end server based on the module ngx_stream_upstream_module packet forwarding, weight distribution, status monitoring, scheduling algorithm advanced features.

2.1 tcp load balancing configuration parameters

stream { #定义stream
    upstream backend { #定义后端服务器
        hash $remote_addr consistent; #定义调度算法
        server backend1.example.com:12345 weight=5; #定义具体server
        server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }
    upstream dns { #定义后端服务器
        server 192.168.0.1:53535; #定义具体server
        server dns.example.com:53;
}
    server { #定义server
        listen 12345; #监听IP:PORT
        proxy_connect_timeout 1s; #连接超时时间
        proxy_timeout 3s; #转发超时时间
        proxy_pass backend; #转发到具体服务器组
    }
    server {
        listen 127.0.0.1:53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }
    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
}

2.2 load balancing instance -Redis

redis configuration

root@Z4:~# apt install redis
vim /etc/redis/redis.conf
bind 0.0.0.0

Restart redis

root@Z4:~# ss -ntl |grep 6379
LISTEN   0         128                 0.0.0.0:6379             0.0.0.0:*  

nginx configuration

root@z2:~# mkdir /apps/nginx/conf/tcp
root@z2:~# vim /apps/nginx/conf/tcp/tcp.conf
stream {
    upstream redis_server {
        #hash $remote_addr consistent;
        server 192.168.104:6379 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 192.168.102:6379;
        proxy_connect_timeout 3s;
        proxy_timeout 3s;
        proxy_pass redis_server;
    }
}

note http stream position and trim bits

root@z2:~# vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/tcp.conf;

test:

root@z1:~# apt install redis-tools
root@z1:~# redis-cli -h 192.168.1.102
192.168.1.102:6379> 

2.3 Load Balancing instance: MySQL

mysql settings

root@z3:~# apt install maraidb-server
root@z3:~# vim /etc/mysql/mariadb.conf.d/50-server.cnf
bind-address            = 0.0.0.0
root@z3:~# systemctl restart  mariadb

And set the mysql password

MariaDB [(none)]> create user 't'@'192.168.1.102' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

nginx configuration

stream {
    upstream redis_server {
        server 192.168.1.104:6379 max_fails=3 fail_timeout=30s;    
    }
    upstream mysql_server {
        least_conn;
            server 192.168.1.103:3306 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 192.168.1.102:3306;
        proxy_connect_timeout 6s;
        proxy_timeout 15s;
        proxy_pass mysql_server;
    }
    server {
        listen 192.168.1.102:6379;
        proxy_connect_timeout 3s;
        proxy_timeout 3s;
        proxy_pass redis_server;
    }
}  

Restart nginx and access test

root@z1:~# mysql  -ut  -h192.168.1.103 -p123456 ;
ERROR 1130 (HY000): Host '192.168.1.101' is not allowed to connect to this MariaDB server
root@z1:~# mysql  -ut  -h192.168.1.102 -p123456 ;
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

3, to achieve FastCGI

3.1 FastCGI configuration directives

Based on Nginx module ngx_http_fastcgi_module fastcgi protocol specified by the client request is forwarded to php-fpm processing instructions configured as follows

Basic instructions
fastcgi_pass address;
#转发请求到后端服务器,address为后端的fastcgi server的地址,可用位置:location, if in location 

fastcgi_index name;
#fastcgi默认的主页资源,示例:fastcgi_index index.php;
fastcgi_param parameter value [if_not_empty];
#设置传递给FastCGI服务器的参数值,可以是文本,变量或组合,可用于将Nginx的内置变量赋值给自定义key

fastcgi_param REMOTE_ADDR $remote_addr; 
#客户端源IP

fastcgi_param REMOTE_PORT $remote_port;
#客户端源端口

fastcgi_param SERVER_ADDR $server_addr;
#请求的服务器IP地址

fastcgi_param SERVER_PORT $server_port;
#请求的服务器端口

fastcgi_param SERVER_NAME $server_name;
#请求的server name

Nginx default configuration examples

location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
    #默认脚本路径include fastcgi_params;
}
Definition instruction cache
Syntax:	fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default:	—
Context:	http
定义fastcgi的缓存
path #缓存位置为磁盘上的文件系统路径

max_size=size #磁盘path路径中用于缓存数据的缓存空间上限

levels=levels #目录的层级数量,以及每一级的目录数量,levels=ONE:TWO:THREE,示例:leves=1:2:2

keys_zone=name:size #设置缓存名称及k/v映射的内存空间的名称及大小

inactive=time #缓存有效时间,默认10分钟,需要在指定时间满足 fastcgi_cache_min_uses 次数被视为活动缓存。
Call instruction cache
fastcgi_cache zone | off;
#调用指定的缓存空间来缓存数据,可用位置:http, server, location

fastcgi_cache_key string;
#定义用作缓存项的key的字符串,示例:fastcgi_cache_key $request_uri;

fastcgi_cache_methods GET | HEAD | POST ...;
#为哪些请求方法使用缓存

fastcgi_cache_min_uses number;
#缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项

fastcgi_keep_conn on | off;
#收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接

fastcgi_cache_valid [code ...] time;
#不同的响应码各自的缓存时长

fastcgi_hide_header field; #隐藏响应头指定信息

fastcgi_pass_header field; #返回响应头指定信息,默认不会将Status、X-Accel-...返回

Example 3.2 FastCGI -Nginx with the same server php-fpm

php installation can be compiled or installed by apt, apt to install using relatively simple, easy to build and install more custom parameters or options.

3.2.1: php environment ready

root@z2:~# apt install php-fpm php-mysql -y

The default configuration of php-fpm

root@z2:~# grep "^[a-Z]"   /etc/php/7.2/fpm/php-fpm.conf 
pid = /run/php/php7.2-fpm.pid
error_log = /var/log/php7.2-fpm.log
include=/etc/php/7.2/fpm/pool.d/*.conf
root@z2:~# grep "^[a-Z]"     /etc/php/7.2/fpm/pool.d/www.conf
user = www-data
group = www-data
listen = /run/php/php7.2-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

3.2.2 optimization settings

[www]
listen = 127.0.0.1:9000 #监听地址及IP
listen.allowed_clients = 127.0.0.1 #允许客户端从哪个源IP地址访问,要允许所有行首加 ;注释即可
user = nginx #php-fpm启动的用户和组,会涉及到后期文件的权限问题
group = nginx
pm = dynamic #动态模式进程管理
pm.max_children = 500 #静态方式下开启的php-fpm进程数量,在动态方式下他限定php-fpm的最大进程数
pm.start_servers = 100 #动态模式下初始进程数,必须大于等于pm.min_spare_servers和小于等于pm.max_children的值。
pm.min_spare_servers = 100 #最小空闲进程数
pm.max_spare_servers = 200 #最大空闲进程数
pm.max_requests = 500000 #进程累计请求回收值,会重启
pm.status_path = /pm_status #状态访问URL
ping.path = /ping #ping访问动地址
ping.response = ping-pong #ping返回值
slowlog = /var/log/php-fpm/www-slow.log #慢日志路径
php_admin_value[error_log] = /var/log/php-fpm/www-error.log #错误日志
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files #phpsession保存方式及路径
php_value[session.save_path] = /var/lib/php/session #当时使用file保存session的文件路径

End change restart:

root@z2:~# systemctl restart php7.2-fpm

== Note: == Minimum maximum number of processes often set to the same number, the process of reducing the frequent opening and closing of cpu overhead caused

3.2.3 ready to test php page

root@z2:~# mkdir /data/nginx/php
root@z2:~# vim  /data/nginx/php/index.php
<?php
phpinfo();
?>

3.2.4 Nginx configuration Forward:

After the installation is complete Nginx generate a default configuration file with fastcgi, usually stored in the conf directory of the installation path nginx among such /apps/nginx/conf/fastcgi.conf,/apps/nginx/conf/fastcgi_params.

location ~ \.php$ {
    root /data/nginx/php; #$document_root调用root目录
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
   
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param SCRIPT_FILENAME /data/nginx/php$fastcgi_script_name;
	#如果SCRIPT_FILENAME是绝对路径则可以省略root /data/nginx/php;  
    
    include fastcgi_params;
}

note:$ Document_root that refers to the location of the root directory

3.2.5 access validation test php page

mark

3.2.6 php-fpm operational status page

Access to the configuration file inside the specified path, returns the current operating status of php-fpm.

Nginx configuration

(Www.conf provided in pm.status_path = / pm_status)

location ~ ^/(pm_status|ping)$ {
    #access_log off;
    #allow 127.0.0.1;
    #deny all;
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}

Restart Nginx and test

root@z2:~# curl http://www.mage.net/pm_status
pool:                 www
process manager:      dynamic
start time:           15/Mar/2020:13:39:44 +0800
start since:          5
accepted conn:        1
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       1
active processes:     1
total processes:      2
max active processes: 1
max children reached: 0
slow requests:        0
root@z2:~# curl http://www.mage.net/ping
pongroot@z2:~# 
Published 62 original articles · won praise 7 · views 1255

Guess you like

Origin blog.csdn.net/qq_36801585/article/details/104880617