且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LNMP环境下nginx、php-fpm的配置文件讲解

更新时间:2022-09-03 20:00:39

一、nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    include  vhosts/*.conf;  
}

配置解释                                                            

# 指定Nginx的worker进程运行用户以及用户组 

user nobody nobody;


# 指定Nginx要开启的进程数,设置为CPU的总核数

worker_processes 2;


# 指定Nginx全局错误日志路径与级别,日志级别有: debug、info、notice、warn、error、crit

# 其中debug输出日志最为详细,crit输入日志最少 ;

error_log /usr/local/nginx/logs/nginx_error.log crit;


# 指定进程id的存储文件位置

pid /usr/local/nginx/logs/nginx.pid;


# 一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。

# ulimit -n 查看系统限制

worker_rlimit_nofile 51200;


# 设定Nginx的工作模式及连接数上限

events

# 指定工作模式为epoll,工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll,

# 其中select和poll是标准的工作模式,kqueue和qpoll是高效的工作模式;epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

 use epoll;

   

# 单个进程最大连接数(最大连接数=连接数*进程数)

worker_connections 6000;

# 文件扩展名与文件类型映射表

include mime.types;

   

# 默认文件类型为二进制流

default_type application/octet-stream;

   

# 服务器名字的hash表大小

server_names_hash_bucket_size 3526;

   

# 服务器名字的hash表的最大量

server_names_hash_max_size 4096;

   

# 指定Nginx日志的输出格式,其中combined_realip为自定义的日志名字

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"';

   

# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。

sendfile on;


# 用于防止网络堵塞

tcp_nopush on;

tcp_nodelay on;

   

# 长连接超时时间,单位为秒

keepalive_timeout 65;

   

# 设置客户端请求头读取超时时间

client_header_timeout 10;

   

# 设置客户端请求主题2超时时间

client_body_timeout 10;

   

# 指定响应客户端的超时时间

send_timeout 10;

   

# 为每个请求分配的内存池,内存池用于小配额内存块,如果一个块大于内存池 或者大于分页大小,那么它将被分配到内存池之外,如果位于内存池中较小的分配量没有足够的内存,那么将分配一个相同内存池大小的新块,这个指令仅有相当有限的效果 

connection_pool_size 256;

request_pool_size 4k;

   

# 指定来自客户端请求头的大小

client_header_buffer_size 1k;

   

# 指定客户端请求中较大的请求头的最大缓存最大数量和大小

large_client_header_buffers 8 4k;

   

# 输出缓存大小    

output_buffers 4 32k;

postpone_output 1460;

   

# 指令指定允许客户端连接的最大请求主体大小

client_max_body_size 10m;

   

# 这个指令可以指定连接请求主体的缓冲区大小。

client_body_buffer_size 256k;

   

#  指定连接请求主体试图写入的临时文件路径

client_body_temp_path /usr/local/nginx/client_body_temp;

# 反向代理临时存储目录

proxy_temp_path /usr/local/nginx/proxy_temp;


FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。

# 为Nginx配置FastCGI缓存指定一个路径

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

# 如果这个选项没有设置,即使创建了404.html和配置了error_page也没有效果

fastcgi_intercept_errors on;


# 启用压缩    

gzip on;

# 最小压缩文件大小

gzip_min_length 1k;

# 压缩缓冲区

gzip_buffers 4 16k;

# 压缩等级

gzip_comp_level 5;

# 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)

gzip_http_version 1.1;

# 要压缩的类型

gzip_types text/plain application/x-javascript text/css text/htm application/xml;

# 开启虚拟配置目录

include vhosts/*.conf;


验证nginx默认虚拟主机

在/usr/local/nginx/conf目录下新建立一个vhosts目录,并创建一个default.conf 配置文件;

1
2
3
4
5
6
7
8
9
10
[root@localhost blog]# mkdir /usr/local/nginx/conf/vhosts
[root@localhost blog]# cd /usr/local/nginx/conf/vhosts
[root@localhost vhosts]# vi default.conf
server
{
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;
}

listen 80后面默认不加;后面加 default 和 default_server 都可以;实验测试成功;

保存退出后,-t 检查配置文件是否正确,然后重启nginx;使用curl命令测试是否成功。或者在浏览器输入192.168.20.30 显示nginx欢迎页面表示成功;

1
2
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t
[root@localhost vhosts]# /etc/init.d/nginx restart
1
2
3
4
5
6
7
8
9
10
[root@localhost vhosts]# curl 192.168.20.30 -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 14 May 2015 06:28:36 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 11 May 2015 09:27:11 GMT
Connection: keep-alive
ETag: "555075ef-264"
Accept-Ranges: bytes
1
2
3
4
5
6
7
8
9
10
[root@localhost vhosts]# curl 192.168.20.30/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 13 May 2015 09:15:51 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 11 May 2015 09:27:11 GMT
Connection: keep-alive
ETag: "555075ef-264"
Accept-Ranges: bytes


二、php-fpm.conf 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vi   /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
user = php-fpm
group = php-fpm
listen.owner = nobody  
listen.group = nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024 
slowlog = /usr/local/php/var/log/slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir]=/data/www/:/tmp/

配置解释:

[global]:全局配置

pid:指定进程id文件

error_log:指定错误日志文件

[www]:指定pool 资源池的名字

listen:指定监听方式与Nginx配置中一致 ;IP+端口或sock文件;

user:启动进程的用户

group:启动进程的用户组


动态、静态子进程pm = static/dynamic

如果选择static,则由pm.max_children指定固定的子进程数。

如果选择dynamic,则由以下参数决定:

pm.max_children ,子进程最大数

pm.start_servers ,启动时的进程数

pm.min_spare_servers ,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程

pm.max_spare_servers ,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理

对于专用服务器,pm可以设置为static。

慢执行日志用来性能追踪,进行定位php脚本哪里有问题。

每一个池子可以单独写一个慢日志;日志路径可以自定义位置;

slowlog = /usr/local/php/var/log/slow.log

request_slowlog_timeout = 1      #慢日志的超时时间;


open_basedir的格式,目录需要自定义;

php_admin_value[open_basedir]=/data/www/:/tmp/


如果listen使用ip+端口通讯的话不需要指定listen.owner;

默认listen.owner是php-fpm;如果不在配置文件更改的话,没有权限执行/tmp/php-fcgi.sock这个文件,所以会报502错误;

php-fcgi.sock文件是php-fpm进程创建的;重启php-fpm服务,在tmp目录下会出现;默认权限为660,其他用户没有执行权限。

手动更改sock文件权限为666后,重启php-fpm服务,又会变为660,其他用户没权限执行

1
2
3
4
5
6
7
8
9
10
[root@localhost etc]# ls -l /tmp/
srw-rw----  1 php-fpm php-fpm    0 5月  14 15:53 php-fcgi.sock
[root@localhost etc]# chmod 666 /tmp/php-fcgi.sock 
[root@localhost etc]# ls -l /tmp/
srw-rw-rw-  1 php-fpm php-fpm    0 5月  14 15:53 php-fcgi.sock
[root@localhost etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost etc]# ls -l /tmp/
srw-rw----  1 php-fpm php-fpm    0 5月  14 15:57 php-fcgi.sock

LNMP环境下nginx、php-fpm的配置文件讲解


实验测试,使用sock文件通讯,nginx默认虚拟主机配置加入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost vhosts]# cat default.conf 
server
{
    listen 80 default;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;
     
    location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php-fcgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
}

配置解释

server:定义虚拟主机开始的关键字

listen:指定虚拟主机的服务端口

server_name:指定IP地址或域名,多个域名之间用空格分开

index:设定访问的默认首页地址

root:指定虚拟主机的网页根目录

charset:设置网页的默认编码格式

include fastcgi_params:开启fastcgi

fastcgi_pass:指定fastcgi监听方式:1、sock方式监听;2、TCP/IP方式监听

fastcgi_index:指定fastcgi默认起始页

fastcgi_param SCRIPT_FILENAME:设定fastcgi监听的目录


使用curl进行测试,返回200 OK,说明sock文件通讯成功;

1
2
3
4
5
6
7
[root@localhost log]# curl -x127.0.0.1:80 192.168.20.30/phpinfo.php -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 14 May 2015 08:25:03 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.37






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1651899,如需转载请自行联系原作者