blob: fa005eff26d29947a0578e239daa29f367d23b58 [file] [log] [blame]
Filip Pytloun6b5bb652015-10-06 16:28:32 +02001
Aleš Komárek296a8442017-04-11 13:22:35 +02002=============
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +03003Usage
Aleš Komárek296a8442017-04-11 13:22:35 +02004=============
Filip Pytloun6b5bb652015-10-06 16:28:32 +02005
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +03006Nginx is an open source reverse proxy server for HTTP, HTTPS,
7SMTP, POP3, and IMAP protocols, as well as a load balancer,
8HTTP cache, and a web server (origin server). The nginx project
9started with a strong focus on high concurrency, high performance
10and low memory usage.
Filip Pytloun6b5bb652015-10-06 16:28:32 +020011
Aleš Komárek296a8442017-04-11 13:22:35 +020012Sample Pillars
Filip Pytloun6b5bb652015-10-06 16:28:32 +020013==============
14
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +030015Gitlab server setup:
Filip Pytloun6b5bb652015-10-06 16:28:32 +020016
17.. code-block:: yaml
18
19 nginx:
20 server:
21 enabled: true
22 bind:
23 address: '0.0.0.0'
24 ports:
25 - 80
26 site:
27 gitlab_domain:
28 enabled: true
29 type: gitlab
30 name: domain
31 ssl:
32 enabled: true
33 key: |
34 -----BEGIN RSA PRIVATE KEY-----
35 ...
36 cert: |
37 xyz
38 chain: |
39 my_chain..
40 host:
41 name: gitlab.domain.com
42 port: 80
43
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +030044Simple static HTTP site:
Filip Pytloun6b5bb652015-10-06 16:28:32 +020045
46.. code-block:: yaml
47
48 nginx:
49 server:
50 site:
51 nginx_static_site01:
52 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +020053 type: nginx_static
Filip Pytloun6b5bb652015-10-06 16:28:32 +020054 name: site01
55 host:
56 name: gitlab.domain.com
57 port: 80
58
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +030059Simple load balancer:
Dmitry Stremkovskiy52145c62017-08-21 23:42:17 +030060
61.. code-block:: yaml
62
63 nginx:
64 server:
65 upstream:
66 horizon-upstream:
67 backend1:
68 address: 10.10.10.113
69 port: 8078
70 opts: weight=3
71 backend2:
72 address: 10.10.10.114
73 site:
74 nginx_proxy_openstack_web:
75 enabled: true
76 type: nginx_proxy
77 name: openstack_web
78 proxy:
79 upstream_proxy_pass: http://horizon-upstream
80 host:
81 name: 192.168.0.1
82 port: 31337
83
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +030084Static site with access policy:
Adam Tenglere7746cb2016-04-27 19:01:25 +020085
86.. code-block:: yaml
87
88 nginx:
89 server:
90 site:
91 nginx_static_site01:
92 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +020093 type: nginx_static
Adam Tenglere7746cb2016-04-27 19:01:25 +020094 name: site01
95 access_policy:
96 allow:
97 - 192.168.1.1/24
98 - 127.0.0.1
99 deny:
100 - 192.168.1.2
101 - all
102 host:
103 name: gitlab.domain.com
104 port: 80
105
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300106Simple TCP/UDP proxy:
Dmitry Stremkovskiy6484afa2017-08-22 17:07:06 +0300107
108.. code-block:: yaml
109
110 nginx:
111 server:
112 stream:
113 rabbitmq:
114 host:
115 port: 5672
116 backend:
117 server1:
118 address: 10.10.10.113
119 port: 5672
120 least_conn: true
121 hash: "$remote_addr consistent"
122 unbound:
123 host:
124 bind: 127.0.0.1
125 port: 53
126 protocol: udp
127 backend:
128 server1:
129 address: 10.10.10.113
130 port: 5353
131
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300132Simple HTTP proxy:
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200133
134.. code-block:: yaml
135
136 nginx:
137 server:
138 site:
139 nginx_proxy_site01:
140 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200141 type: nginx_proxy
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200142 name: site01
143 proxy:
144 host: local.domain.com
145 port: 80
146 protocol: http
147 host:
148 name: gitlab.domain.com
149 port: 80
150
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300151Simple HTTP proxy with multiple locations:
152
153.. note:: If proxy part is defined and location is missing ``/``,
154 the proxy part is used. If the ``/`` location is defined,
155 it overrides the proxy part.
Jakub Josef83f7acf2018-04-09 16:05:49 +0200156
157.. code-block:: yaml
158
159 nginx:
160 server:
161 site:
162 nginx_proxy_site01:
163 enabled: true
164 type: nginx_proxy
165 name: site01
166 proxy:
167 host: local.domain.com
168 port: 80
169 protocol: http
170 location:
171 /internal/:
172 host: 172.120.10.200
173 port: 80
174 protocol: http
175 /doc/:
176 host: 172.10.10.200
177 port: 80
178 protocol: http
179 host:
180 name: gitlab.domain.com
181 port: 80
182
183.. code-block:: yaml
184
185 nginx:
186 server:
187 site:
188 nginx_proxy_site01:
189 enabled: true
190 type: nginx_proxy
191 name: site01
192 location:
193 /:
194 host: 172.120.10.200
195 port: 80
196 protocol: http
197 /doc/:
198 host: 172.10.10.200
199 port: 80
200 protocol: http
201 host:
202 name: gitlab.domain.com
203 port: 80
204
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300205Simple Websocket proxy:
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200206
207.. code-block:: yaml
208
209 nginx:
210 server:
211 site:
212 nginx_proxy_site02:
213 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200214 type: nginx_proxy
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200215 name: site02
216 proxy:
217 websocket: true
218 host: local.domain.com
219 port: 80
220 protocol: http
221 host:
222 name: gitlab.domain.com
223 port: 80
224
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300225Content filtering proxy:
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200226
227.. code-block:: yaml
228
229 nginx:
230 server:
231 enabled: true
232 site:
233 nginx_proxy_site03:
234 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200235 type: nginx_proxy
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200236 name: site03
237 proxy:
238 host: local.domain.com
239 port: 80
240 protocol: http
241 filter:
242 search: https://www.domain.com
243 replace: http://10.10.10.10
244 host:
245 name: gitlab.domain.com
246 port: 80
247
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300248Proxy with access policy:
Adam Tenglere7746cb2016-04-27 19:01:25 +0200249
250.. code-block:: yaml
251
252 nginx:
253 server:
254 site:
255 nginx_proxy_site01:
256 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200257 type: nginx_proxy
Adam Tenglere7746cb2016-04-27 19:01:25 +0200258 name: site01
259 access_policy:
260 allow:
261 - 192.168.1.1/24
262 - 127.0.0.1
263 deny:
264 - 192.168.1.2
265 - all
266 proxy:
267 host: local.domain.com
268 port: 80
269 protocol: http
270 host:
271 name: gitlab.domain.com
272 port: 80
273
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000274Use nginx `ngx_http_map_module` that creates variables whose values depend on
275values of other variables.
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100276
277.. code-block:: yaml
278
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000279 nginx:
280 server:
281 enabled: true
282 map:
283 enabled: true
284 items:
285 mymap:
286 enabled: true
287 string: input_string
288 variable: output_map_variable
289 body:
290 default:
291 value: '""'
292 example.com:
293 value: '1'
294 example.org:
295 value: '2'
296
297Use nginx `ngx_http_geo_module module` that creates variables with values
298depending on the client IP address.
299
300.. code-block:: yaml
301
302 nginx:
303 server:
304 enabled: true
305 geo:
306 enabled: true
307 items:
308 my_geo_map:
309 enabled: true
310 variable: output_get_variable
311 body:
312 default:
313 value: '""'
314 cl1
315 name: 10.12.100.1/32
316 value: '1'
317 cl2
318 name: 10.13.0.0/16
319 value: 2'
320
321Use `ngx_http_limit_req_module` module that is used to limit the request
322processing rate per a defined key, in particular, the processing rate of
323requests coming from a single IP address. The limitation is done using
324the `leaky bucket` method.
325The `limit_req_module` might be configured globally or applied to specific
326nginx site.
327
328.. code-block:: yaml
329
330 nginx:
331 server:
332 limit_req_module:
333 limit_req_zone:
334 global_limit_ip_zone:
335 key: global_limit_ip_var
336 size: 10m
337 rate: '1r/s'
338 limit_req_status: 503
339 limit_req:
340 global_limit_zone:
341 burst: 5
342 enabled: true
343
344There is an example to to limit requests to all sites based on IP.
345In the following example all clients are limited except of 10.12.100.1
346with 1 req per second.
347
348#. Create geo instance that will match IP and set `limit_action` var.
349 "0" - is unlimited, 1 - limited
350
351#. Create a `global_geo_limiting_map` that will map `ip_limit_key` to
352 `ip_limit_action`
353
354#. Create global `limit_req_zone` called `global_limit_zone` that limits
355 number of requests to 1r/s
356
357#. Apply `global_limit_zone` globally to all requests with 5 req burst.
358
359.. code-block:: yaml
360
361 nginx:
362 server:
363 enabled: true
364 geo:
365 enabled: true
366 items:
367 global_geo_limiting:
368 enabled: true
369 variable: ip_limit_key
370 body:
371 default:
372 value: '1'
373 unlimited_client1:
374 name: '10.12.100.1/32'
375 value: '0'
376 map:
377 enabled: true
378 items:
379 global_geo_limiting_map:
380 enabled: true
381 string: ip_limit_key
382 variable: ip_limit_action
383 body:
384 limited:
385 name: 1
386 value: '$binary_remote_addr'
387 unlimited:
388 name: 0
389 value: '""'
390 limit_req_module:
391 limit_req_zone:
392 global_limit_zone:
393 key: ip_limit_action
394 size: 10m
395 rate: '1r/s'
396 limit_req_status: 503
397 limit_req:
398 global_limit_zone:
399 burst: 5
400 enabled: true
401
402To apply request limiting to particular site only `limit_req` should be
403applied on site level, for example:
404
405.. code-block:: yaml
406
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100407 nginx:
408 server:
409 site:
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000410 nginx_proxy_openstack_api_keystone:
411 limit_req_module:
412 limit_req:
413 global_limit_zone:
414 burst: 5
415 enabled: true
416
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100417
Oleksandr Bryndziia26617c2018-12-07 17:36:42 +0000418Use `ngx_http_limit_conn_module` module that is used to set the shared memory
419zone and the maximum allowed number of connections for a given key value.
420The `limit_conn_module` might be configured globally or applied to specific
421nginx site.
422
423.. code-block:: yaml
424
425 nginx:
426 server:
427 limit_conn_module:
428 limit_conn_zone:
429 global_limit_conn_zone:
430 key: 'binary_remote_addr'
431 size: 10m
432 limit_conn_status: 503
433 limit_conn:
434 global_limit_conn_zone:
435 connection: 50
436 enabled: true
437
438
439To apply connection limiting to particular site only `limit_conn` should be
440applied on site level, for example:
441
442.. code-block:: yaml
443
444 nginx:
445 server:
446 site:
447 nginx_proxy_openstack_web:
448 limit_conn_module:
449 limit_conn:
450 global_limit_conn_zone:
451 connections: 25
452 enabled: true
453
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300454Gitlab server with user for basic auth:
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200455
456.. code-block:: yaml
457
458 nginx:
459 server:
460 enabled: true
461 user:
462 username1:
463 enabled: true
464 password: magicunicorn
465 htpasswd: htpasswd-site1
466 username2:
467 enabled: true
468 password: magicunicorn
469
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300470Proxy buffering:
Ales Komarekd77b7972015-11-12 11:02:39 +0100471
472.. code-block:: yaml
473
474 nginx:
475 server:
476 enabled: true
477 bind:
478 address: '0.0.0.0'
479 ports:
480 - 80
481 site:
482 gitlab_proxy:
483 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200484 type: nginx_proxy
Ales Komarekd77b7972015-11-12 11:02:39 +0100485 proxy:
Ondrej Smola5e177bc2017-12-07 12:55:23 +0100486 request_buffer: false
Ales Komarekd77b7972015-11-12 11:02:39 +0100487 buffer:
488 number: 8
489 size: 16
490 host:
491 name: gitlab.domain.com
492 port: 80
493
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300494Let's Encrypt:
Michael Kutý3a5abf12015-12-04 21:03:33 +0100495
496.. code-block:: yaml
497
498 nginx:
499 server:
500 enabled: true
501 bind:
502 address: '0.0.0.0'
503 ports:
504 - 443
505 site:
506 gitlab_domain:
507 enabled: true
508 type: gitlab
509 name: domain
510 ssl:
511 enabled: true
512 engine: letsencrypt
513 host:
514 name: gitlab.domain.com
515 port: 443
516
Filip Pytloune0f75512016-11-03 14:34:26 +0100517SSL using already deployed key and cert file.
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300518
519.. note:: The cert file should already contain CA cert and
520 complete chain.
Filip Pytloune0f75512016-11-03 14:34:26 +0100521
522.. code-block:: yaml
523
524 nginx:
525 server:
526 enabled: true
527 site:
528 mysite:
529 ssl:
530 enabled: true
531 key_file: /etc/ssl/private/mykey.key
532 cert_file: /etc/ssl/cert/mycert.crt
533
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300534Advanced SSL configuration, more information about SSL option
535may be found at http://nginx.org/en/docs/http/ngx_http_ssl_module.html
536
537.. note:: Prior to nginx 1.11.0, only one type of ecdh curve
538 can be applied in ``ssl_ecdh_curve directive``.
539
540 if mode = ``secure`` or mode = ``normal`` and ``ciphers``
541 or ``protocols`` are set, they should have type ``string``.
542 If mode = ``manual``, their type should be ``dict``
543 as shown below.
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200544
545.. code-block:: yaml
546
547 nginx:
548 server:
549 enabled: true
550 site:
551 mysite:
552 ssl:
553 enabled: true
554 mode: 'manual'
555 key_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}.key
556 cert_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}.crt
557 chain_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}-with-chain.crt
558 protocols:
559 TLS1:
560 name: 'TLSv1'
561 enabled: True
562 TLS1_1:
563 name: 'TLSv1.1'
564 enabled: True
565 TLS1_2:
566 name: 'TLSv1.2'
567 enabled: False
568 ciphers:
569 ECDHE_RSA_AES256_GCM_SHA384:
570 name: 'ECDHE-RSA-AES256-GCM-SHA384'
571 enabled: True
572 ECDHE_ECDSA_AES256_GCM_SHA384:
573 name: 'ECDHE-ECDSA-AES256-GCM-SHA384'
574 enabled: True
575 buffer_size: '16k'
576 crl:
577 file: '/etc/ssl/crl.pem'
578 enabled: False
579 dhparam:
580 enabled: True
581 numbits: 2048
azvyagintsev4f4e7d42019-01-24 13:44:55 +0200582 use_dsaparam: True
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200583 ecdh_curve:
584 secp384r1:
585 name: 'secp384r1'
586 enabled: False
587 secp521r1:
588 name: 'secp521r1'
589 enabled: True
590 password_file:
591 content: 'testcontent22'
592 enabled: True
593 file: '/etc/ssl/password.key'
594 prefer_server_ciphers: 'on'
595 ticket_key:
596 enabled: True
597 numbytes: 48
598 resolver:
599 address: '127.0.0.1'
600 valid_seconds: '500'
601 timeout_seconds: '60'
602 session_tickets: 'on'
603 stapling: 'off'
604 stapling_file: '/path/to/stapling/file'
605 stapling_responder: 'http://ocsp.example.com/'
606 stapling_verify: 'on'
607 verify_client: 'on'
608 client_certificate:
609 file: '/etc/ssl/client_cert.pem'
610 enabled: False
611 verify_depth: 1
612 session_cache: 'shared:SSL:15m'
613 session_timeout: '15m'
Adam Tengler5c64af62018-06-22 12:17:55 +0000614 strict_transport_security:
615 max_age: 16000000
616 include_subdomains: False
617 always: true
618 enabled: true
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200619
Vasyl Saienkof5c57e22018-09-28 08:54:33 +0000620Setting custom proxy headers:
621
622.. code-block:: yaml
623
624 nginx:
625 server:
626 enabled: true
627 site:
628 custom_headers:
629 type: nginx_proxy
630 proxy_set_header:
631 Host:
632 enabled: true
633 value: "$host:8774"
634 X-Real-IP:
635 enabled: true
636 value: '$remote_addr'
637 X-Forwarded-For:
638 enabled: true
639 value: '$proxy_add_x_forwarded_for'
640 X-Forwarded-Proto:
641 enabled: true
642 value: '$scheme'
643 X-Forwarded-Port:
644 enabled: true
645 value: '$server_port'
646
Dzmitry Stremkouski13772462018-10-27 14:56:47 +0200647Define site catalog indexes:
648
649.. code-block:: yaml
650
651 nginx:
652 server:
653 enabled: true
654 site:
655 nginx_catalog:
656 enabled: true
657 type: nginx_static
658 name: server
659 indexes:
660 - index.htm
661 - index.html
662 host:
663 name: 127.0.0.1
664 port: 80
665
666Define site catalog autoindex:
667
668.. code-block:: yaml
669
670 nginx:
671 server:
672 enabled: true
673 site:
674 nginx_catalog:
675 enabled: true
676 type: nginx_static
677 name: server
678 autoindex: True
679 host:
680 name: 127.0.0.1
681 port: 80
682
683Nginx stats server (required by collectd nginx plugin) (DEPRECATED):
Adam Tenglerc3916572016-01-25 17:46:52 +0100684
Aleš Komárek296a8442017-04-11 13:22:35 +0200685.. code-block:: yaml
Adam Tenglerc3916572016-01-25 17:46:52 +0100686
687 nginx:
688 server:
689 enabled: true
690 site:
691 nginx_stats_server:
692 enabled: true
693 type: nginx_stats
694 name: server
695 host:
696 name: 127.0.0.1
697 port: 8888
698
Dzmitry Stremkouski13772462018-10-27 14:56:47 +0200699or:
700
701.. code-block:: yaml
702
703 nginx:
704 server:
705 enabled: true
706 site:
707 nginx_stats_server:
708 enabled: true
709 root: disabled
710 indexes: []
711 stats: True
712 type: nginx_static
713 name: stat_server
714 host:
715 name: 127.0.0.1
716 address: 127.0.0.1
717 port: 8888
718
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300719Nginx configured to wait for another service/s before
720starting (currently only with systemd):
Adam Tengler86893942018-06-20 14:06:24 +0000721
722.. code-block:: yaml
723
724 nginx:
725 server:
726 wait_for_service:
727 - foo-bar.mount
728 enabled: true
729 site:
730 ...
731
Aleš Komárek296a8442017-04-11 13:22:35 +0200732More Information
733================
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200734
735* http://wiki.nginx.org/Main
736* https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
737* http://nginx.com/resources/admin-guide/reverse-proxy/
738* https://mozilla.github.io/server-side-tls/ssl-config-generator/