Mastering Nginx

(Ron) #1

The NGINX HTTP Server


[ 118 ]

In this example, the http://www.example.com server will have the server_namein


redirect directive set to on as well as the default.example.com server. Note that
this would also work if both servers had no listen directive, since they would still


both match the same IP address and port number (that of the default value for listen,
which is *:80). Inheritance, though, is not guaranteed. There are only a few directives


that are inherited, and which ones are changes over time.


A better use for the default server is to handle any request that comes in on that IP


address and port, and does not have a Host header. If you do not want the default
server to handle requests without a Host header, it is possible to define an empty


server_name directive. This server will then match those requests.


server {

server_name "";

}

The following table summarizes the directives relating to server:


Table: HTTP server directives

Directive Explanation
port_in_redirect Determines whether or not the port will be
specified in a redirect issued by NGINX.
server Creates a new configuration context,
defining a virtual host. The listen
directive specifies the IP address(es) and
port(s); the server_name directive lists
the Host header values that this context
matches.
server_name Configures the names that a virtual host
may respond to.
server_name_in_redirect Activates using the first value of the
server_name directive in any redirect
issued by NGINX within this context.
server_tokens Disables sending the NGINX version string
in error messages and the Server response
header (default value is on).
Free download pdf