Nginx + FastCGI

Советы по настройке Nginx + FastCGI для Webasyst

Содержание...

Для работы фреймворка в среде Nginx + FastCGI без использования Apache необходимо включить в конфигурационный файл Nginx ряд специальных настроек.

Ниже приведен пример конфигурации для следующих условий:

  • Каталог на сервере, в который установлен фреймворк — /var/www/fw/.
  • PHP FastCGI обслуживает TCP-порт 9000 (это настройка по умолчанию).
server {
    listen 80;

    server_name fw;

    root /var/www/fw;
    index index.php;

    try_files $uri $uri/ /index.php?$query_string;

    location /index.php {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
    }

    # for install only
    location /install.php {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
    }

    location /api.php {
        fastcgi_split_path_info  ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
    }

    location ~ /(oauth.php|link.php|payments.php|captcha.php) {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ^~ /wa-data/protected/ {
        internal;
    }

    location ~ /wa-content {
        allow all;
    }

    location ~ /wa-apps/[^/]+/(plugins/[^/]+/)?(lib|locale|templates)/ {
        deny all;
    }

    location ~ /(wa-plugins/([^/]+)|wa-widgets)/.+/(lib|locale|templates)/ {
        deny all;
    }

    location ~* ^/wa-(cache|config|installer|log|system)/ {
        return 403;
    }

    location ~* ^/wa-data/public/contacts/photos/[0-9]+/ {
        root /var/www/fw/;
        access_log off;
        expires  30d;
        error_page   404  =  @contacts_thumb;
    }

    location @contacts_thumb {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/contacts/photos/thumb.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/wa-data/public/contacts/photos/thumb.php;
    }

    # photos app
    location ~* ^/wa-data/public/photos/[0-9]+/ {
        access_log   off;
        expires      30d;
        error_page   404  =  @photos_thumb;
    }

    location @photos_thumb {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/photos/thumb.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/wa-data/public/photos/thumb.php;
    }
    # end photos app

    # shop app
    location ~* ^/wa-data/public/shop/products/[0-9]+/ {
        access_log   off;
        expires      30d;
        error_page   404  =  @shop_thumb;
    }
    location @shop_thumb {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/shop/products/thumb.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/wa-data/public/shop/products/thumb.php;
    }

    location ~* ^/wa-data/public/shop/promos/[0-9]+ {
        access_log   off;
        expires      30d;
        error_page   404  =  @shop_promo;
    }
    location @shop_promo {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/shop/promos/thumb.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/wa-data/public/shop/promos/thumb.php;
    }
    # end shop app

    # mailer app
    location ~* ^/wa-data/public/mailer/files/[0-9]+/ {
        access_log   off;
        error_page   404  =  @mailer_file;
    }
    location @mailer_file {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/mailer/files/file.php;
        fastcgi_param  SCRIPT_FILENAME $document_root/wa-data/public/mailer/files/file.php;
    }
    # end mailer app

    # tasks app
    location ~* ^/wa-data/public/tasks/tasks/[0-9]+/ {
        access_log   off;
        expires      30d;
        error_page   404  =  @tasks_thumb;
    }

    location @tasks_thumb {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_NAME  /wa-data/public/tasks/tasks/thumb.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/wa-data/public/tasks/tasks/thumb.php;
    }
    # end tasks app

    location ~* ^.+\.(jpg|jpeg|gif|png|webp|js|css)$ {
        access_log   off;
        expires      30d;
    }
}

Проверьте также, чтобы в конфигурационном файле /etc/nginx/fastcgi_params присутствовали эти значения:

fastcgi_param SCRIPT_FILENAME     $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO           $fastcgi_path_info;