• No results found

Static Pages

In document + LET’S ENCRYPT (Page 35-38)

,ETS START EXPLORING NGINX BY INSTALLING THE NGINX LITE PACKAGE UNDER Ubuntu, then looking at the configuration and how you can get a basic static site running.

&IRST )M GOING TO INSTALL THE NGINX CORE PACKAGE

$  sudo  apt-­get  install  nginx-­core

I then can start the server with the fairly standard shell command:

$  sudo  service  nginx  start

After a few moments, nginx will have started, as I can tell either by typing this:

$  sudo  serviced  nginx  status to which I get the response:

nginx  is  running

And if I go to the home page on my current server, I’m greeted by, h7ELCOME TO NGINXv

But of course, I’d really like to have my own content there. Let’s take

a look at the configuration file, which is in /etc/nginx/nginx.conf on my system, and see how it’s formatted and how to change it to make some custom static content.

Now, if you’re used to Apache configuration files, the style of nginx’s file is going to take some getting used to. Like Apache, each line contains a CONFIGURATION SETTING IN A NAME VALUE STYLE 5NLIKE !PACHE THE SECTIONS ARE DELIMITED USING CURLY BRACES [ ] AND EACH LINE MUST END WITH A SEMICOLON   For example, the first line in my installed, default nginx configuration file is:

user  www-­data;;

4HIS MEANS NGINX WILL RUN AS THE WWW DATA USER WHICH IS PRETTY standard in the world of Ubuntu (and Debian). Next comes the configuration parameter:

worker_processes  4;;

This describes how many processes nginx should launch when running.

But, it would seem to contradict what I wrote above, namely that nginx uses only a single process (and no threads within that process) for extra SPEED NO 7ELL YES AND NOˆTHE IDEA IS THAT YOULL PROBABLY WANT TO have one nginx worker process per CPU core on your server. On this server, I have four cores, each of which can (and should) have an nginx WORKER PROCESS 9OU CAN THINK OF THIS AS A ONE COMPUTER VERSION OF A LOAD BALANCER DISTRIBUTING THE LOAD ACROSS THE AVAILABLE #05S %ACH WORKER process can and will handle a large number of network connections.

)F YOUR SERVER WILL BE RUNNING MORE THAN JUST NGINXˆFOR EXAMPLE IF YOU ARE RUNNING A DATABASE SERVER ON THE SAME MACHINEˆYOU LIKELY WILL WANT to reduce this number, so that at least one core is always available for those other processes.

4HE DEFAULT CONFIGURATION FILE THEN CONTAINS AN hEVENTSv SECTION

events  {  

     worker_connections  768;;  

     #  multi_accept  on;;  

}

In this, I set worker_connectionsˆMEANING HOW MANY NETWORK

connections can each worker process handle simultaneously? In this case, ITS SET TO  )M NOT SURE WHERE THIS NUMBER COMES FROM BUT IT MEANS that if my site becomes popular, I might find that I run out of network connections. You might well want to raise this number.

The multi_accept directive, which is commented out by default, is ALSO SET TO hONv BY DEFAULTˆMEANING THAT NGINX IS WILLING TO ACCEPT NEW connections as they arrive, handling more than one at a time. I can’t think of a good reason to turn this off.

.EXT IS AN hHTTPv SECTION WHICH YOU WONT BE SURPRISED TO HEAR HAS TO do with HTTP connections made to the system.

Most of these configuration directives aren’t going to be of interest RIGHT AWAY AS YOU CAN SEE NGINXS LOGGING DIRECTIVES ARE SIMILAR TO THOSE in Apache and other servers:

access_log  /var/log/nginx/access.log;;  

error_log  /var/log/nginx/error.log;;

Where is the location of the site defined? In the case of nginx, ITS NOT DIRECTLY WITHIN THE hHTTPv BLOCK 2ATHER ITS INSIDE ANOTHER CONFIGURATION FILEˆOR MORE ACCURATELY A SET OF CONFIGURATION FILES FOR the sites configured on the server:

include  /etc/nginx/sites-­enabled/*;;

Because I’m using a fresh installation of nginx on a computer that hasn’t been used for other things yet, there is only a single server

configured. You easily can imagine a situation in which a single computer is configured to work with dozens, or even hundreds, of different sites, each of which will have its own configuration file. In this case, however, )LL JUST WORK WITH THE hDEFAULTv SERVER DEFINED HERE

/etc/nginx/sites-­enabled/default

4HIS FILE STARTS WITH A hSERVERv SECTION DESCRIBING A SINGLE PORT ON WHICH nginx should be listening. This means if you want to listen on multiple

PORTSˆFOR EXAMPLE ON PORT  FOR (440 AND PORT  FOR (4403ˆYOULL NEED TO CONFIGURE THOSE IN SEPARATE BLOCKS 4HIS hSERVERv BLOCK OPENS with the following:

listen  80  default_server;;

This means that it’s going to be listening to port 80, and that this is the default server for the system. Consider a computer on which nginx is running, which is hosting several dozen sites using virtual hosts. Using default_server, you can tell nginx which site will ACCEPT REQUESTS FOR NAMES THAT ARENT OTHERWISE CLAIMED BY ANOTHER virtual host.

Finally, here are the two lines that tell nginx where to look for my files:

root  /usr/share/nginx/html;;  

index  index.html  index.htm;;

The root directive tells nginx in which directory to look. And the index DIRECTIVE INDICATES THAT IF SOMEONE ASKS FOR THE DIRECTORYˆIN THIS CASE THE SIMPLE 52, hvˆWHICH FILE SHOULD BE SERVED

So, I know that to modify my (current, default) static Web site, I need to edit the file /usr/share/nginx/html/index.html. And sure enough, if I look in that location on my server’s filesystem, I see the h7ELCOME TO NGINXv FILE "Y CHANGING THAT FILE ) CAN CHANGE WHAT MY site looks like.

In document + LET’S ENCRYPT (Page 35-38)