Join the community today
Become a Member

Sysadmin How to resizing image with nginx

Discussion in 'System Administration' started by hendry, Oct 11, 2016.

  1. hendry

    hendry Member

    59
    13
    8
    Sep 19, 2016
    Ratings:
    +17
    Local Time:
    10:51 AM
    nginx/1.11.4
    MariaDB 10
    Based on this post that talk about install laravel so I try to install it and now it's 100% work but I have problem with images that I tested on Gtmetric. So I try to search about resizing images with nginx in centminmod community but I can't found it.
    GTmetric result:
    gtmetric result.png

    As you can see serve sclaed images on pagespeed score is zero. I came across on nginx module (ngx_http_image_filter_module) that allowed us to resize all image but I don't know how to implement that module on centmin. I have tried this tutorial (image resizing with nginx and also this tutorial Resize images on the fly with Nginx try_files) but it's not work. Any suggestion about this and what I should to do with centmin mod?
     
  2. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    For dynamic resizing you need to enable and install ngx_pagespeed module Nginx PageSpeed - CentminMod.com LEMP Nginx web stack for CentOS

    FYI, 123.08stable no longer works with ngx_pagespeed, you need to update to 123.09beta01 for ngx_pagespeed. See news thread at Nginx 1.9.11 dynamic module compatibility. 123.08stable has disabled ngx_pagespeed and lua nginx modules due to incompatibility with nginx 1.9.11 dynamic modules.

    If you want ngx_pagespeed for now you need to update Centmin Mod to 123.09beta01 outlined here Beta Branch - Centmin Mod .09 beta branch Testing

    With 123.09beta01, set in /etc/centminmod/custom_config.inc persistent config file you create or already created and set:
    Code (Text):
    NGINX_PAGESPEED=y
    NGXDYNAMIC_NGXPAGESPEED=y
    

    and then recompile Nginx 1.11.4 or higher via centmin.sh menu option 4

    Then tweak pagespeed.conf for your site and troubleshoot. Here's some links for examples and help
    1. https://centminmod.com/nginx_ngx_pagespeed.htm
    2. Nginx PageSpeed - This forum's pagespeed.conf | Centmin Mod Community
    3. Nginx PageSpeed - Nginx Pagespeed Troubleshooting steps | Centmin Mod Community
    4. Nginx, PHP-FPM & MariaDB MySQL | Centmin Mod Community
    5. Install & Upgrades or Pre-Install Questions | Centmin Mod Community
     
  3. hendry

    hendry Member

    59
    13
    8
    Sep 19, 2016
    Ratings:
    +17
    Local Time:
    10:51 AM
    nginx/1.11.4
    MariaDB 10
    After install pagespeed and recompile I can't restart my nginx. below errors comes up:

    nginx: [warn] PCRE library does not support JIT in /usr/local/nginx/conf/nginx.conf:8
    nginx: [emerg] pattern "^(.+?\.php)(/.*)$" must have 2 captures in /usr/local/nginx/conf/php.conf:3

    nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

    try to edit and aomment out those error I still can't restart nginx.
     
  4. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    which version of centmin mod ? 123.09beta01 ? output for
    Code (Text):
    nginx -V

    123.09beta01 had recent change, so update 123.09beta01 to latest first - then run centmin.sh to prompt for PCRE Jit compile automatically before centmin.sh menu 4 recompile
     
  5. hendry

    hendry Member

    59
    13
    8
    Sep 19, 2016
    Ratings:
    +17
    Local Time:
    10:51 AM
    nginx/1.11.4
    MariaDB 10
    yes I use 123.09beta01 with latest update. I don't know why this error show up after I install pagespeed and recompile nginx.

    here is nginx output:
     
  6. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    what's output for
    Code (Text):
    which pcretest
    pcretest -C
     
  7. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    also output for

    Code (Text):
    which wget
    wget -V
     
  8. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    also try stopping nginx then seeing if any nginx processes are listed
    Code (Text):
    ps aufxw

    if there are you need to kill process ids related to nginx
    nginx running
    Code (Text):
    root     30863  0.0  0.1 195736 33340 ?        Ss   13:01   0:00 nginx: master process /usr/local/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    nginx    30864  0.0  0.1 208028 49148 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30865  0.0  0.1 208028 47984 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30866  0.0  0.1 208028 47984 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30867  0.0  0.1 208028 47984 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30868  0.0  0.1 208028 47984 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30870  0.0  0.1 208028 47912 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30871  0.0  0.1 208028 47912 ?        S<l  13:01   0:00  \_ nginx: worker process
    nginx    30874  0.0  0.1 208028 47984 ?        S<l  13:01   0:00  \_ nginx: worker process

    Then restart nginx
     
  9. bassie

    bassie Active Member

    491
    103
    43
    Apr 29, 2016
    Ratings:
    +310
    Local Time:
    5:51 AM
    @hendry There is no need to use Nginx with a heavy module as Pagespeed
    and a lot of configuration changes in your Nginx environment to optimize images.

    You can pre- optimize your sources (images, pictures, js, css etc) with for example Compressor.io, JScompress etc.

    The above tools are optimizing the files one single time and you are ready, instead of Pagespeed.
    Which is never ending and is continuously working on optimization the same file based on browser, device type, etc
    (high consumption of memory and CPU at a busy site and unnecessary if you ask me).

    Just review the top sites in the world, they all have pre- optimized resources.
    Nobody uses Pagespeed from them, not even Google.
     
  10. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    yes pre-optimization can only take you so far and is what i always do see WebPerf - Zopfli Optimization: Literally Free Bandwidth | Centmin Mod Community

    but also see Nginx PageSpeed - Benefits of ngx_pagespeed | Centmin Mod Community dynamically uploaded end user images you may or may not be able to pre-optimize depending on web app i.e. forums but wordpress has plugins that can do that on uploaded images etc

    you can't really tell if they do or not as pagespeed can do inplace optimisations so filenames remain the same and just hide the pagespeed headers and you wouldn't know :)
     
  11. bassie

    bassie Active Member

    491
    103
    43
    Apr 29, 2016
    Ratings:
    +310
    Local Time:
    5:51 AM
    Pagespeed is to unstable and uses to many resources for the extent of all of Google's sites, crawled resources i.e. images etc. They are simply too big.

    Should be possible Google used technique from Pagespeed but never ever the Pagespeed module as whole. Can't be. Don't believe it even 0.0000001 % ;).
     
  12. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    yes uses more cpu resources but google has a distributed web server farm so easy to deploy for that speed boost. There's no real free lunch for extra page load speed - resources will always be used more i.e. memory for caching (whether it be from varnish cache, memcached caching, redis cache, nginx caching) and cpu for any image optimisation processing etc.

    I've been using ngx_pagespeed for several dozen sites for 3+ yrs now. Of course my sites aren't as highly traffic based but these are with 128MB - 512MB VPS predominantly :)

    Centmin Mod Nginx based ngx_pagespeed compiled as dynamic module is only a ~2.9-3.8MB nginx binary
     
    Last edited: Oct 11, 2016
  13. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    FYI, apache mod_pagespeed uses way more cpu resources than nginx ngx_pagespeed from my stress tests when comparising apache without mod_pagespeed and nginx without ngx_pagespeed ;)

    IIRC was around 3.5x to 6.5x times higher with mod_pagespeed vs ngx_pagespeed
     
  14. bassie

    bassie Active Member

    491
    103
    43
    Apr 29, 2016
    Ratings:
    +310
    Local Time:
    5:51 AM
    I was referring to storage. Simply to big to store both original and compressed image (disks and memory) as Pagespeed does.

    If you are uploading an image via Google+ it will be compressed immediately. Reviewed both my original and uploaded image.

    Same goes for the Google crawler. Reviewed the original plus the one stored at Google images.
     
  15. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    current forum's ngx_pagespeed image stats ~55.9% reduction in image size served

    upload_2016-10-11_23-41-41.png
     
  16. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
    yeah well that's part of the 'no free lunch' cost for page load speed :D
     
  17. hendry

    hendry Member

    59
    13
    8
    Sep 19, 2016
    Ratings:
    +17
    Local Time:
    10:51 AM
    nginx/1.11.4
    MariaDB 10
    here they are
    Code (Text):
    [root@server1 centminmod]# which wget
    alias wget='/usr/local/bin/wget'
            /usr/local/bin/wget
    [root@server1 centminmod]# wget -V
    GNU Wget 1.18 built on linux-gnu.
    
    -cares +digest -gpgme +https +ipv6 +iri +large-file -metalink +nls
    +ntlm +opie -psl +ssl/openssl
    
    Wgetrc:
        /usr/local/etc/wgetrc (system)
    Locale:
        /usr/local/share/locale
    Compile:
        /opt/rh/devtoolset-3/root/usr/bin/gcc -DHAVE_CONFIG_H
        -DSYSTEM_WGETRC="/usr/local/etc/wgetrc"
        -DLOCALEDIR="/usr/local/share/locale" -I. -I../lib -I../lib -I
        /usr/local/include -DHAVE_LIBSSL -DNDEBUG -O2 -g -pipe -Wall
        -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
        --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic
    Link:
        /opt/rh/devtoolset-3/root/usr/bin/gcc -I /usr/local/include
        -DHAVE_LIBSSL -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2
        -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4
        -grecord-gcc-switches -m64 -mtune=generic -L /usr/local/lib -lpcre
        -lssl -lcrypto -lz -lidn -lrt ftp-opie.o openssl.o http-ntlm.o
        ../lib/libgnu.a
    
    Copyright (C) 2015 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later
    <[URL]http://www.gnu.org/licenses/gpl.html>[/URL].
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Originally written by Hrvoje Niksic <hniksic@xemacs.org>.
    Please send bug reports and questions to <bug-wget@gnu.org>.


    Code (Text):
    [root@server1 centminmod]# ps aufxw
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.0  19236   816 ?        Ss   Oct10   0:00 init
    root         2  0.0  0.0      0     0 ?        S    Oct10   0:00 [kthreadd/8828]
    root         3  0.0  0.0      0     0 ?        S    Oct10   0:00  \_ [khelper/8828]
    root       134  0.0  0.0  10644   224 ?        S<s  Oct10   0:00 /sbin/udevd -d
    root       544  0.0  0.0 218748   884 ?        Sl   Oct10   0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
    dbus       610  0.0  0.0  21440   312 ?        Ss   Oct10   0:00 dbus-daemon --system
    root       636  0.0  0.0  66240   444 ?        Ss   Oct10   0:00 /usr/sbin/sshd
    root     17063  0.0  0.0  98564  1356 ?        Ss   04:29   0:01  \_ sshd: root@pts/0
    root     17069  0.0  0.1  13552  2136 pts/0    Ss   04:30   0:00      \_ -bash
    root     13488  0.0  0.0  13372  1016 pts/0    R+   14:00   0:00          \_ ps aufxw
    nobody    1138  0.0  0.0 288808   652 ?        Ssl  Oct10   0:14 /usr/local/bin/memcached -d -m 8 -l 127.0.0.1 -p 11211 -c 2048 -b 2048 -R 200 -t 4 -n 72 -f 1.25 -u nob
    root      1167  0.0  0.0  90976   804 ?        Ss   Oct10   0:01 sendmail: accepting connections
    smmsp     1176  0.0  0.0  78256   592 ?        Ss   Oct10   0:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
    root      1218  0.0  0.0  20040   544 ?        Ss   Oct10   0:00 crond
    root      1226  0.0  0.0   4068   312 tty1     Ss+  Oct10   0:00 /sbin/mingetty console
    root      1227  0.0  0.0   4068   312 tty2     Ss+  Oct10   0:00 /sbin/mingetty tty2
    root     11770  0.0  0.3  69712  6456 ?        Ss   00:00   0:04 lfd - sleeping
    root     17213  0.0  0.0 506788   780 ?        Ss   04:34   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
    root     12116  0.0  0.0   9348  1484 pts/0    S    12:22   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/server1.svetogama.com.p
    mysql    12470  0.0  3.5 566548 75244 pts/0    Sl   12:22   0:03  \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --use
    root     12734  0.0  0.1 185756  2356 ?        Ss   12:25   0:00 pure-ftpd (SERVER)


    OMG i can't start nginx again. I don't see nginx proses after I stoped nginx.

    Thanks for the suggest but this is for my knowledge or maybe others who use centmin mod too. btw if we have site that user can upload an image, I think we need this module. So it's impossible for me if I must download all user images and upload it again.
     
    Last edited: Oct 12, 2016
  18. eva2000

    eva2000 Administrator Staff Member

    28,929
    6,567
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,747
    Local Time:
    1:51 PM
    Nginx 1.13.x
    MariaDB 5.5
  19. hendry

    hendry Member

    59
    13
    8
    Sep 19, 2016
    Ratings:
    +17
    Local Time:
    10:51 AM
    nginx/1.11.4
    MariaDB 10
    Code (Text):
    [root@server1 centminmod]# which pcretest
    /usr/local/bin/pcretest
    You have new mail in /var/spool/mail/root
    [root@server1 centminmod]# pcretest -C
    PCRE version 8.39 2016-06-14
    Compiled with
      8-bit support
      UTF-8 support
      16-bit support
      UTF-16 support
      32-bit support
      UTF-32 support
      Unicode properties support
      Just-in-time compiler support: x86 64bit (little endian + unaligned)
      Newline sequence is LF
      \R matches all Unicode newlines
      Internal link size = 2
      POSIX malloc threshold = 10
      Parentheses nest limit = 250
      Default match limit = 10000000
      Default recursion depth limit = 10000000
      Match recursion uses stack
    
     
  20. bassie

    bassie Active Member

    491
    103
    43
    Apr 29, 2016
    Ratings:
    +310
    Local Time:
    5:51 AM
    Pagespeed is more expensive as it does store the original image on the disk and the optimized image on the disk or memory. So double usage.

    Where is the benchmark of pre-compressed images? You can't compare Pagespeed to .......
    With the above Compressor.io or Kraken you can reduce image size served even more then Pagespeed ever can. I would say try it out.