我有一个项目,已经在EC2实例上正常运行了一年以上。从后端到前端的响应通常在600毫秒内。在此服务器上,我们将其称为服务器A,前端和后端托管在同一台服务器上,数据库托管在MongoDB Atlas上。
现在我们使用的是docker,前端和后端位于单独的容器中,每个容器托管在不同的实例上。现在,响应时间已跃升至3.36s。因此从600 毫秒到3.36 秒。我正在使用与服务器A相同的数据库服务器。
我真的很想听听您关于如何优化PHP,Laravel和Nginx,以在Docker上获得最快响应时间的想法。
这是我的配置文件DockerFile,start.sh和nginx.conf
DockerFile
FROM nginx:mainline-alpine
COPY start.sh /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
COPY supervisord.conf /etc/supervisord.conf
RUN apk add --update \
php7 \
php7-fpm \
php7-pdo \
php7-pdo_mysql \
php7-gd \
php7-mcrypt \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-curl \
php7-zip \
php7-fileinfo \
php7-xmlwriter \
php7-tokenizer \
php7-mongodb \
php7-dom \
php7-session \
php7-zlib && \
php7 -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" && \
php7 composer-setup.php --install-dir=/usr/bin --filename=composer && \
php7 -r "unlink('composer-setup.php');" && \
ln -s -f /usr/bin/php7 /usr/bin/php && \
ln -s -f /etc/php7/php.ini /etc/php7/conf.d/php.ini
RUN apk add --update \
bash \
openssh-client \
supervisor
RUN mkdir -p /etc/nginx && \
mkdir -p /etc/nginx/sites-available && \
mkdir -p /etc/nginx/sites-enabled && \
mkdir -p /run/nginx && \
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
mkdir -p /var/log/supervisor && \
rm -Rf /var/www/* && \
chmod 755 /start.sh
RUN php -i | grep -i gd
RUN wget -O phpunit https://phar.phpunit.de/phpunit-8.phar
RUN chmod +x phpunit
RUN ./phpunit --version
RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" \
-e "s/variables_order = \"GPCS\"/variables_order = \"EGPCS\"/g" \
/etc/php7/php.ini && \
sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" \
-e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" \
-e "s/user = nobody/user = nginx/g" \
-e "s/group = nobody/group = nginx/g" \
-e "s/;listen.mode = 0660/listen.mode = 0666/g" \
-e "s/;listen.owner = nobody/listen.owner = nginx/g" \
-e "s/;listen.group = nobody/listen.group = nginx/g" \
-e "s/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm.sock/g" \
-e "s/^;clear_env = no$/clear_env = no/" \
/etc/php7/php-fpm.d/www.conf
EXPOSE 80
COPY . /var/www
WORKDIR /var/www
RUN chmod -R 777 storage/
RUN echo `ls`
CMD ["/start.sh"]
start.sh
#!/bin/bash
# ----------------------------------------------------------------------
# Create the .env file if it does not exist.
# ----------------------------------------------------------------------
if [[ ! -f "/var/www/.env" ]] && [[ -f "/var/www/.env.example" ]];
then
cp /var/www/.env.example /var/www/.env
fi
# ----------------------------------------------------------------------
# Run Composer
# ----------------------------------------------------------------------
if [[ ! -d "/var/www/vendor" ]];
then
cd /var/www
composer install --no-scripts
composer update
composer dump-autoload
fi
php artisan optimize --force
php artisan config:cache
# ----------------------------------------------------------------------
# Start supervisord
# ----------------------------------------------------------------------
exec /usr/bin/supervisord -n -c /etc/supervisord.conf
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
#tcp_nopush on;
keepalive_timeout 0;
#gzip on;
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=BACKEND:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
server {
listen 80;
root /var/www/public;
index index.php index.html;
set $no_cache 0;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\. {
deny all;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_cache BACKEND;
fastcgi_cache_valid 200 1m;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
答案 0 :(得分:0)
在我的情况下(mac os),我发现卷挂接正在受到打击。
通过应用此应用,性能提高了50%。
volumes:
- .:/var/www/html:cache
您可以使用其他音量标志来提高性能
参考来自 https://www.docker.com/blog/user-guided-caching-in-docker-for-mac/
注意:仅适用于Docker版本Docker 17.04 CE Edge