PHP-fpm 与 nginx 配合使用时速度很慢,

PHP-fpm 与 nginx 配合使用时速度很慢,

因此,我在 docker 容器(nginx 连接到 php-fpm)上运行了一些基准测试,它比裸机慢 70 多倍。我每秒可以处理 100 个请求,而裸机每秒要处理 7,000 个请求。

docker-compose.yml:

version: '3'

services:
    #web
    frontend:
        build:
            context: ./environment/nginx
            dockerfile: ./Dockerfile
        container_name: nginx_software
        restart: always
        ports:
            - 80:80
        volumes:
            - ./environment/nginx/nginx.conf:/etc/nginx/nginx.conf
        links:
            - php
    php:
        build:
            context: ./environment/php
            args:
                version: 7.3-fpm
            dockerfile: ./Dockerfile
        container_name: php_software
        restart: always
        ports:
            - 9000:9000
        volumes:
            - ./api:/var/www/software:cached
        links:
            - mysql
    mysql:
        build:
            context: ./environment/mysql
            args:
                version: 5.7
            dockerfile: ./Dockerfile
        container_name: mysql_software
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
            - 3306:3306
        volumes:
            - ./environment/mysql/data:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: software
            MYSQL_DATABASE: software
            MYSQL_USER: software
            MYSQL_PASSWORD: software

我的 nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {

}

http {

    include /etc/nginx/mime.types;

    server {

        listen 80;
        listen [::]:80;
        server_name software.test;
        root /usr/share/nginx/html/software;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        #add_header X-Content-Type-Options "nosniff";

        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }

        charset utf-8;
    }

    server {
        listen 80;
        listen [::]:80;
        server_name api.software.test;
        root /var/www/software/public;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        #add_header X-Content-Type-Options "nosniff";

        index index.php;

        charset utf-8;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        error_page 404 /index.php;

        location ~ \.php$ {

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO       $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

            fastcgi_pass   php:9000;
            fastcgi_index index.php;
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }
    }

}

我的 nginx dockerfile 只是将 conf 文件复制到 /etc/nginx 文件夹。

如果我构建自定义 fpm 文件,或者使用以下文件:https://github.com/uvd/php-docker-bench我获得了正确的 RPS 数量(7,000),但使用我当前的 nginx/fpm 设置,我几乎没有超过 100 RPS。我做错了什么,有什么想法吗?

答案1

Opcache 在默认的 PHP FPM docker 容器中被禁用。在 PHP Dockerfile 中添加此行以安装它:

运行 docker-php-ext-install opcache

相关内容