在上面分别分析了Nginx的FastCGI模块和php的php-fpm模式,下面通过一个例子将这两者结合起来,使得动静分离,静态网页由Nginx处理,而动态网页由php-fpm处理,其实在本章一开始的部分就将这两者结合起来了。
添加Nginx配置
下面看一个配置实例:
Nginx配置文件nginx.conf:
…… http { include conf/mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_connect_timeout 150; fastcgi_send_timeout 150; fastcgi_read_timeout 150;
#keepalive_timeout 0; keepalive_timeout 20; tcp_nodelay on;
#gzip on;
server { listen 192.168.3.12:80; server_name xxx.com www.xxx.com; #charset utf8;
#access_log logs/host.access.log main;
location = / { root /sdb1/www/; index index.php; }
location / { root /sdb1/www/; index index.php index.html;
if (!-f $request_filename) { rewrite ^(.*)$ /index.php?q=$1 last; break; }
if (!-d $request_filename) { rewrite ^(.*)$ /index.php?q=$1 last; break; }
}
error_page 404 /index.php;
# static files directly location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ { access_log off; expires 30d; }
# redirect error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
location ~ .php$ { fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; include fastcgi.conf; }
# deny access to .htaccess files, if it’s a Apache's document root location ~ /\.ht { deny all; }
} } ……
|
添加php-fpm配置
php-fpm的配置文件php-fpm.conf:
[root@mail etc]# grep -v ";" php-fpm.conf
[global] pid = run/php-fpm.pid error_log = log/php-fpm.log log_level = debug emergency_restart_threshold = 10 emergency_restart_interval = 20 process_control_timeout = 5
[www]
listen = 127.0.0.1:9000 listen.allowed_clients = 127.0.0.1 user = nobody group = nobody
pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 20 pm.max_requests = 300
pm.status_path = /status ping.path = /ping ping.response = pong
request_terminate_timeout = 50 request_slowlog_timeout = 50
slowlog = log/$pool.log.slow rlimit_files = 1024
|
还有一个FastCGI的参数文件fastcgi.conf就不贴出来了就是上面的那个内容。然后再重新载入Nginx的配置文件就可以了。
本实例是在一个业务不太繁忙下的配置,如果你的业务较忙,那么根据实际情况对配置文件中的参数做相关调整就可以了,在前面的内容中已经将配置文件中的指令介绍过了。
有关使用upstream_keepalive模块提供FastCGI模块实现keep-live的知识请参考卷1,在此就不多罗嗦了。