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

Setting up a default catch-all virtual host


Once you are comfortable setting up the virtual hosts, you will end up in a situation where you have a lot of domains pointing at the IP. In addition to the domains, you would also have the web server responding to the IP addresses it hosts, and many other unused subdomains of the domains pointing at it. We can take a look at this with a simple example, so you have http://www.example1.com pointing at the IP address, you have configured a virtual host to handle the domains www.example1.com and example1.com. In such a scenario, when the user types in abc.example1.com or an IP address the web server will not be able to serve the relevant content (be it 404 or some other promotional page).

How to do it...

For situations like the one above, one can utilize the default catchall virtual host that Nginx provides; here is a simple example where this default catchall virtual host serves a simple set of web pages.

The following is the code for sites-enabled/default.conf:

server {
	listen   80 default; 
 	server_name  _;
	location / {
		root /var/www/default;
		index index.html index.htm;
	}
}

How it works...

The key thing to note is the fact that you are listening on the default port and that the server_name is "_" which is the catchall mechanism. So whenever the user enters a domain for which you have no defined virtual host, pages will get server from the /var/www/default directory.