📜 ⬆️ ⬇️

Bitrix in conjunction Nginx + PHP-FPM, CNC setting, as well as a composite cache with feedback via nginx. Modified configuration

Objective: Provide the configuration of the virtual server Nginx to work Bitrix-cms in conjunction Nginx + PHP-FPM. Which is also suitable for Nginx + Apache2 bundles, with minor modifications.

Target audience: Server administrators, advanced site administrators, programmers.

There are enough articles on this topic, but if you don’t look official, they usually contain errors, and the official ones are full if you don’t want to use them in Nginx. I hope after I put this config to the Nginx + PHP-FPM bundle will become more serious.

Here is the official config for running the composite cache with feedback via nginx.

I will show the implementation of the file composite cache return. In general, the return from memcached is done by analogy. In the file cache return configuration, I counted 11 if, which I got rid of by remaking them on the map.

I will start with a simplified version of the CNC for those who need just a bunch of Nginx + PHP-FPM without returning the composite cache via Nginx. It is understood that the server section is already configured, with domain names and transfer to php-fpm.

location / { try_files $uri $uri/ /bitrix/urlrewrite.php$is_args$args; } 

How not surprisingly looking at those canvases of configs that I came across, this is enough for the bitrix to work correctly. If you need a redirect with index.php and index.html on without, then you need to add this line:

 if ($request_uri ~ ^(.*)/index.(html|php)) { return 301 $1/$is_args$args; } 

Unfortunately, there is no decent replacement if. But this line works without creating problems.

Sample minimalist configuration
 server { listen 80; server_name site.ru; root /var/www/site.ru/; index index.php; location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } if ($request_uri ~ ^(.*)/index.(html|php)) { return 301 $1/$is_args$args; } location / { try_files $uri $uri/ /bitrix/urlrewrite.php$is_args$args; } location ~* @.*\.html$ { internal; } } 


I want to emphasize that this is exactly the minimalistic configuration without rules for statics, compression, and there I covered only the composite cache files from direct access. The configuration that covers certain places from direct access through nginx is quite individual. I have here such a set that can someone come up. But you need to use carefully with knowledge of the case. Note that the introduction of these locales into your configuration can lead to the inoperability of the site or part of its functions.

Config fragment closing access to files that should be available only when accessing from PHP
 location ~ \.php$ { location ~* /\.\./ { internal; } location ~ /\.[^/]+$ { internal; } location ~* ^/upload/1c_[^/]+/ { internal; } location ~* ^/(bitrix/(cache|images|tmp)|upload)/ { internal; } location ~* ^/bitrix/(footer|header|license_key)\.php$ { internal; } location ~* ^/(bitrix|local)/components/(.*)/(.*)/(class|component)\.php$ { internal; } location ~* ^/(bitrix|local)/(backup|blocks|bx_cloud_upload|local_cache|module|modules|managed_cache|php_interface|public|stack_cache)/ { internal; } include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } location ~* \.(hg|svn|git|bzr)$ { internal; } location ~* /\.\./ { internal; } location ~* @.*\.html$ { internal; } location / { location ~* ^/(bitrix|local)/(backup|blocks|bx_cloud_upload|local_cache|module|modules|managed_cache|php_interface|public|services|stack_cache)/ { internal; } location ~ /\.[^/]+$ { internal; } location ~* ^/upload/1c_[^/]+/ { internal; } try_files $uri $uri/ /bitrix/urlrewrite.php$is_args$args; } 


And of course the location example for static files.

 location ~* \.(jpg|jpeg|png|tiff|gif|webp|xml|html|yml|ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|rtf|pdf|txt|js|css|bmp|pnm|pbm|ppm)$ { access_log off; expires 97d; } 

Now let's move on to the configuration for the composite to work with the return of cache files via nginx. First of all, it is necessary to determine whether the composite cache can be returned to this request or should be sent for processing via php. To do this, in Nginx, in the http section, add several maps, as well as several directives:

 modern_browser_value "modern"; modern_browser msie 10.0; modern_browser unlisted; map "$cookie_BITRIX_SM_LOGIN:$cookie_BITRIX_SM_UIDH:$cookie_BITRIX_SM_CC" $storedAuth { default ""; "~*:*:Y" ""; "~*:*:*" 1; "~*:*:" 1; } map "$request_method:$http_bx_action_type:$cookie_BITRIX_SM_NCC:$http_x_forwarded_scheme:$modern_browser:$storedAuth" $usecache { default "1"; "~GET:::*https*" "1"; "~GET:::*:*:" ""; } 

Further, directly in the server section we register

 set $i "index@"; location / { try_files /bitrix/html_pages/$host$uri$i${args}.html$usecache /bitrix/html_pages/$host$uri$i${args}=.html$usecache /bitrix/html_pages/$host$uri/$i${args}.html$usecache /bitrix/html_pages/$host$uri/$i${args}=.html$usecache $uri $uri/ /bitrix/urlrewrite.php$is_args$args; } 

Well, to understand how the minimum configuration of the server section will look like

An example of a minimalist configuration with the return of the file composite cache via nginx
 server { listen 80; server_name site.ru; root /var/www/site.ru/; index index.php; location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } if ($request_uri ~ ^(.*)/index.(html|php)) { return 301 $1/$is_args$args; } set $i "index@"; location / { try_files /bitrix/html_pages/$host$uri$i${args}.html$usecache /bitrix/html_pages/$host$uri$i${args}=.html$usecache /bitrix/html_pages/$host$uri/$i${args}.html$usecache /bitrix/html_pages/$host$uri/$i${args}=.html$usecache $uri $uri/ /bitrix/urlrewrite.php$is_args$args; } location ~* @.*\.html$ { internal; } } 

I will be glad to questions and suggestions about this config, as well as for the joy of seeing your developments.

Source: https://habr.com/ru/post/438604/