1

I seem to be unable to get sub_filter to work with PHP-FPM on nginx

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

    proxy_set_header Accept-Encoding ""; # no compression allowed or next won't work
    sub_filter '<a href="http://'  '<a href="//';
    sub_filter '<img src="http://' '<img src="//';
    sub_filter_once off;
    sub_filter_types *;
}

For reference, the filter works perfectly fine with plain HTML files

location / {
    try_files $uri $uri/ =404;
    proxy_set_header Accept-Encoding ""; # no compression allowed or next won't work
    sub_filter '<a href="http://'  '<a href="//';
    sub_filter '<img src="http://' '<img src="//';
    sub_filter_once off;
}

Any help would be appreciated

2 Answers2

0

We had the same problem on our setup as you are having on yours. We discovered that we had PHP compression turned on. You will need to turn it off. The setting is located in the php.ini file.

The compression setting should read, zlib.output_compression = Off.

Once we updated that setting and restarted PHP, the NGINX sub_filter worked as expected.

Derrick
  • 336
  • 5
  • 18
0

I got perl CGI script where fastcgi_pass points, and in my case

zlib.output_compression = Off

is not the answer.

gzip off; not works for me also

I found in this answer that problem might be with compression that my backend script does. In case of proxy_pass they use proxy_set_header Accept-Encoding ""; to turn off compression.

In my case with fastcgi_pass this directive helped me (found related answer here):

fastcgi_param HTTP_ACCEPT_ENCODING "";
Artem S.
  • 543
  • 5
  • 16