Nginx error 413: Request entity too large Quick Fix

A quick article today about a common error people encounter with Nginx: the infamous 413 Request Entity Too Large HTTP error. I had the problem myself just yesterday on one of my newly configured servers, so I thought I'd make a blog post about it to make sure I don't forget this next time.

Error 413: what does it mean?

This error shows up when a visitor sends too much data in the HTTP request. This is normally caused by:
• an overy large file being uploaded by the visitor
• more generally too much POST data being sent by the client
The fix is as simple as setting a directive in your Nginx configuration, read on below.

How to fix it? client_max_body_size

To fix this, you need to increase the value of the client_max_body_size directive. This directive defines the maximum amount of data Nginx will accept in an HTTP request. By default this value is set to 1 megabyte, meaning if you attempt to upload a file larger than 1 megabyte you'll be getting an Error 413: Request entity too large page. You can insert this directive at three levels:

  • In the http block: this will set the directive value for all server and locations in your configurationn
  • In the server block: this will set the directive value for all locations of one particular server
  • In the location block: this will set the directive value for one specific location in a particular server
In this example I'm going to insert it in my http block and set it to 500 megabytes:
http {
    client_max_body_size 500M; # allows file uploads up to 500 megabytes

Anything else I need to do?

After changing your server configuration, don't forget to reload Nginx for the configuration to apply. There are also some configuration settings you need to change if you are using PHP. If you aren't, just ignore the rest of this post. You'll need to open your php.ini configuration file and look for two values:

  • upload_max_filesizeMaximum allowed size for uploaded files (default: 2 megabytes). You need to increase this value if you expect files over 2 megabytes in size.
  • post_max_sizeMaximum size of POST data that PHP will accept (default: 8 megabytes). Files are sent via POST data, so you need to increase this value if you are expecting files over 8 megabytes.

After making the changes, don't forget to reload PHP-FPM as well so that the changes are applied correctly. That's all! You shouldn't be getting any more upload errors after that.


Top 5 fixes for: Excel 2013 has stopped working - also works for Word, Powerpoint, Outlook...

Recently I've started encountering a particularly annoying problem with Office 2013. Every time I'd open Excel 2013 under Windows 8, it would crash instantly, leaving me a message: "Excel 2013 has stopped working". I looked around and found a few solutions, but nothing worked for me. Eventually, I managed to fix the problem all by myself. I'm going to describe the steps that I've tried here. Note that this post is also valid for messages such as:
  • Word 2013 has stopped working
  • Powerpoint 2013 has stopped working
  • Excel 2013 has stopped working
  • Outlook 2013 has stopped working
  • Office 2013 has stopped working (insert any other Office tool here).
  • Most of these solutions are also valid for Office 2007 and Office 2010 applications.

Solution 1: change the default printer

This solution fixed all my problems, so I'm going to describe it first. Also it's the simplest! Try this first before anything else, because the next solutions are a bit more complex. What we're doing here is just changing the default printer. 

Why does this fix our problem? Because when you open an Office document, such as an Excel spreadsheet or a Word document, the application attempts to communicate with the printer to figure out the margins supported by the default printer. If that communication fails, the application (ie. Excel) crashes. All you have to do is change the default printer. Below is a quick guide to do that.

First, close all open Office applications such as Excel, Outlook and whatnot. Then, go to the "Devices and Printers" window. If you don't know how to open that, just go to the Control Panel and you'll find the "Devices and Printers" applet.

Righ-click on "Microsoft XPS Document Writer", for example, and choose "Set as default printer". Now open Excel again and voila: the document opens fine, and the margins are correctly defined.

Solution 2: start in safe mode, disable add-ins

While the above solution worked for me, I've also experienced add-in problems in the past; some add-ins caused some of my Office applications to crash for no apparent reason. So if the above fix doesn't help you, try this one, it's likely that this will work for you.

What we're going to do here: start Excel (or Word, Powerpoint, Outlook) in Safe Mode so that it doesn't load any add-in potentially crashing the application. Then from the Safe Mode we'll disable add-ins one by one and see if this helped.

To start one of these applications in safe mode, first press the Windows+R key combination to get to the "Run..." window:
In the text box that appears, type one of those commands depending on the application you're trying to fix:
  • Excel.exe /safe
  • Winword.exe /safe
  • Outlook.exe /safe
  • Powerpoint.exe /safe
Next, go to the "File" menu (the colored menu bar at the top left of the application that says "File"). Then in the menu on the left, click "Options". In the options window, click "Add-Ins" at the bottom. You should see a window like this:

 At the bottom of this window, there is a "Manage: [_____]" option list. Select "COM Add-ins", and click the button "Go..." that's just on the right. You'll be seeing a list of add-ins:

Make sure all boxes are unchecked, and press "OK". Restart the application normally and see if this fixed it for you. If it did; it's likely that one of these add-ins is causing the problem. Simply repeat this process and re-enable add-ins one by one until you find the one that's causing the issue.

Solution 3: start the application as Administrator

I've read that this solution worked for a couple of people, so I'm reporting the information here; hopefully this will help. Apparently in some cases all you need to do is start Excel/Word/Powerpoint/Outlook as Administrator in order to get permissions to do something on the computer. If you're using Windows 7, right-click the application in the Start Menu and click "Run as administrator". See if this works for you. 

In Windows 8, open the Home screen, find the application you're trying to open; right-click it and select "Open as administrator" at the bottom.

Solution 4: repair Office 2013

In some cases, it may just happen that one of the files necessary to the well-functioning of your Office 2013 applications has been damaged, deleted or corrupted after a brutal shutdown for example. So it might be useful to attempt an automatic repair. There's actually an option for that, and here's how to access it.

First, open the Control Panel, then select "Programs and Features". Find "Microsoft Office 2013" in the list, and click the "Change" button at the top:

You will be presented with an option to automatically "Repair" the Office 2013 program files, which might actually help towards fixing your problem.

Solution 5: fully uninstall and reinstall Office 2013

In the most severe cases, the only solution you'll have is to fully uninstall Office 2013 from your computer, wiping it clean of all tracks the programs might have left on your Windows set-up. Rest assured you won't need to do this manually; Microsoft are offering a program for that: an Office 2013 uninstaller. So just open this page-- click here, scroll down until you see this button:

Click it to begin downloading the uninstall tool. Once the uninstall is complete, restart your computer, then proceed to reinstalling Office 2013.


Microsoft did it again: they've managed to make an application suite as unstable as Internet Explorer! And they aren't even providing proper solutions for it, or even fixing the problem at all. If your Office 2013 apps still crash after you've tried all of the above, then sue Microsoft... just kidding, maybe ask for a refund I suppose. Also: if you've heard of any other solution to fix this sort of problem, please post a comment here and I'll see about integrating it in my post.


Access a DBISAM database with PHP via ODBC

Recently I've been needing to access a DBISAM database created by third-party application, in order to export some of its data. Initially, I didn't even know what format the database was; I'd just found a bunch of files with the following extensions: .dat, .idx, .blb, .ibk, .bbk, and more. After a quick Google search, I stumbled upon a few pages that led me to DBISAM from Elevate Software. It's a proprietary database format that doesn't require a server application, somewhat similar to SQLite.

Accessing the database from PHP was quite complex because I didn't have a proper reference or blog article to detail the process. That's exactly why I'm writing this: to give people directions on a subject that's hardly documented at all. In this tutorial, you'll learn how to access the database via PHP, execute a simple SQL query, and display the result.

ODBC driver and connection

I almost forgot: there's a catch-- even two, and here's why. In order for PHP to be able to communicate with this proprietary database system, you'll need to install an ODBC driver. First catch: as far as I know, the driver is only available for Windows. Second catch: while you'll be able to download the trial version free of charge, it'll cost you $250 to get the full unlocked driver. Right now I'm using the trial version, and I'll probably purchase the retail product when the trial is over.

As I said, we're going to have to use Windows to access our DBISAM database, and I'd highly recommend WAMP for your web server/PHP setup. Begin by registering at Elevate Software and download the trial version of the ODBC driver the install it. To make sure it installed correctly, go to the Windows start menu and look for the "ODBC data sources" program, open the 32-bit version preferably. In the window that appears, click on the "Add..." button and you should see a window like this:

This is a list of data source drivers, among which you should find "DBISAM 4 ODBC Driver". Now we're not going to do anything with this for the time being -- we're just checking that the driver installed correctly; so just click "Cancel" and close the ODBC data sources program.

Connecting to the database

In order to be able to connect to an ODBC data source, your PHP setup must have the ODBC extension enabled. If you are using WAMP as I recommended previously, it should already be there and enabled, as it comes built-in with the PHP distribution from WAMP. However if you're using any other form of PHP setup, you need to run the phpinfo() function and see if you find ODBC in the list of enabled extensions. Check the PHP.net documentation for more information on setting up the ODBC extension.

Now we've got that cleared up, we can actually write our first script that will connect to the DBISAM database. Remember: a DBISAM database has no server, you just need to indicate the path of the database files; the ODBC driver will do the rest (I/O operations).

The function that interests us here is odbc_connect(). It takes three parameters: the DSN (data source name), a login, and a password. Here's a tip: by default, the DBISAM login is "admin" and the password is blank (empty password). At least this worked for me. Now this leaves us to figure out the DSN.

Here is the DSN I am currently using: DRIVER={DBISAM 4 ODBC Driver};ConnectionType=Local;CatalogName=c:/wamp/www/odbc/data/;
There are three components:

  • The DRIVER: you must specify the exact name of the driver as it appears in the "ODBC data sources" program that I had you open above (see screenshot above). 
  • The ConnectionType: it's set to Local because we're reading files from our local computer, not remotely.
  • The CatalogName: simply specify the path of the folder that contain your database files. Use normal slashes instead of backslashes.

So, this is the full command that we'll use to establish the connection:
$db = odbc_connect("DRIVER={DBISAM 4 ODBC Driver};ConnectionType=Local;CatalogName=c:/wamp/www/odbc/data/;","admin","");
If you get a warning, something probably went wrong. Check the driver name, the database path, make sure you typed everything correctly. If no error or warning shows up, you're good, and $db will contain a resource identifier.

Executing a query and displaying its contents

If you've already used MySQL or other database providers before, this will be a piece of cake. The principles are similar: execute an SQL query, run a while loop fetching the results. Let's assume you have a "customers" table; in practice this means you should see these two files in your data folder: customers.dat and customers.idx (there may be more files though - blb, blk, etc.). Here's the full code sample that I wrote.

$db = odbc_connect("DRIVER={DBISAM 4 ODBC Driver};ConnectionType=Local;CatalogName=c:/wamp/www/odbc/data/;","admin","");
$res = odbc_exec($db,"SELECT * FROM customers");
echo odbc_num_rows($res)." rows found";
while($row = odbc_fetch_array($res)) {

This code sample loops through all the records of the customers table and displays them in the current page. That's it! Now all you have to do is work your brains and write the appropriate SQL queries. If you have any questions on the subject I'd be happy to answer them.

Search This Blog