Nginx – PHP-fpm is slow with nginx,

dockernginxphp-fpmUbuntu

So, I have been running some benchmarks on my docker container (nginx connecting to php-fpm) and it's over 70 x slower than bare metal. I can manage 100 requests a second compared to bare metal which is 7,000 a second.

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

My 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;
        }
    }

}

My dockerfile for nginx, just copies the conf file to the /etc/nginx folder.

if I build a custom fpm file, or use the following one: https://github.com/uvd/php-docker-bench I get the correct amount of RPS (7,000) but with my current nginx/fpm setup, I barely surpass 100 RPS. What am I doing wrong, any ideas?

Best Answer

Opcache is disabled in the default PHP FPM docker container. Add this line in your PHP Dockerfile to install it:

RUN docker-php-ext-install opcache