Tag Archives: Nginx

Resolving Nginx 413 Request Entity Too Large Error

You may receive the error 403 “Request Entity Too Large Error” when trying to upload a large file in Nginx. By default, nginx only allows files less than 1 MB. To increase that limit, you will have to add the following to your nginx.conf file:

client_max_body_size 4M;

The number 4 should be changed to however megabytes you want the limit to be. Do note it’s 4M for 4 megabytes, rather than 4MB. After having added that to your configuration file, you should restart nginx for this change to take effect. Do note the command may differ, but should be the following if you installed from a RPM/DEB package or setup the startup script from our Nginx Startup Script post.

/etc/init.d/nginx restart

Even after increasing nginx’s upload limit, the PHP limit may also be limiting how large of a file you can upload. You will want to change the post_max_size and upload_max_filesize values in your php.ini file as well. Note that if you make a change here, most likely you will have to restart the spawn-fcgi process to make the change live.

Nginx Change Server Header

A while ago, I posted about an Nginx Startup Script and installing Nginx with PHP on Centos. Today, I will explain how to modify the server header. There are many reasons to do so, partly due to the fact that nginx has relatively more bug than Apache. Note that in order to modify the server header, you will need to build nginx from source since nginx doesn’t provide an option to change it with the configuration files.

If you only want to remove the version, you just need to add the following code to your configuration:

server_tokens off;

To start, download the nginx source code. Open the src/http/ngx_http_header_filter_module.c file, and find:

static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

Now, you can change the server to anything you wish. For example, you can pretend to use Apache by changing it to the following code:

static char ngx_http_server_string[] = "Server: Apache" CRLF;
static char ngx_http_server_full_string[] = "Server: Apache" CRLF;

Nginx with PHP FastCGI on CentOS

Nginx is a lightweight alternative to Apache, and has nearly all the features of Apache. Nginx uses much less memory and CPU compared to Apache, and is slightly faster than Lighttpd. To get started, make sure you are using EPEL. If you’re not sure or if you’re not, run the following command:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

Download and install nginx:

yum install nginx

Now, edit the nginx configuration file, which can be found at /etc/nginx/conf.d. Modify it to the following:

worker_processes  1;

events {
	worker_connections 1024;
}

http {
	include mime.types;
	default_type text/plain;
	sendfile on;
	keepalive_timeout 15;
	gzip on;
	server {
		listen 80;
		server_name  localhost;
		location / {
			root /home;
			index index.php;
		}
		location ~ .php$ {
			root html;
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_index index.php;
			fastcgi_param SCRIPT_FILENAME /home$fastcgi_script_name;
			include fastcgi_params;
		}
	}
}

You will need to modify root and fastcgi_param SCRIPT_FILENAME to your home directory. Note that either one end with a slash. After you’ve done all of above, nginx will be all set. Now, you have to install FastCGI.

yum install spawn-fcgi

Download the PHP FastCGI startup script.

wget //www.fusionswift.com/files/2010/02/php_cgi.sh.zip
unzip php_cgi.sh.zip
mv php_cgi.sh /etc/init.d/php_cgi
chmod 0755 /etc/init.d/php_cgi

Start nginx and PHP FastCGI.

service nginx start
service php_cgi start

Now you’re all set! If you want to accelerate PHP execution, be sure to check out XCache.