search
HomeBackend DevelopmentPHP Tutorial 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配置及优化指南(上)
******************转载请注明来源 ***************


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software