Book Image

Nginx 1 Web Server Implementation Cookbook

By : Dipankar Sarkar
Book Image

Nginx 1 Web Server Implementation Cookbook

By: Dipankar Sarkar

Overview of this book

<p>Nginx is an open source high-performance web server, which has gained quite some popularity recently. Due to its modular architecture and small footprint, it has been the default choice for a lot of smaller Web 2.0 companies for use as a load-balancing proxy server. It supports most of the existing back-end web protocols like FCGI, WSGI, and SCGI. This book is for you if you want to have in-depth knowledge of the Nginx server.<br /><br /><i>Nginx 1 Web Server Implementation Cookbook</i> covers the whole range of techniques that would prove useful for you in setting up a very effective web application with the Nginx web server. It has recipes for lesser-known applications of Nginx like a mail proxy server, streaming of video files, image resizing on the fly, and much more.<br /><br />The first chapter of the book covers the basics that would be useful for anyone who is starting with Nginx. Each recipe is designed to be independent of the others.<br /><br />The book has recipes based on broad areas such as core, logging, rewrites, security, and others. We look at ways to optimize your Nginx setup, setting up your WordPress blog, blocking bots that post spam on your site, setting up monitoring using munin, and much more.</p> <p>Nginx 1 Web Server Implementation Cookbook makes your entry into the Nginx world easy with step-by-step recipes for nearly all the tasks necessary to run your own web application.</p>
Table of Contents (17 chapters)
Nginx 1 Web Server Implementation Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Increasing the size of uploaded files


Usually when you are running a site where the user uploads a lot of files, you will see that when they upload a file which is more than 1MB in size you get an Nginx error stating, "Request entity too Large" (413), as shown in the following screenshot. We will look at how Nginx can be configured to handle larger uploads.

How to do it...

This is controlled by one simple part of the Nginx configuration. You can simply paste this in the server part of the Nginx configuration:

client_max_body_size 100M; # M stands for megabytes

This preceding configuration will allow you to upload a 100 megabyte file. Anything more than that, and you will receive a 413. You can set this to any value which is less than the available disk space to Nginx, which is primarily because Nginx downloads the file to a temporary location before forwarding it to the backend application.

There's more...

Nginx also lets us control other factors related to people uploading files on the web application, like timeouts in case the client has a slow connection. A slow client can keep one of your application threads busy and thus potentially slow down your application. This is a problem that is experienced on all the heavy multimedia user-driven sites, where the consumer uploads all kinds of rich data such as images, documents, videos, and so on. So it is sensible to set low timeouts.

client_body_timeout 60; # parameter in seconds
client_body_buffer_size 8k;
client_header_timeout 60; # parameter in seconds
client_header_buffer_size 1k;

So, here the first two settings help you control the timeout when the body is not received at one read-step (basically, if the server is queried and no response comes back). Similarly, you can set the timeout for the HTTP header as well. The following table lists out the various directives and limits you can set around client uploading.

Directive

Use

client_body_in_file_only

This directive forces Nginx to always store a client request body in temporary disk files, even if the file size is 0.

The file will not be removed at request completion.

client_body_in_single_buffer

This directive specifies whether to keep the whole body in a single client request buffer.

client_body_buffer_size

This directive specifies the client request body buffer size.

If the request body is more than the buffer, then the entire request body or some part is written in a temporary file.

client_body_temp_path

This directive assigns the directory for storing the temporary files in it with the body of the request.

client_body_timeout

This directive sets the read timeout for the request body from client.

client_header_buffer_size

This directive sets the header buffer size for the request header from client.

client_header_timeout

This directive assigns timeout with reading of the title of the request of client.

client_max_body_size

This directive assigns the maximum accepted body size of client request, indicated by the line Content-Length in the header of request.