nginx redirecting http requests to https

If you have your nginx server setup with valid SSL certificates to support https, you can redirect all http incoming requests to https with a simple 301 ‘Permanently Moved’ response to the client.

Returning a 301 is considered best practice for enforcing an http to https upgrade.

This post covers this approach, which involves configuring a server block for port 80 to always respond with a 301 response including the https url:

server {
    listen 80;
    listen [::]:80;
    server_name www.example.com; 
    return 301 https://www.example.com$request_uri;
}

The example config in the above article also shows how to redirect first to your server defined with no subdomain, and then additionally redirect from the request with no subdomain to your www subdomain, e.g from http://www.example.com to https://example.com then to https://www.example.com. Apparently the way nginx caches server names there’s performance advantages to redirect to a no-name subdomain first. Redirecting directly to your www subdomain obviously still works too.

Increasing media upload size for WordPress / PHP5

On a default WordPress 4.8 install on PHP5 the default file upload size seems to be (at least on my install) 2MB. If you’re uploading large image files or videos, it’s likely you’ll want to upload files much larger than this.

There’s a number of possible configuration locations that can control/configure the upload size. The first location is likely to be your php.ini file used by your apache or nginx. The settings to increase are:

  • post_max_size (must be larger than upload_max_size)
  • upload_max_filesize
  • memory_limit

This article has a comprehensive walkthrough the possible configuration changes.

Comparing nginx memory usage to apache2

I’ve trimmed down my apache conf to work ok for a small site, but after uploading my exported data from my old site to my new, the 4x apache2 processes have grown considerably and consumed all my VPS memory. Knowing nginx has a much lighter footprint, I wondered what it would look like in comparison.

Here’s the memory usage after completing my exported data file imports – I get that after completing this imports to the new site this is the memory usage of the apache processes after load from importing about 20x 10MB xml export files. At idle after a restart the memory usage does start considerably lower, but here’s where I’m at right now:

$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
32.2  1.7 169228 904300 /usr/sbin/mysqld
24.5  1.2 128684 385360 /usr/sbin/apache2 -k start
20.2  0.2 105968 355004 /usr/sbin/apache2 -k start
18.4  4.0 96740 352540 /usr/sbin/apache2 -k start
 2.5  0.0 13144 283312 /usr/sbin/apache2 -k start

So following the guide here just to install nginx and the fastcgi php, and then here for WordPress specific config, here’s what it looked like on restart:

$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
 9.3  0.4 49124 576488 /usr/sbin/mysqld
 6.0  0.0 31516 235504 php-fpm: pool www                                                       
 4.5  0.0 23860 232660 php-fpm: pool www                                                       
 2.6  0.0 14032 230016 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
 0.5  0.0  2804  86472 nginx: worker process
 0.3  0.0  2004  86128 nginx: worker process
 0.3  0.0  2004  86128 nginx: worker process
 0.3  0.0  1988  86128 nginx: worker process
 0.3  0.0  1900  33188 init
 0.2  0.0  1452  85832 nginx: master process /usr/sbin/nginx

This is looking better, still need to work on getting the mysql usage down, but at least now I’m not maxed out:

$ free
             total       used       free     shared    buffers     cached
Mem:        524288     431192      93096      68180          0     259440
-/+ buffers/cache:     171752     352536
Swap:            0          0          0