summaryrefslogtreecommitdiff
path: root/pod/nginx/beginners_guide.pod
blob: a8ca27f753362776b74ac02f0b4a82fbe76fce5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
=encoding utf-8


=head1 Name


beginners_guide - Beginner’s Guide


=head1



This guide gives a basic introduction to nginx and describes some
simple tasks that can be done with it.
It is supposed that nginx is already installed on the reader’s machine.
If it is not, see the L<install> page.
This guide describes how to start and stop nginx, and reload its
configuration, explains the structure
of the configuration file and describes how to set up nginx
to serve out static content, how to configure nginx as a proxy
server, and how to connect it with a FastCGI application.





nginx has one master process and several worker processes.
The main purpose of the master process is to read and evaluate configuration,
and maintain worker processes.
Worker processes do actual processing of requests.
nginx employs event-based model and OS-dependent mechanisms to efficiently
distribute requests among worker processes.
The number of worker processes is defined in the configuration file and
may be fixed for a given configuration or automatically adjusted to the
number of available CPU cores (see
L<ngx_core_module>).





The way nginx and its modules work is determined in the configuration file.
By default, the configuration file is named F<nginx.conf>
and placed in the directory
F<E<sol>usrE<sol>localE<sol>nginxE<sol>conf>,
F<E<sol>etcE<sol>nginx>, or
F<E<sol>usrE<sol>localE<sol>etcE<sol>nginx>.




=head1 Starting, Stopping, and Reloading Configuration



To start nginx, run the executable file.
Once nginx is started, it can be controlled by invoking the executable
with the C<-s> parameter.
Use the following syntax:

    
    nginx -s <i>signal</i>


Where I<signal> may be one of the following:

=over



=item *

C<stop>E<mdash>fast shutdown


=item *

C<quit>E<mdash>graceful shutdown


=item *

C<reload>E<mdash>reloading the configuration file


=item *

C<reopen>E<mdash>reopening the log files


=back


For example, to stop nginx processes with waiting for the worker processes
to finish serving current requests, the following command can be executed:

    
    nginx -s quit



B<NOTE>
This command should be executed under the same user that
started nginx.





Changes made in the configuration file
will not be applied until the command to reload configuration is
sent to nginx or it is restarted.
To reload configuration, execute:

    
    nginx -s reload







Once the master process receives the signal to reload configuration,
it checks the syntax validity
of the new configuration file and tries to apply the configuration provided
in it.
If this is a success, the master process starts new worker processes
and sends messages to old worker processes, requesting them to
shut down.
Otherwise, the master process rolls back the changes and
continues to work with the old configuration.
Old worker processes, receiving a command to shut down,
stop accepting new connections and continue to service current requests until
all such requests are serviced.
After that, the old worker processes exit.





A signal may also be sent to nginx processes with the help of Unix tools
such as the C<kill> utility.
In this case a signal is sent directly to a process with a given process ID.
The process ID of the nginx master process is written, by default, to the
F<nginx.pid> in the directory
F<E<sol>usrE<sol>localE<sol>nginxE<sol>logs> or
F<E<sol>varE<sol>run>.
For example, if the master process ID is 1628, to send the QUIT signal
resulting in nginx’s graceful shutdown, execute:

    
    kill -s QUIT 1628


For getting the list of all running nginx processes, the C<ps>
utility may be used, for example, in the following way:

    
    ps -ax | grep nginx


For more information on sending signals to nginx, see
L<control>.




=head1 Configuration File’s Structure



nginx consists of modules which are controlled by directives specified
in the configuration file.
Directives are divided into simple directives and block directives.
A simple directive consists of the name and parameters separated by spaces
and ends with a semicolon (C<;>).
A block directive has the same structure as a simple directive, but
instead of the semicolon it ends with a set of additional instructions
surrounded by braces (C<{> and C<}>).
If a block directive can have other directives inside braces,
it is called a context (examples:
L<ngx_core_module>,
L<ngx_http_core_module>,
L<ngx_http_core_module>,
and
L<ngx_http_core_module>).





Directives placed in the configuration file outside
of any contexts are considered to be in the
L<main|ngx_core_module> context.
The C<events> and C<http> directives
reside in the C<main> context, C<server>
in C<http>, and C<location> in
C<server>.





The rest of a line after the C<#> sign is considered a comment.




=head1 Serving Static Content



An important web server task is serving out
files (such as images or static HTML pages).
You will implement an example where, depending on the request,
files will be served from different local directories: F<E<sol>dataE<sol>www>
(which may contain HTML files) and F<E<sol>dataE<sol>images>
(containing images).
This will require editing of the configuration file and setting up of a
L<ngx_http_core_module>
block inside the L<ngx_http_core_module>
block with two L<ngx_http_core_module>
blocks.





First, create the F<E<sol>dataE<sol>www> directory and put an
F<index.html> file with any text content into it and
create the F<E<sol>dataE<sol>images> directory and place some
images in it.





Next, open the configuration file.
The default configuration file already includes several examples of
the C<server> block, mostly commented out.
For now comment out all such blocks and start a new
C<server> block:

    
    http {
        server {
        }
    }


Generally, the configuration file may include several
C<server> blocks
L<distinguished|request_processing> by ports on which
they L<listen|ngx_http_core_module> to
and by
L<server names|server_names>.
Once nginx decides which C<server> processes a request,
it tests the URI specified in the request’s header against the parameters of the
C<location> directives defined inside the
C<server> block.





Add the following C<location> block to the
C<server> block:

    
    location / {
        root /data/www;
    }


This C<location> block specifies the
“F<E<sol>>” prefix compared with the URI from the request.
For matching requests, the URI will be added to the path specified in the
L<ngx_http_core_module>
directive, that is, to F<E<sol>dataE<sol>www>,
to form the path to the requested file on the local file system.
If there are several matching C<location> blocks nginx
selects the one with the longest prefix.
The C<location> block above provides the shortest
prefix, of length one,
and so only if all other C<location>
blocks fail to provide a match, this block will be used.





Next, add the second C<location> block:

    
    location /images/ {
        root /data;
    }


It will be a match for requests starting with C<E<sol>imagesE<sol>>
(C<location E<sol>> also matches such requests,
but has shorter prefix).





The resulting configuration of the C<server> block should
look like this:

    
    server {
        location / {
            root /data/www;
        }
    
        location /images/ {
            root /data;
        }
    }


This is already a working configuration of a server that listens
on the standard port 80 and is accessible on the local machine at
C<http:E<sol>E<sol>localhostE<sol>>.
In response to requests with URIs starting with C<E<sol>imagesE<sol>>,
the server will send files from the F<E<sol>dataE<sol>images> directory.
For example, in response to the
C<http:E<sol>E<sol>localhostE<sol>imagesE<sol>example.png> request nginx will
send the F<E<sol>dataE<sol>imagesE<sol>example.png> file.
If such file does not exist, nginx will send a response
indicating the 404 error.
Requests with URIs not starting with C<E<sol>imagesE<sol>> will be
mapped onto the F<E<sol>dataE<sol>www> directory.
For example, in response to the
C<http:E<sol>E<sol>localhostE<sol>someE<sol>example.html> request nginx will
send the F<E<sol>dataE<sol>wwwE<sol>someE<sol>example.html> file.





To apply the new configuration, start nginx if it is not yet started or
send the C<reload> signal to the nginx’s master process,
by executing:

    
    nginx -s reload








B<NOTE>

In case something does not work as expected, you may try to find out
the reason in F<access.log> and
F<error.log> files in the directory
F<E<sol>usrE<sol>localE<sol>nginxE<sol>logs> or
F<E<sol>varE<sol>logE<sol>nginx>.





=head1 Setting Up a Simple Proxy Server



One of the frequent uses of nginx is setting it up as a proxy server, which
means a server that receives requests, passes them to the proxied servers,
retrieves responses from them, and sends them to the clients.





We will configure a basic proxy server, which serves requests of
images with files from the local directory and sends all other requests to a
proxied server.
In this example, both servers will be defined on a single nginx instance.





First, define the proxied server by adding one more C<server>
block to the nginx’s configuration file with the following contents:

    
    server {
        listen 8080;
        root /data/up1;
    
        location / {
        }
    }


This will be a simple server that listens on the port 8080
(previously, the C<listen> directive has not been specified
since the standard port 80 was used) and maps
all requests to the F<E<sol>dataE<sol>up1> directory on the local
file system.
Create this directory and put the F<index.html> file into it.
Note that the C<root> directive is placed in the
C<server> context.
Such C<root> directive is used when the
C<location> block selected for serving a request does not
include its own C<root> directive.





Next, use the server configuration from the previous section
and modify it to make it a proxy server configuration.
In the first C<location> block, put the
L<ngx_http_proxy_module>
directive with the protocol, name and port of the proxied server specified
in the parameter (in our case, it is C<http:E<sol>E<sol>localhost:8080>):

    
    server {
        location / {
            proxy_pass http://localhost:8080;
        }
    
        location /images/ {
            root /data;
        }
    }







We will modify the second C<location>
block, which currently maps requests with the C<E<sol>imagesE<sol>>
prefix to the files under the F<E<sol>dataE<sol>images> directory,
to make it match the requests of images with typical file extensions.
The modified C<location> block looks like this:

    
    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }


The parameter is a regular expression matching all URIs ending
with F<.gif>, F<.jpg>, or F<.png>.
A regular expression should be preceded with C<~>.
The corresponding requests will be mapped to the F<E<sol>dataE<sol>images>
directory.





When nginx selects a C<location> block to serve a request
it first checks L<ngx_http_core_module>
directives that specify prefixes, remembering C<location>
with the longest prefix, and then checks regular expressions.
If there is a match with a regular expression, nginx picks this
C<location> or, otherwise, it picks the one remembered earlier.





The resulting configuration of a proxy server will look like this:

    
    server {
        location / {
            proxy_pass http://localhost:8080/;
        }
    
        location ~ \.(gif|jpg|png)$ {
            root /data/images;
        }
    }


This server will filter requests ending with F<.gif>,
F<.jpg>, or F<.png>
and map them to the F<E<sol>dataE<sol>images> directory (by adding URI to the
C<root> directive’s parameter) and pass all other requests
to the proxied server configured above.





To apply new configuration, send the C<reload> signal to
nginx as described in the previous sections.





There are many L<more|ngx_http_proxy_module>
directives that may be used to further configure a proxy connection.




=head1 Setting Up FastCGI Proxying



nginx can be used to route requests to FastCGI servers which run
applications built with various frameworks and programming languages
such as PHP.





The most basic nginx configuration to work with a FastCGI server
includes using the
L<ngx_http_fastcgi_module>
directive instead of the C<proxy_pass> directive,
and L<ngx_http_fastcgi_module>
directives to set parameters passed to a FastCGI server.
Suppose the FastCGI server is accessible on C<localhost:9000>.
Taking the proxy configuration from the previous section as a basis,
replace the C<proxy_pass> directive with the
C<fastcgi_pass> directive and change the parameter to
C<localhost:9000>.
In PHP, the C<SCRIPT_FILENAME> parameter is used for
determining the script name, and the C<QUERY_STRING>
parameter is used to pass request parameters.
The resulting configuration would be:

    
    server {
        location / {
            fastcgi_pass  localhost:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param QUERY_STRING    $query_string;
        }
    
        location ~ \.(gif|jpg|png)$ {
            root /data/images;
        }
    }


This will set up a server that will route all requests except for
requests for static images to the proxied server operating on
C<localhost:9000> through the FastCGI protocol.