Hey everyone,

We are facing a rather peculiar issue with our new stack of Ubunt, Nginx, PHP 7 & Drupal 8. Specifically image styles are not being generated.

Our current NGINX conf is the following:

server {
    listen      123.123.123.123:80;
    server_name domain.com;
    root        /home/user/web/domain.com/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/domain.com.log combined;
    access_log  /var/log/nginx/domains/domain.com.bytes bytes;
    error_log   /var/log/nginx/domains/domain.com.error.log error;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* "/\.(htaccess|htpasswd|git)$" {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ ^/sites/.*/private/ {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ ^/sites/[^/]+/settings.php$ {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ /(readme.html|license.txt) {
        deny all;
        access_log off;
        log_not_found off;
    }

    location / {
        try_files $uri @rewrite;
        location ~* ^.+\.(ogg|ogv|svg|svgz|swf|eot|otf|woff|mov|mp3|mp4|webm|flv|ttf|rss|atom|jpg|jpeg|gif|png|ico|bmp|mid|midi|wav|rtf|css|js|jar)$ {
            expires 365d;
            access_log off;
        }

        location ~ \.php$|^/update.php {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            fastcgi_pass    unix:/var/run/php/domain.com.sock;
            fastcgi_index   index.php;
            fastcgi_intercept_errors on;
            include         /etc/nginx/fastcgi_params;

            fastcgi_cache microcache;
            fastcgi_cache_valid 200 1m;
            fastcgi_cache_valid 301 302 1m;
            fastcgi_cache_valid 404 1m;
            fastcgi_cache_bypass $no_cache;
            fastcgi_no_cache $no_cache;

            set $no_cache 0;
                if ($request_method !~ ^(GET|HEAD)$) {
                    set $no_cache 1;
                }
                if ($no_cache = "1") {
                    add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
                    add_header X-Microcachable "0";
                }
                if ($http_cookie ~ SESS) {
                    set $no_cache "1";
                }
        }
    }

    location @rewrite {
        rewrite ^ /index.php last;
    }

    location ~ ^/sites/.*/files/styles/ {
        try_files $uri @rewrite;
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/user/web/domain.com/document_errors/;
    }

    location /vstats/ {
        alias   /home/user/web/domain.com/stats/;
        include /home/user/web/domain.com/stats/auth.conf*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/user/conf/web/nginx.domain.com.conf*;
}

Any thoughts on this, would be greatly appreciated

Comments

nateB’s picture

vibrasphere’s picture

In my case on Drupal 7 this solves the problem:

location ~* /sites/default/files/.*\.(?:css|js|ico|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
    expires 1w;
    add_header ETag "";
    add_header Cache-Control "max-age=2628000, no-transform, public";
    try_files $uri $uri/ @rewrite;
}

The actual problem was the missing try_files $uri $uri/ @rewrite

Used with AdvAgg module.

boesbo’s picture

do you have solution ?

phanosd’s picture

Unfortunately not. Can confirm however that I have the same issue with Drupal 7 as well on nginx.

If you do find anything please share

nateB’s picture

What shows up in the log at / var/log/nginx/domains/domain.com.error.log when you try to generate images?

Can you get image styles to generate when using the NGINX prescribed .conf?

digibear’s picture

ok, this right here:

location / { try_files $uri @rewrite; location ~* ^.+\.(ogg|ogv|svg|svgz|swf|eot|otf|woff|mov|mp3|mp4|webm|flv|ttf|rss|atom|jpg|jpeg|gif|png|ico|bmp|mid|midi|wav|rtf|css|js|jar)$ { expires 365d; access_log off; }
 

can be a problem.   Btw, its never safe to let a script configure a server. You are just asking for it!

.htaccess  should be done manually. because the other arguments, I don't know if they are setting it up correctly. The "rewrite engine on" has to be on first line in the htaccess for this to work properly with nginx.

but I also question the rewrite line. as what should be printed in .htaccess should be:

  

location ~\.(ogg|ogv|svg|svgz|swf|eot|otf|woff|mov|mp3|mp4|webm|flv|ttf|rss|atom|jpg|jpeg|gif|png|ico|bmp|mid|midi|wav|rtf|css|js|jar)$ { expires 365d; access_log off; }

I also question the  expires 365d; option, as you should clear the cache way before 1 year from now.

also it should have soft restarted the web server. if you need to soft start, I find switch php versions forces a reboot. (just make sure your settings go back, make a list of the php options before switching it)

also, the code

try_files $uri $uri/ @rewrite

is missing too

ikaros123’s picture

My setup is the following

Drupal 8.6.3

mysql 5.7.24

Php 7.2.14-1

ubuntu16.04.1 

nginx/1.10.3

My nginx configuration is the default recipe from https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/

Permissions on folders are already checked.

I cannot get image styles to work. New images are uploaded correctly, but no image styles are being created . 

This post https://drupal.stackexchange.com/questions/236290/drupal-8-image-styles-... mentions that systemd is configured to give php-fpm a private /tmp that is different from PHP's /tmp...

Has anyone else tried configuring php-fpm for drupal and nginx ? 

nateB’s picture

While I want to trust that all's correctly uploaded and configured as you say, it's really hard to troubleshoot the issue without seeing error logs and your personal nginx config file.