Как ускорить сайт на Bitrix посредством FastCGI

Как известно такие обработчики как APACHE, CGI значительно медленнее обрабатывают динамические скрипты нежели в связке с NGINX поэтому рекомендуем использовать связки, например FastCGI (Nginx + PHP-FPM).

Перед внесением изменений сохраните базовый конфиг локально !

Рассмотрим вариант оптимизированного конфига для Bitrix при использовании ISPmanager (значения выделенные жирным необходимо заменить на свои).

server {
server_name mydomain.kz www.mydomain.kz;
return 301 https://$server_name$request_uri;
charset off;
index index.php index.html;
add_header X-Frame-Options SAMEORIGIN;
location ^~ /bitrix/modules {
deny all;
}
add_header X-Content-Type-Options nosniff;
disable_symlinks if_not_owner from=$root_path;
include /etc/nginx/vhosts-includes/.conf; include /etc/nginx/vhosts-resources/mydomain.kz/.conf;
access_log /var/www/httpd-logs/mydomain.kz.access.log;
error_log /var/www/httpd-logs/mydomain.kz.error.log notice;
ssi on;
return 301 https://$host:443$request_uri;
set $root_path /var/www/mydomainkz/data/www/mydomain.kz;
root $root_path;
gzip on;
gzip_comp_level 7;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
location / {
location ~ [^/].ph(p\d|tml)$ { try_files /does_not_exists @php; } location ~ ^.+.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
expires 7d;
}
}
location @php {
fastcgi_index index.php;
fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mydomain.kz";
fastcgi_pass unix:/var/www/php-fpm/mydomainkz.sock;
fastcgi_split_path_info ^((?U).+.ph(?:p\d|tml))(/?.+)$; try_files $uri =404; include fastcgi_params; } listen 178.89.187.55:80; } server { ssi on; gzip on; gzip_comp_level 7; gzip_disable "msie6"; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript; server_name mydomain.kz www.mydomain.kz; ssl_certificate "/var/www/httpd-cert/mydomainkz/mydomain.kz_le5.crtca"; ssl_certificate_key "/var/www/httpd-cert/mydomainkz/mydomain.kz_le5.key"; ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; add_header Strict-Transport-Security "max-age=31536000;"; ssl_dhparam /etc/ssl/certs/dhparam4096.pem; charset off; #disable_symlinks if_not_owner from=$root_path; add_header X-Frame-Options SAMEORIGIN; location ^~ /bitrix/modules { deny all; } add_header X-Content-Type-Options nosniff; index index.php index.html; root $root_path; set $root_path /var/www/mydomainkz/data/www/mydomain.kz; set $php_sock unix:/var/www/php-fpm/mydomainkz.sock; access_log /var/www/httpd-logs/mydomain.kz.access.log; error_log /var/www/httpd-logs/mydomain.kz.error.log notice; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 1024M; client_body_buffer_size 4M; if ($host = 'www.mydomain.kz' ) { rewrite ^(.)$ https://mydomain.kz$1 permanent;
}
location / {
try_files $uri $uri/ @bitrix;
location ~ [^/].ph(p\d|tml)$ { try_files /does_not_exists @php; } location ~ ^.+.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
expires 7d;
}
}
location ~* /upload/..(php|php3|php4|php5|php6|phtml|pl|asp|aspx|cgi|dll|exe|shtm|shtml|fcg|fcgi|fpl|asmx|pht|py|psp|rb|var)$ { types { text/plain text/plain php php3 php4 php5 php6 phtml pl asp aspx cgi dll exe ico shtm shtml fcg fcgi fpl asmx pht py psp rb var; } } location ~ .php$ { try_files $uri @bitrix; fastcgi_pass $php_sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@mydomain.kz"; include fastcgi_params; } location @bitrix { fastcgi_pass $php_sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/bitrix/urlrewrite.php; fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@mydomain.kz"; } location ~ /bitrix/admin.+.php$ {
try_files $uri @bitrixadm;
fastcgi_pass $php_sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@mydomain.kz";
include fastcgi_params;
}
location @bitrixadm{
fastcgi_pass $php_sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/bitrix/admin/404.php;
fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@mydomain.kz";
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#
block this locations for any installation
#
ht(passwd|access)
location ~* /.ht { deny all; }
repositories
location ~* /.(svn|hg|git) { deny all; }
bitrix internal locations
location ~* ^/bitrix/(modules|local_cache|stack_cache|managed_cache|php_interface) {
deny all;
}
upload files
location ~* ^/upload/1c_[^/]+/ { deny all; }
use the file system to access files outside the site (cache)
location ~* /../ { deny all; }
location ~* ^/bitrix/html_pages/.config.php { deny all; }
location ~* ^/bitrix/html_pages/.enabled { deny all; }
Intenal locations
location ^~ /upload/support/not_image { internal; }
Cache location: composite and general site
location ~* @..html$ { internal; # disable browser cache, php manage file expires -1y; add_header X-Bitrix-Composite "Nginx (file)"; } # Player options, disable no-sniff location ~ ^/bitrix/components/bitrix/player/mediaplayer/player$ {
add_header Access-Control-Allow-Origin ; } # Accept access for merged css and js location ~ ^/bitrix/cache/(css/.+.css|js/.+.js)$ {
expires 30d;
error_page 404 /404.html;
}
Disable access for other assets in cache location
location ~* ^/bitrix/cache { deny all; }
Use nginx to return static content from s3 cloud storage
/upload/bx_cloud_upload/…amazonaws.com/
location ^~ /upload/bx_cloud_upload/ {
location ~ ^/upload/bx_cloud_upload/(http[s]?).([^/:]+).(s3|s3-us-west-1|s3-eu-west-1|s3-ap-southeast-1|s3-ap-northeast-1).amazonaws.com/(.+)$ {
internal;
resolver 8.8.8.8;
proxy_method GET;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Server $host;
proxy_max_temp_file_size 0;
proxy_pass $1://$2.$3.amazonaws.com/$4;
}
location ~* .$ { deny all; } } # Static content location ~ ^/(upload|bitrix/images|bitrix/tmp) {
expires 30d;
}
location ~* .(css|js|gif|png|jpg|jpeg|ico|ogg|ttf|woff|eot|otf)$ {
error_page 404 /404.html;
expires 30d;
}
location = /404.html {
access_log off ;
}
disable_symlinks if_not_owner from=$root_path;
include /etc/nginx/vhosts-includes/.conf; include /etc/nginx/vhosts-resources/mydomain.kz/.conf;
location @php {
fastcgi_index index.php;
fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mydomain.kz";
fastcgi_pass unix:/var/www/php-fpm/mydomainkz.sock;
fastcgi_split_path_info ^((?U).+.ph(?:p\d*|tml))(/?.+)$;
try_files $uri =404;
include fastcgi_params;
}
listen 178.89.187.55:443 ssl http2;
}

Теперь посмотрим на результат проделанной работы в тестере производительности Bitrix:

Было

Стало