В отличие от классической схемы с Apache, вариант связки nginx + PHP‑FPM позволяет обрабатывать PHP-запросы напрямую через менеджер процессов FPM, что дает выигрыш в производительности и более экономное использование ресурсов сервера.
Пример будет полезен администраторам, которые выбирают легковесное и высокопроизводительное окружение без использования Apache, адаптируйте конфигурацию под свои требованиям:
server {
server_name example.com www.example.com;
root /var/www/example.com/public_html;
listen 80;
# Для HTTPS:
# listen 443 ssl;
# ssl_certificate "/var/www/httpd-cert/example/example.com.crt";
# ssl_certificate_key "/var/www/httpd-cert/example/example.com.key";
# ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
# ssl_prefer_server_ciphers on;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
# ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
# Скрываем версию nginx
server_tokens off;
index index.php;
client_max_body_size 10m;
client_body_timeout 60s;
client_header_timeout 60s;
# access_log /var/log/nginx/example.com_access.log;
# error_log /var/log/nginx/example.com_error.log warn;
# Запрет опасных HTTP-методов
if ($request_method !~ ^(GET|HEAD|POST|PUT|DELETE)$) {
return 405;
}
# Запрет доступа к системным папкам HostCMS
location ~* ^/(upload/private|upload/helpdesk_\d+/attachments|upload/shop_\d+/eitems|hostcmsfiles/backup|hostcmsfiles/cache|hostcmsfiles/lib|hostcmsfiles/logs|hostcmsfiles/printlayout|hostcmsfiles/shop|hostcmsfiles/structure|hostcmsfiles/tmp|hostcmsfiles/tpl|hostcmsfiles/update|hostcmsfiles/xsl) {
deny all;
return 403;
}
# Запрет доступа к скрытым файлам и служебным папкам
location ~ /\.(ht|git|svn|idea|vscode) {
deny all;
return 404;
}
# Запрет выполнения PHP в папке uploads
location ~* ^/upload/.*\.php$ {
deny all;
return 403;
}
# Основной location
location / {
# Пробуем отдать статический HTML-кеш HostCMS (если включён)
set $cache_uri $uri;
if ($http_cookie !~* "PHPSESSID|_hccagree") {
set $cache_uri /cache_html/$host$request_uri/index.html;
}
try_files $uri $uri/ $cache_uri @hostcms;
}
# Обработка PHP-файлов
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/user_fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
# Передача HTTPS за прокси (если используется)
fastcgi_param HTTPS $https if_not_empty;
include fastcgi_params;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 300;
}
# Fallback на index.php для ЧПУ
location @hostcms {
# Все запросы к несуществующим файлам/директориям направляются на index.php
rewrite ^ /index.php last;
}
# Статика: кеширование и сжатие
location ~* \.(ico|pdf|flv|swf|xml|txt|woff2?|ttf|eot|svg|otf)$ {
expires 14d;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
location ~* \.(js|css|png|jpg|jpeg|gif|webp|avif)$ {
expires 14d;
add_header Cache-Control "public, must-revalidate";
try_files $uri =404;
}
# Обработка ошибки 404
error_page 404 /404/;
}