Nginx反向代理详解

Nginx反向代理    
    nginx只能做反向代理服务,httpd既能做正向又能做反向代理
    反向代理时,必须有反向代理相关的模块
    从httpd服务端取到内容--->放在nginx proxy cache--->返回给客户端
    
    nginx通常用来做proxy,做httpd很少,

    下面来介绍一下nginx做反向代理模块及相关参数:
    ngx_http_proxy_module模块功能:

        The ngx_http_proxy_module module allows passing requests to another server.
    
        1、proxy_pass URL;
            Context:    location, if in location, limit_except
            
            注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;
                
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://hos[:port];
                    }
                    ...
                }
                
                http://HOSTNAME/uri --> http://host/uri
                
            proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri;
                
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://host/new_uri/;
                    }
                    ...
                }
                
                http://HOSTNAME/uri/ --> http://host/new_uri/
                
            如果location定义其uri时使用了正则表达式的模式,或在if语句或limt_execept中使用proxy_pass指令,则proxy_pass之后必须    不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;
            
                server {
                    ...
                    server_name HOSTNAME;
                    location ~|~* /uri/ {
                        proxy http://host;
                    }
                    ...
                }
                
                http://HOSTNAME/uri/ --> http://host/uri/;
                
        2、proxy_set_header field value;
            设定发往后端主机的请求报文的请求首部的值;Context:    http, server, location
            
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
        3、proxy_cache_path  缓存空间先定义后使用;指明哪种方式可以查缓存
            定义可用于proxy功能的缓存;Context:    http            
            
            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];
            
        4、proxy_cache zone(名字) | off;
            指明要调用的缓存,或关闭缓存机制;Context:    http, server, location
            
        5、proxy_cache_key string;
            缓存中用于“键”的内容;
            
            默认值:proxy_cache_key $scheme$proxy_host$request_uri;
            
        6、proxy_cache_valid [code ...] time;
            定义对特定响应码的响应内容的缓存时长;
            
            定义在http{...}中;
            proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
            
            定义在需要调用缓存功能的配置段,例如server{...};
            proxy_cache pxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
            
        7、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 ...;
            
            Determines in which cases a stale cached response can be used when an error occurs during communication     with the proxied server.
            
        8、proxy_cache_methods GET | HEAD | POST ...;
             如果客户端请求方法在这个指令中列出,那么响应将被缓存。
            
        9、proxy_hide_header field;
            默认情况下,nginx不会传递头字段“Date”、“Server”、“X-Pad”和“X-Accel-…”从代理服务器到客户机的响应。proxy_hide_header指令设置不会传递的其他字段。
            
        10、proxy_connect_timeout time;
            定义与代理服务器建立连接的超时。应该注意,这个超时通常不能超过75秒。默认为60s;最长为75s;
            
        11、proxy_read_timeout time;
            定义从代理服务器读取响应的超时。超时仅在两个连续的读取操作之间设置,而不用于传输整个响应。
            
        12、proxy_send_timeout time;
            设置向代理服务器传输请求的超时。超时仅在两个连续的写操作之间设置,而不用于传输整个请求。如果代理服务器在此期间没有接收到任何内容,则关闭连接。
                nginx与web结合只有一种方式,由于php没办法编译成nginx模块,并直接内置在nginx上自己处理动态内容,因此处niginx处理动态页面方式:nginx+php(fpmserver),nginx需要装fastcgi_module,基于此模块作为协议的客户端(fastcgi协议)
                fastcgi协议对用户并发请求的资源响应能力很有限,为了避免fcgi的局限性,将fastcgi_module换成AP(apache对于静态资源的请求,由nginx响应;动态资源请求交给AP来处理。

    ngx_http_headers_module模块功能:
        The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
        
        向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;
        
        1、add_header name value [always];
            添加自定义首部;
            
            add_header X-Via  $server_addr;
            add_header X-Accel $server_name;
            
        2、expires [modified] time;
            expires epoch | max | off;
            
            用于定义Expire或Cache-Control首部的值;
                                

    ngx_http_fastcgi_module模块功能:
        
        The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.
        
        1、fastcgi_pass address;
            address为fastcgi server的地址;    location, if in location;
            
                http://www.ilinux.io/admin/index.php --> /admin/index.php (uri)
                    /data/application/admin/index.php
                        
            
        2、fastcgi_index name;
            fastcgi默认的主页资源;
            
        3、fastcgi_param parameter value [if_not_empty];
            Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination.
            
        配置示例1:
            前提:配置好fpm server和mariadb-server服务;
                location ~* \.php$ {
                    root           /usr/share/nginx/html;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
                    include        fastcgi_params;
                }
                
        配置示例2:通过/pm_status和/ping来获取fpm server状态信息;
            location ~* ^/(pm_status|ping)$ {
                include        fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
            }            
                
        4、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];
        
            定义fastcgi的缓存;缓存位置为磁盘上的文件系统,节约网络IO资源,降低瓶颈,由path所指定路径来定义;
            
                levels=levels:缓存目录的层级数量,以及每一级的目录数量;levels=ONE:TWO:THREE
                    leves=1:2:2
                keys_zone=name:size
                    k/v映射的内存空间的名称及大小
                inactive=time
                    非活动时长
                max_size=size
                    磁盘上用于缓存数据的缓存空间上限
                
        5、fastcgi_cache zone | off;
            调用指定的缓存空间来缓存数据;http, server, location
            
        6、fastcgi_cache_key string;
            定义用作缓存项的key的字符串;
            
        7、fastcgi_cache_methods GET | HEAD | POST ...;
            为哪些请求方法使用缓存;
            
        8、fastcgi_cache_min_uses number;
            缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;
            
        9、fastcgi_cache_valid [code ...] time;
            不同的响应码各自的缓存时长;
            
            示例:
                http {
                    ...
                    fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20minactive=120s;
                    ...
                    server {
                        ...
                        location ~* \.php$ {
                            ...
                            fastcgi_cache fcgi;
                            fastcgi_cache_key $request_uri;
                            fastcgi_cache_valid 200 302 10m;
                            fastcgi_cache_valid 301 1h;
                            fastcgi_cache_valid any 1m;    
                            ...
                        }
                        ...
                    }
                    ...
                }
            
        10、fastcgi_keep_conn on | off;
            默认情况下,FastCGI服务器将在发送响应后立即关闭连接。但是,当这个指令被设置为on时,nginx将指示FastCGI服务器保持连接打开

实验环境:
Upsteam Server1 node1 192.168.10.11
Upsteam Server2 node2 192.168.10.12
nigix_proxy Server node3 外网网卡ens192:192.168.170.10 内网网卡ens224:192.168.10.254

实验拓扑图如下:

静态资源服务器
Upsteam Server1 node1 192.168.10.11/24

ntp时间同步
[root@node1 ~]# ntpdate 192.168.170.8
10 Jan 22:26:48 ntpdate[27266]: step time server 192.168.170.8 offset 496.871859 sec
安装httpd服务,提供默认测试页,启动服务及查看端口状态
[root@node1 ~]# yum -y install httpd
[root@node1 ~]# vi /var/www/html/index.html
<h1>Upsteam Server1</h1>
[root@node1 ~]# systemctl start httpd
[root@node1 ~]# ss -tunlp | grep 80
tcp    LISTEN     0      128      :::80                   :::*                   users:(("httpd",pid=21294,fd=4),("httpd",pid=21293,fd=4),("httpd",pid=21292,fd=4),("httpd",pid=21291,fd=4),("httpd",pid=21290,fd=4),("httpd",pid=16973,fd=4))
[root@node1 ~]# 

动态资源服务器
Upsteam Server2 node2 192.168.10.12/24

ntp同步时间
[root@node2 ~]# ntpdate 192.168.170.8
10 Jan 22:32:49 ntpdate[11532]: adjust time server 192.168.170.8 offset 0.014516 sec
安装httpd服务,提供默认测试页,启动服务及查看端口状态
[root@node2 ~]# yum -y install httpd
[root@node2 ~]# vi /var/www/html/index.html
<h1>Upsteam Server2</h1>
[root@node2 ~]# cd /var/www/html/
安装上传文件工具包并上传图片
[root@node2 html]# yum -y install lrzsz
[root@node2 html]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring flower.jpg...
  100%     136 KB     136 KB/sec    00:00:01       0 Errors  
[root@node2 html]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring sky.jpg...
  100%      37 KB      37 KB/sec    00:00:01       0 Errors  
[root@node2 html]# ls
flower.jpg  index.html  sky.jpg
[root@node2 ~]#systemctl start httpd
[root@node2 ~]# ss -tunlp | grep 80
tcp    LISTEN     0      128      :::80                   :::*                   users:(("httpd",pid=21294,fd=4),("httpd",pid=21293,fd=4),("httpd",pid=21292,fd=4),("httpd",pid=21291,fd=4),("httpd",pid=21290,fd=4),("httpd",pid=16973,fd=4))
[root@node2 ~]#

nigix_proxy Server node3配置

安装nginx服务,配置nginx反向代理服务
[root@node3 ~]# yum -y install nginx 
[root@node3 ~]# vi /etc/nginx/conf.d/ilinx.conf
server {
	listen 80;
	server_name www.mylinux.com;
	location / {
		proxy_pass http://192.168.10.11:80;
	}
	location ~* \.(jpg|jpeg|png)$ {
		proxy_pass http://192.168.10.12:80;
	}
}
测试语法,启动服务
[root@node3 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node3 ~]# systemctl start nginx
[root@node3 ~]# ss -tunlp | grep 80
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=20921,fd=6),("nginx",pid=20920,fd=6),("nginx",pid=20919,fd=6),("nginx",pid=20918,fd=6))
tcp    LISTEN     0      128      :::80                   :::*                   users:(("nginx",pid=20921,fd=7),("nginx",pid=20920,fd=7),("nginx",pid=20919,fd=7),("nginx",pid=20918,fd=7))
[root@node3 ~]# 

 客户端测试:可以看到静态资源在node1上,动态资源在node2上。

在nginx proxy服务器端查看客户端请求报文及各种连接参数状态。

[root@node3 ~]# tail /var/log/nginx/access.log
172.17.1.32 - - [11/Jan/2019:14:16:20 +0800] "GET /poweredby.png HTTP/1.1" 200 2811 "http://192.168.170.9/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.1.32 - - [11/Jan/2019:14:16:20 +0800] "GET /favicon.ico HTTP/1.1" 404 3650 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.1.32 - - [11/Jan/2019:14:23:24 +0800] "GET / HTTP/1.1" 200 25 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.1.32 - - [11/Jan/2019:14:23:24 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.1.32 - - [11/Jan/2019:14:23:44 +0800] "GET /sky.jpg HTTP/1.1" 200 38831 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.1.32 - - [11/Jan/2019:14:25:21 +0800] "GET /sky.jpg HTTP/1.1" 200 38831 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
172.17.1.32 - - [11/Jan/2019:14:25:38 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://www.mylinux.com/sky.jpg" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
172.17.1.32 - - [11/Jan/2019:14:26:21 +0800] "GET / HTTP/1.1" 200 25 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
172.17.1.32 - - [11/Jan/2019:14:36:21 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
172.17.1.32 - - [11/Jan/2019:14:38:09 +0800] "GET /sky.jpg HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"

设定发往后端主机的请求报文的请求首部的值

[root@node3 ~]# vi /etc/nginx/conf.d/ilinx.conf
server {
	listen 80;
	server_name www.linux.io;

	location / {
		proxy_pass http://192.168.10.11:80;
		proxy_set_header X_Real-IP $remote_addr;
	}
	location ~* \.(jpg|jpeg|png)$ {
		proxy_pass http://192.168.10.12:80;
	}
}
[root@node3 ~]# nginx -t
[root@node3 ~]# nginx -s reload

Upsteam Server2

编辑httpd配置文件,修改nginx proxy代理的真正首部。

[root@node2 ~]# vi /etc/httpd/conf/httpd.conf
LogFormat "%{X_Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node2 ~]# systemctl restart httpd

查看upstream server1服务log日志信息,可以看到nginx proxy服务器没修改客户端请求真正的ip地址,直接传给后端服务器。

[root@node2 ~]# tail /var/log/httpd/access_log
172.17.1.32 - - [11/Jan/2019:14:48:31 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.1.32 - - [11/Jan/2019:14:48:53 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.1.32 - - [11/Jan/2019:14:48:53 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
172.17.1.32 - - [11/Jan/2019:14:48:53 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"

设定发往客户端的响应报文的首部值,该功能是由ngx_http_headers_module模块所提供,所以安装时编译安装即可使用此功能

[root@node3 ~]# vi /etc/nginx/conf.d/ilinx.conf
server {
	port 80;
	server_name www.mylinux.com;
	location /data/ {
		root /data/nginx/html;

	location / {
		proxy_pass http://192.168.10.11:80;
		proxy_set_header X_Real-IP $remote_addr;
		add_header X-Via $server_addr;
	}
	location ~* \.(jpg|jpeg|png)$ {
		proxy_pass http://192.168.10.12:80;
	}
}
[root@node3 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node3 ~]# nginx -s reload

 浏览器输入测试:http://www.mylinux.com

配置nignx启用缓存功能

在nginx主配置文件http段中配置

在nginx主配置文件http段中配置
[root@node3 ~]# vi /etc/nginx/nginx.conf
http {
	proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g;
}
参数说明:
	proxy_cache_path 缓存文件路径
	levels 设置缓存文件目录层次;levels=1:1:1 表示三级目录
	keys_zone 设置缓存名字和共享内存大小
	inactive 在指定时间内没人访问则被删除
	max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。
创建缓存路径
[root@node3 ~]# mkdir /data/nginx/cache -pv
mkdir: created directory ‘/data/nginx’
mkdir: created directory ‘/data/nginx/cache’
配置nginx配置文件调用缓存功能
[root@node3 ~]# vi /etc/nginx/conf.d/ilinx.conf
server {
	listen 80;
	server_name www.mylinux.com;
	proxy_cache pcache; #调用指定的缓存空间pcache来缓存数据;
	proxy_cache_key $request_uri; #定义用作缓存项的key的字符串;
	proxy_cache_methods GET HEAD; #为哪些请求方法使用缓存;
	proxy_cache_valid 200 302 301 1h; #200,302,301响应码缓存时长1小时;
	proxy_cache_valid any 1m; #其它响应码缓存1分钟;
	proxy_cache_use_stale http_502;  #主机在与代理服务器通信期间响应码是502时,代理服务器使用本地缓存来响应。
	#缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;
	proxy_connect_timeout 40; #代理连接超时时长;

	location / {
		proxy_pass http://192.168.10.11:80;
		proxy_set_header X_Real-IP $remote_addr;
		add_header X-Via $server_addr;
		
	}
	location ~* \.(jpg|jpeg|png)$ {
		proxy_pass http://192.168.10.12:80;
	}
}
[root@node3 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node3 ~]# nginx -s reload
[root@node3 ~]# ls /data/nginx/cache/9/d/7/6666cd76f96956469e7be39d750cc7d9 
P8\ 查看缓存内容

通过nginx代理实现lnmp环境

 

node1 Upstream Server1

安装php-fpm mariadb-server php-mysql php-mbstring软件包

[root@node1 ~]# yum -y install php-fpm mariadb-server php-mysql php-mbstring
[root@node1 ~]# cd /etc/php-fpm.d/
编辑php模块配置文件
[root@node1 ~]# vi www.conf
listen = 0.0.0.0:9000
#listen.allowed_clients = 127.0.0.1
pm.max_children = 150
[root@node1 ~]# mkdir /var/lib/php/session -pv 
[root@node1 ~]# chown  apache:apache /var/lib/php/session
启动php服务,查看状态
[root@node1 ~]# systemctl start php-fpm.service
[root@node1 ~]# ss -tunl | grep 9000
tcp    LISTEN     0      128       *:9000                  *:*   

在nginx上配置ngx_http_fastcgi_module模块支持使用fastcgi_param指令设置参数的嵌入式变量

[root@node3 ~]# vi /etc/nginx/conf.d/ilinux.conf
server {
	listen 80;
	server_name www.mylinux.com;
	index index.php index.html;

	location / {
		root /data/nginx/html;
		
	}
	location ~* \.php$ {
		fastcgi_pass 192.168.10.11:9000; #设置FastCGI服务器的地址。地址可以指定为域名或IP地址,以及端口。
		fastcgi_index index.php;  #fastcgi默认的主页资源
		fastcgi_param SCRIPT_FILENAME  		/data/apps/$fastcgi_script_name; #设置应传递给FastCGI服务器的参数。该值可以包含文本、变量及其组合。SCRIPT_FILENAME参数将等于“/data/apps/index.php”
		include fastcgi_params; #调用fastcgi_params模块生效。
	}
}
创建本地静态资源访问路径,同时提供默认测试页
[root@node3 ~]#  mkdir /data/nginx/html/ -pv
[root@node3 ~]# vi /data/nginx/html/index.html
<h1>Nginx Server</h1>
测试语法并重新加载配置文件
[root@node3 ~]# nginx -t
[root@node3 ~]# nginx -s reload

node1 upstream server1

创建php默认测试页

[root@node1 ~]# mkdir /data/apps -pv
mkdir: created directory ‘/data/apps’
[root@node1 ~]# vi /data/apps/index.php
<?php
        phpinfo();
?>

客户端浏览器测试:http://www.mylinux.com

node1 upsteam server1

数据库安装,修改配置文件

[root@node1 ~]# yum -y install mariadb-server
[root@node1 ~]# vi /etc/my.cnf.d/server.cnf
skip_name_resolve=ON  #跳过域名反向解析
innodb_file_per_table=ON #开启每表查询
启动服务,查看端口状态
[root@node1 ~]# systemctl start mariadb
[root@node1 ~]# ss -tunlp | grep 3306
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=1286,fd=17))
[root@node1 ~]#
[root@node1 ~]# mysql_secure_installation #初始化数据库
[root@node1 ~]# mysql -uroot -p123456  #测试登录数据

部署phpMyAdmin网站

上传phpMyAdmin压缩包,解压,创建链接,并修改配置文件

[root@node1 ~]# ls
 phpMyAdmin-4.0.10.20-all-languages.tar.gz 
[root@node3 ~]# tar xf phpMyAdmin-4.7.9-all-languages.tar.gz -C /data/nginx/html/
cd [root@node3 ~]# cd /data/nginx/html/
[root@node3 html]# ls
index.html  phpMyAdmin-4.7.9-all-languages
[root@node3 html]# ln -sv phpMyAdmin-4.7.9-all-languages pma
‘pma’ -> ‘phpMyAdmin-4.7.9-all-languages’
[root@node3 html]# cd pma/
[root@node3 pma]# cp config.sample.inc.php config.inc.php 
[root@node1 pma]# vi config.inc.php 
$cfg['blowfish_secret'] = '23425werfgdyrta8b7c6d'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '394056ab';

 开启FastCGI服务器保持连接打开状态

       默认情况下,FastCGI服务器将在发送响应后立即关闭连接。但是,当这个指令被设置为on时,nginx将指示FastCGI服务器保持连接打开。

编辑配置文件增加如下配置
[root@node2 ~]# vi ilinux.conf
	location ~* \.php$ {
		fastcgi_keep_conn on;
}
[root@node2 ~]# nginx -t
[root@node2 ~]# nginx -s reload

压测后端服务,可以看到性能有所提升

[root@node2 ~]# ab -c 100 -n 1000 http://www.mylinux.com/pma/index.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.mylinux.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.12.2
Server Hostname:        www.mylinux.com
Server Port:            80

Document Path:          /pma/index.php
Document Length:        8715 bytes

Concurrency Level:      100
Time taken for tests:   23.741 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      10082000 bytes
HTML transferred:       8715000 bytes
Requests per second:    42.12 [#/sec] (mean)
Time per request:       2374.052 [ms] (mean)
Time per request:       23.741 [ms] (mean, across all concurrent requests)
Transfer rate:          414.72 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.1      0      10
Processing:    58 2325 510.3   2308    4076
Waiting:       43 2244 485.6   2245    3880
Total:         58 2326 510.1   2308    4081

Percentage of the requests served within a certain time (ms)
  50%   2308
  66%   2441
  75%   2589
  80%   2715
  90%   2986
  95%   3228
  98%   3569
  99%   3736
 100%   4081 (longest request)

通过/status和/ping来获取fpm server状态信息

[root@node3 ~]# vi /etc/nginx/conf.d/ilinux.conf
server {
				location ~* ^/(status|ping)$ {
					include        fastcgi_params;
					fastcgi_pass 192.168.10.11:9000;
					fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
				}
}			
[root@node3 ~]# nginx -t
[root@node3 ~]# nginx -s reload

浏览器输入http://www.mylinux.com/status 或 /www.mylinux.com/ping可以看到nginx proxy代理状态页

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值