Table of Contents
Nginx的配置文件
主配置文件nginx.conf详细说明
主配置文件nginx.conf参数优化要点
1. worker_processes及 worker_connections配置
2. 隐藏Ngnix版本信息
3. 拒绝web访问系统隐藏文件
4. 限制最大文件上传大小 
5. Nginx静态文件缓存控制
6. Ngnix转发PHP请求至PHP-FPM
7. 开启GZIP压缩
Home Backend Development PHP Tutorial Nginx&&PHP-FPM配置及优化指南(下)

Nginx&&PHP-FPM配置及优化指南(下)

Jun 13, 2016 pm 01:21 PM
amp nginx php root worker

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
Copy after login

主配置文件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;
}
Copy after login

主配置文件nginx.conf参数优化要点

Nginx&&PHP-FPM配置及优化指南(上)

1. worker_processes及 worker_connections配置

默认配置中worker_processes及 worker_connections的数目有点小,只能应付1000次/秒以内的请求。

#默认配置
worker_processes  1;
worker_connections  1024;
Copy after login

通常情况下,worker_processes设置为cpu数目,worker_connections保持1024即可。你可以使用cat /proc/cpuinfo |grep processor来查看CPU数量

2. 隐藏Ngnix版本信息

server_tokens off;
Copy after login

3. 拒绝web访问系统隐藏文件

location ~ /\. {
    access_log off;
    log_not_found off; 
    deny all;
}
Copy after login

4. 限制最大文件上传大小 

client_max_body_size 20m;
client_body_buffer_size 128k;
Copy after login

5. Nginx静态文件缓存控制

浏览器缓存非常有利于节省带宽,在Nginx中非常容易配置

location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           360d;
}
Copy after login


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;
}
Copy after login
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;
Copy after login

********************************************
* 作者:叶文涛 
* 本文链接:Nginx&&PHP-FPM配置及优化指南(上)
******************转载请注明来源 ***************


Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles