一、nginx代理分离设置
1、动态网页
在服务器端执行,根据请求的不同生成不同的结果,并将结果返回给请求者,就叫动态网页。
开发动态网页的常用语言有及nginx与其结合的模块:
php:fastcgi,php不能做为nginx的模块运行
jsp(tomcat):http协议直接代理
python:uwsgi
perl:cgi
2、图片和其它文件分离
location / { proxy_pass http://192.168.100.179; } location ~* \.(jpg|jpeg|png|gif)$ { proxy_pass http://192.168.100.175; }
3、php文件和其它文件分离
location / { proxy_pass http://192.168.100.179; } location ~* \.php$ { fastcgi_pass http://xxx; }
二、LNMP
1、环境
Node1:php-fpm 7.1.1
Node2:Mariadb 10.0.20
Node7:nginx 1.10.3
各安装过程可以查看之前的文章,不重复了
一般建议将静态web服务器和动态web应用服务器放在同一台服务器上,放在不同服务器上时,需要很大的网络开销
2、整合nginx和php
源码编译安装的nginx配置文件中有默认就有整合php的配置项(被注销了)
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 ### php作为httpd的模块工作 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ### php-fpm服务器 #location ~ \.php$ { # root html; # php-fpm服务器上放动态页面的目录 # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
fastcgi模块的常用指令:
fastcgi_pass
指定fastcgi服务监听端口、地址、也支持使用 Uxin socket
ngx_http_fastcgi_module
Syntax: | fastcgi_pass |
---|---|
Default: | — |
Context: | location , if in location |
Sets the address of a FastCGI server. The address can be specified as a domain name or IP address, and a port:
fastcgi_pass localhost:9000;
or as a UNIX-domain socket path:
fastcgi_pass unix:/tmp/fastcgi.socket;
fastcgi_param
定义传递给fpm的参数
Syntax: | fastcgi_param |
---|---|
Default: | — |
Context: | http , server , location |
Sets a parameter
that should be passed to the FastCGI server. The value
can contain text, variables, and their combination. These directives are inherited from the previous level if and only if there are no fastcgi_param
directives defined on the current level.
The following example shows the minimum required settings for PHP:
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;
fastcgi_bind
指定连接fastcgi服务时使用的地址
fastcgi_index
定义默认主页
实例:
修改nginx的配置文件
server { root /www/c.net; server_name www.c.net;# location / {# proxy_pass http://backend;# health_check;# } location ~ \.php$ { root /data/htdocs2; fastcgi_pass 192.168.10.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } }
源码编译安装的nginx生成的fastcgi_params文件格式有问题,我们重新提供一个配置文件:
[root@Node7 nginx]# cat fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;
在php-fpm服务器上提供测试页面:
[root@Node1 htdocs2]# cat index.php
测试:
fastcgi模块常用的指令:
fastcgi_pass:指定fastcgi服务监听端口、地址、也支持使用 Uxin socket
fastcgi_bind:指定练习fpm服务时使用的地址
fastcgi_param:定义传递给fpm的参数
fastcgi_index:php的主页面文件
php-fpm的结果也是可以缓存的,缓存空间使用proxy_cache_path定义,使用fastcg_cache定义
fastcgi_cache_path
fastcgi_cache
fastcgi_cache_vaild
fastcgi_connect_timeout:连接fastcgi服务器的超时时长
fastcgi_send_timeout:向fastcgi服务器传输数据的超时时长
用法和proxy模块上的相应指令一样,不再重复
Embedded Variables
The ngx_http_fastcgi_module
module supports embedded variables that can be used to set parameters using the directive:
$fastcgi_script_name
保存了请求报文中的uri
For example, for the “/info/” request with the following directives
fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
the SCRIPT_FILENAME
parameter will be equal to“/home/www/scripts/php/info/index.php
”
# 如果前面没有使用root指令fastcgi服务器上的根目录,这里就要使用绝对路径
三、获取php-fpm的状态的状态信息
server { root /www/c.net; server_name www.c.net;# location / {# proxy_pass http://backend;# health_check;# } location ~* \.php$|status|ping { # 要先上php-fpm的配置文件中启用status,ping选项 root /data/htdocs2; fastcgi_pass 192.168.10.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } }
测试:
php-fpm状态信息详解:
pool: # 进程池名称
process manager: # 进程管理;有两种类型static和dynamic
start time: # 系统启动的时间
start since: # 已运行时长
accepted conn: # 已经接收的请求数
listen queue: # 监听(等待)队列中的请求个数
max listen queue: # fpm启动以来,监听(等待)队列中的请求的总数
listen queue len: # 等待队列长度
idle processes: # 空闲的进程数
active processes: # 活动的进程数
total processes: # 总进程数
max active processes: # fpm启动以来,最大活动进程数
max children reached: # fpm启动以来,达到最大子进程的次数
slow requests: # 慢速请求的个数