Nginx&&PHP-FPM配置及优化指南(下)
Nginx&&PHP-FPM配置及优化指南(上)
本文介绍在Centos5.8/6.2&&RedHat(RHEL) 5.8/6.2下LEMP/LNMP环境下的Nginx&&PHP-FPM的WEB服务器配置及优化指南。
截至目前,各软件版本为
- Nginx 1.2.2
- PHP && PHP-FPM5.4.4
如果您还没有搭建LEMP环境,可以参照我之前写过一篇文章 LEMP(或LNMP)高性能的WEB服务器在CentOS6.2/5.8下的Yum搭建流程。在"LEMP搭建指南"中我只给出了Nginx&&PHP-FPM最基本的配置说明。
在本文中将更深入的介绍Nginx&&PHP-FPM的WEB服务器配置。
Nginx 配置文件也可以参考:http://wiki.nginx.org/NginxChs
Nginx&&PHP-FPM配置及优化指南(上)
Nginx的配置文件
Nginx的配置文件放在/etc/nginx路径之下,运行ls -l /etc/nginx 输出
total 36 drwxr-xr-x. 2 root root 4096 Jul 11 19:52 conf.d -rw-r--r--. 1 root root 964 Jul 3 19:53 fastcgi_params -rw-r--r--. 1 root root 2837 Jul 3 19:53 koi-utf -rw-r--r--. 1 root root 2223 Jul 3 19:53 koi-win -rw-r--r--. 1 root root 3463 Jul 3 19:53 mime.types -rw-r--r--. 1 root root 643 Jul 3 19:50 nginx.conf -rw-r--r--. 1 root root 596 Jul 3 19:53 scgi_params -rw-r--r--. 1 root root 623 Jul 3 19:53 uwsgi_params -rw-r--r--. 1 root root 3610 Jul 3 19:53 win-utf
主配置文件nginx.conf详细说明
#运行用户 user nginx; #进程数目,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志 error_log /var/log/nginx/error.log warn; #PID文件 pid /var/run/nginx.pid; #工作模式及连接数上限 events { #单个worker_process进程的最大并发链接数 worker_connections 1024; } #设定http服务器,利用它的反向代理功能还可以提供负载均衡支持 http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; #制定默认MIME类型为二进制字节流 default_type application/octet-stream; #日志格式,参考URL 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 /var/log/nginx/access.log main; #开启调用Linux的sendfile(),提供文件传输效率 #sendfile一般设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime sendfile on; #是否允许使用socket的TCP_NOPUSH或TCP_CORK选项 #tcp_nopush on; #指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接 keepalive_timeout 65; #设置开启gzip压缩,参考URL #gzip on; #虚拟主机配置文件引入 include /etc/nginx/conf.d/*.conf; }
主配置文件nginx.conf参数优化要点
Nginx&&PHP-FPM配置及优化指南(上)
1. worker_processes及 worker_connections配置
默认配置中worker_processes及 worker_connections的数目有点小,只能应付1000次/秒以内的请求。
#默认配置 worker_processes 1; worker_connections 1024;
通常情况下,worker_processes设置为cpu数目,worker_connections保持1024即可。你可以使用cat /proc/cpuinfo |grep processor来查看CPU数量
2. 隐藏Ngnix版本信息
server_tokens off;
3. 拒绝web访问系统隐藏文件
location ~ /\. { access_log off; log_not_found off; deny all; }
4. 限制最大文件上传大小
client_max_body_size 20m; client_body_buffer_size 128k;
5. Nginx静态文件缓存控制
浏览器缓存非常有利于节省带宽,在Nginx中非常容易配置
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; }
6. Ngnix转发PHP请求至PHP-FPM
# Pass PHP scripts to PHP-FPM location ~* \.php$ { try_files $uri /index.php; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
7. 开启GZIP压缩
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;
********************************************
* 作者:叶文涛
* 本文链接:Nginx&&PHP-FPM配置及优化指南(上)
******************转载请注明来源 ***************

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content
