Mastering Nginx

(Ron) #1
Chapter 4

[ 77 ]

This is the most basic proxy configuration possible. NGINX will terminate all client


connections, and then proxy all requests to the local host on TCP port 8080. We
assume here that Apache has been configured to listen on localhost:8080.


A configuration such as this is typically extended so that NGINX will serve any static


files directly, and then proxy the remaining requests to Apache:


server {

location / {

try_files $uri @apache;

}

location @apache {

proxy_pass http://127.0.0.1:8080;

}

}

The try_files directive (included in the http core module) does just what its name


implies—it tries files, in order, until it finds a match. So, in the preceding example,
NGINX will deliver any files it finds in its root that match the URI given by the


client. If it doesn't find any files, it will proxy the request to Apache for further
processing. We use a named location here to proxy the request after an unsuccessful


try to locate the file locally.


Multiple upstream servers


It is also possible to configure NGINX to pass the request to more than one upstream


server. This is done by declaring an upstream context, defining multiple servers, and
referencing the upstream in a proxy_pass directive:


upstream app {

server 127.0.0.1:9000;

server 127.0.0.1:9001;

server 127.0.0.1:9002;

}
Free download pdf