Home›Trust & Compliance›Backups and private files on nginx hosts

Backups and private files on nginx hosts

A database backup holds your customers, their addresses and every order. TitanCart writes backups to disk so you can download or restore them from Settings → Site Backup, and the one thing that must never happen is a backup being fetched by anyone who types the right address into a browser. This article explains where the files go, how TitanCart checks that they are private, and what to do when the check says they are not.

Site Backup is part of TitanCart Pro. If you run TitanCart Lite there is no backup page and nothing in this article applies to you.

Why an .htaccess is not enough

For years the WordPress habit for private files was a folder under wp-content/uploads with an .htaccess file that says deny from all. That works on Apache, because Apache reads .htaccess files. It does nothing on nginx, which never reads them. Many managed hosts, most “LiteSpeed or nginx” plans, and Plesk servers with Serve static files directly by nginx switched on hand the file straight to the visitor. A backup named after its date and time is easy to guess.

Since TitanCart 0.2.94 backups no longer rely on that file alone.

Where TitanCart keeps backups

On its first run after the update TitanCart picks a private folder once, in this order, and remembers the choice:

  1. A folder you name. If TITANCART_PRIVATE_DIR is defined in wp-config.php and the folder can be written, that folder is used. This is the choice for hosts with a known private area, such as Plesk’s /var/www/vhosts/yourdomain/private or the home directory above public_html on cPanel.
  2. Above the web root. Otherwise TitanCart tries the folder one level above your WordPress installation, in a sub-folder called titancart-private. On most servers that folder is outside what the web server can reach at all, so there is no address to guess. TitanCart skips this step when the folder above WordPress is itself public, which is the case when WordPress lives in a sub-folder of your site.
  3. A secret folder under uploads. When neither works, TitanCart creates wp-content/uploads/titancart-private-32 random characters/. The random name is never printed in a page or a link, and the folder carries an .htaccess in both Apache syntaxes plus an empty index.php so it cannot be listed. This is the weakest of the three, which is why it comes last, and it is the only case TitanCart tests over the network.

Backups that already existed in the old location, wp-content/uploads/titancart/backups/, are moved into the new folder the first time the Site Backup page is opened after the update. The old folder is left in place but empty of backups.

Downloads never use a direct link. Clicking Download on the Site Backup page asks TitanCart for a 15-minute ticket tied to your admin login, and the file is streamed through that ticket. That was already true before 0.2.94; what changed is where the file waits in between.

The note on the Site Backup page

Under the three figures at the top of Settings → Site Backup a line now tells you where the files live. It reads one of four ways:

  • “Backups are stored outside the web root at …” — nothing to do. The folder has no web address.
  • “Backups are stored at the folder TITANCART_PRIVATE_DIR names: …” — nothing to do; you chose the folder.
  • “Backups are stored in a folder with a secret name under uploads … The folder refused a direct request” — the folder is under uploads, and when TitanCart wrote a small probe file and requested it over HTTP the server said no. The .htaccess is being honoured. The line still suggests TITANCART_PRIVATE_DIR, because a hosting change could switch that off without warning. A Check again button repeats the probe; otherwise it runs once a day.
  • A red warning: “Your web server serves the backup folder by URL …” — the probe came back. The server ignores the .htaccess, so the secret folder name is the only thing between a visitor and your backups. Fix it with either of the two steps below, then click Check again.

Fix 1: name a folder outside the web root

This is the better fix because it removes the web address altogether. Create a folder the PHP user can write that is not under your document root, then add one line to wp-config.php above the That’s all, stop editing! comment:

define( 'TITANCART_PRIVATE_DIR', '/var/www/vhosts/yourdomain.com/private/titancart' );

Use your own path. On Plesk the private folder beside httpdocs already exists and is yours; on cPanel a folder in your home directory beside public_html works the same way. Reload the Site Backup page: the note changes to the “TITANCART_PRIVATE_DIR names” wording, and existing backups move across on that first load.

Fix 2: fence the folder in nginx

If you cannot write outside the web root, tell nginx to refuse the folder. On Plesk open Websites & Domains → your domain → Apache & nginx Settings → Additional nginx directives and paste:

location ^~ /wp-content/uploads/titancart-private- { deny all; }
location ^~ /wp-content/uploads/titancart/backups/  { deny all; }

The first line covers the secret-named folder whatever its random suffix is; the second covers the pre-0.2.94 location in case a copy of an old backup is ever left there. On a host without Plesk, add the same two lines inside the server { } block of your site’s nginx configuration and reload nginx. Then click Check again on the Site Backup page: the warning should turn into the “refused a direct request” wording.

Nothing on your site links to either folder, so the rule cannot break a download: the Site Backup page streams files through its ticket, never through these addresses.

Extensions that store private files

Some TitanCart extensions keep files that are delivered only after a check of their own, such as paid downloads or support ticket attachments. Those extensions are adopting the same private folder in their next releases; until then, the folders they use under wp-content/uploads deserve the same nginx deny all treatment on an nginx host. Each extension’s own article names its folder.

See also

Previous
Security headers and a Content-Security-Policy for your store