blob: aa9875fd757dbf00c075535fab19885860e35f78 [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
Taras Khlivnyak4948b0a2022-05-13 16:03:27 +0300274Location with access policy:
275
276.. note:: If location is defined and access_policy for location is defined,
277 it overrides main access_policy for that location.
278
279.. code-block:: yaml
280
281 nginx:
282 server:
283 site:
284 nginx_proxy_site01:
285 enabled: true
286 type: nginx_proxy
287 name: site01
288 access_policy:
289 allow:
290 - 192.168.1.1/24
291 - 127.0.0.1
292 deny:
293 - 192.168.1.2
294 - all
295 proxy:
296 host: local.domain.com
297 port: 80
298 protocol: http
299 location:
300 /internal/:
301 host: 172.120.10.200
302 port: 80
303 protocol: http
304 /restricted/:
305 host: 172.10.10.200
306 port: 80
307 protocol: http
308 access_policy:
309 allow:
310 - 10.10.10.0/24
311 - 127.0.0.1
312 deny:
313 - all
314 host:
315 name: gitlab.domain.com
316 port: 80
317
318
319
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000320Use nginx `ngx_http_map_module` that creates variables whose values depend on
321values of other variables.
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100322
323.. code-block:: yaml
324
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000325 nginx:
326 server:
327 enabled: true
328 map:
329 enabled: true
330 items:
331 mymap:
332 enabled: true
333 string: input_string
334 variable: output_map_variable
335 body:
336 default:
337 value: '""'
338 example.com:
339 value: '1'
340 example.org:
341 value: '2'
342
343Use nginx `ngx_http_geo_module module` that creates variables with values
344depending on the client IP address.
345
346.. code-block:: yaml
347
348 nginx:
349 server:
350 enabled: true
351 geo:
352 enabled: true
353 items:
354 my_geo_map:
355 enabled: true
356 variable: output_get_variable
357 body:
358 default:
359 value: '""'
360 cl1
361 name: 10.12.100.1/32
362 value: '1'
363 cl2
364 name: 10.13.0.0/16
365 value: 2'
366
367Use `ngx_http_limit_req_module` module that is used to limit the request
368processing rate per a defined key, in particular, the processing rate of
369requests coming from a single IP address. The limitation is done using
370the `leaky bucket` method.
371The `limit_req_module` might be configured globally or applied to specific
372nginx site.
373
374.. code-block:: yaml
375
376 nginx:
377 server:
378 limit_req_module:
379 limit_req_zone:
380 global_limit_ip_zone:
381 key: global_limit_ip_var
382 size: 10m
383 rate: '1r/s'
384 limit_req_status: 503
385 limit_req:
386 global_limit_zone:
387 burst: 5
388 enabled: true
389
390There is an example to to limit requests to all sites based on IP.
391In the following example all clients are limited except of 10.12.100.1
392with 1 req per second.
393
394#. Create geo instance that will match IP and set `limit_action` var.
395 "0" - is unlimited, 1 - limited
396
397#. Create a `global_geo_limiting_map` that will map `ip_limit_key` to
398 `ip_limit_action`
399
400#. Create global `limit_req_zone` called `global_limit_zone` that limits
401 number of requests to 1r/s
402
403#. Apply `global_limit_zone` globally to all requests with 5 req burst.
404
405.. code-block:: yaml
406
407 nginx:
408 server:
409 enabled: true
410 geo:
411 enabled: true
412 items:
413 global_geo_limiting:
414 enabled: true
415 variable: ip_limit_key
416 body:
417 default:
418 value: '1'
419 unlimited_client1:
420 name: '10.12.100.1/32'
421 value: '0'
422 map:
423 enabled: true
424 items:
425 global_geo_limiting_map:
426 enabled: true
427 string: ip_limit_key
428 variable: ip_limit_action
429 body:
430 limited:
431 name: 1
432 value: '$binary_remote_addr'
433 unlimited:
434 name: 0
435 value: '""'
436 limit_req_module:
437 limit_req_zone:
438 global_limit_zone:
439 key: ip_limit_action
440 size: 10m
441 rate: '1r/s'
442 limit_req_status: 503
443 limit_req:
444 global_limit_zone:
445 burst: 5
446 enabled: true
447
448To apply request limiting to particular site only `limit_req` should be
449applied on site level, for example:
450
451.. code-block:: yaml
452
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100453 nginx:
454 server:
455 site:
Vasyl Saienkoc14eca22018-12-07 09:46:17 +0000456 nginx_proxy_openstack_api_keystone:
457 limit_req_module:
458 limit_req:
459 global_limit_zone:
460 burst: 5
461 enabled: true
462
Michel Nederlofeb5dd232018-03-13 13:57:34 +0100463
Oleksandr Bryndziia26617c2018-12-07 17:36:42 +0000464Use `ngx_http_limit_conn_module` module that is used to set the shared memory
465zone and the maximum allowed number of connections for a given key value.
466The `limit_conn_module` might be configured globally or applied to specific
467nginx site.
468
469.. code-block:: yaml
470
471 nginx:
472 server:
473 limit_conn_module:
474 limit_conn_zone:
475 global_limit_conn_zone:
476 key: 'binary_remote_addr'
477 size: 10m
478 limit_conn_status: 503
479 limit_conn:
480 global_limit_conn_zone:
481 connection: 50
482 enabled: true
483
484
485To apply connection limiting to particular site only `limit_conn` should be
486applied on site level, for example:
487
488.. code-block:: yaml
489
490 nginx:
491 server:
492 site:
493 nginx_proxy_openstack_web:
494 limit_conn_module:
495 limit_conn:
496 global_limit_conn_zone:
497 connections: 25
498 enabled: true
499
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300500Gitlab server with user for basic auth:
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200501
502.. code-block:: yaml
503
504 nginx:
505 server:
506 enabled: true
507 user:
508 username1:
509 enabled: true
510 password: magicunicorn
511 htpasswd: htpasswd-site1
512 username2:
513 enabled: true
514 password: magicunicorn
515
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300516Proxy buffering:
Ales Komarekd77b7972015-11-12 11:02:39 +0100517
518.. code-block:: yaml
519
520 nginx:
521 server:
522 enabled: true
523 bind:
524 address: '0.0.0.0'
525 ports:
526 - 80
527 site:
528 gitlab_proxy:
529 enabled: true
Niklaus Giger23096b22017-06-30 16:41:49 +0200530 type: nginx_proxy
Ales Komarekd77b7972015-11-12 11:02:39 +0100531 proxy:
Ondrej Smola5e177bc2017-12-07 12:55:23 +0100532 request_buffer: false
Ales Komarekd77b7972015-11-12 11:02:39 +0100533 buffer:
534 number: 8
535 size: 16
536 host:
537 name: gitlab.domain.com
538 port: 80
539
Valeriy Sakharov1cd5c5b2021-07-02 14:40:51 +0400540If we need to read large client request headers, we need to add new
541parameter `large_client_header_buffers` with buffers number and size:
542.. code-block:: yaml
543 nginx:
544 server:
545 enabled: true
546 bind:
547 address: '0.0.0.0'
548 ports:
549 - 80
550 site:
551 gitlab_proxy:
552 enabled: true
553 type: nginx_proxy
554 large_client_header_buffers: '4 8k'
555 host:
556 name: gitlab.domain.com
557 port: 80
558
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300559Let's Encrypt:
Michael Kutý3a5abf12015-12-04 21:03:33 +0100560
561.. code-block:: yaml
562
563 nginx:
564 server:
565 enabled: true
566 bind:
567 address: '0.0.0.0'
568 ports:
569 - 443
570 site:
571 gitlab_domain:
572 enabled: true
573 type: gitlab
574 name: domain
575 ssl:
576 enabled: true
577 engine: letsencrypt
578 host:
579 name: gitlab.domain.com
580 port: 443
581
Filip Pytloune0f75512016-11-03 14:34:26 +0100582SSL using already deployed key and cert file.
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300583
584.. note:: The cert file should already contain CA cert and
585 complete chain.
Filip Pytloune0f75512016-11-03 14:34:26 +0100586
587.. code-block:: yaml
588
589 nginx:
590 server:
591 enabled: true
592 site:
593 mysite:
594 ssl:
595 enabled: true
596 key_file: /etc/ssl/private/mykey.key
597 cert_file: /etc/ssl/cert/mycert.crt
598
Denis Egorenkob8cb5432019-07-15 16:47:32 +0400599or
600
601.. code-block:: yaml
602
603 nginx:
604 server:
605 enabled: true
606 site:
607 mysite:
608 ssl:
609 enabled: true
610 engine: custom
611 key_file: /etc/ssl/private/mykey.key
612 cert_file: /etc/ssl/cert/mycert.crt
613
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300614Advanced SSL configuration, more information about SSL option
615may be found at http://nginx.org/en/docs/http/ngx_http_ssl_module.html
616
617.. note:: Prior to nginx 1.11.0, only one type of ecdh curve
618 can be applied in ``ssl_ecdh_curve directive``.
619
620 if mode = ``secure`` or mode = ``normal`` and ``ciphers``
621 or ``protocols`` are set, they should have type ``string``.
622 If mode = ``manual``, their type should be ``dict``
623 as shown below.
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200624
625.. code-block:: yaml
626
627 nginx:
628 server:
629 enabled: true
630 site:
631 mysite:
632 ssl:
633 enabled: true
634 mode: 'manual'
635 key_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}.key
636 cert_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}.crt
637 chain_file: /srv/salt/pki/${_param:cluster_name}/${salt:minion:cert:proxy:common_name}-with-chain.crt
638 protocols:
639 TLS1:
640 name: 'TLSv1'
641 enabled: True
642 TLS1_1:
643 name: 'TLSv1.1'
644 enabled: True
645 TLS1_2:
646 name: 'TLSv1.2'
647 enabled: False
648 ciphers:
649 ECDHE_RSA_AES256_GCM_SHA384:
650 name: 'ECDHE-RSA-AES256-GCM-SHA384'
651 enabled: True
652 ECDHE_ECDSA_AES256_GCM_SHA384:
653 name: 'ECDHE-ECDSA-AES256-GCM-SHA384'
654 enabled: True
655 buffer_size: '16k'
656 crl:
657 file: '/etc/ssl/crl.pem'
658 enabled: False
659 dhparam:
660 enabled: True
661 numbits: 2048
azvyagintsev4f4e7d42019-01-24 13:44:55 +0200662 use_dsaparam: True
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200663 ecdh_curve:
664 secp384r1:
665 name: 'secp384r1'
666 enabled: False
667 secp521r1:
668 name: 'secp521r1'
669 enabled: True
670 password_file:
671 content: 'testcontent22'
672 enabled: True
673 file: '/etc/ssl/password.key'
674 prefer_server_ciphers: 'on'
675 ticket_key:
676 enabled: True
677 numbytes: 48
678 resolver:
679 address: '127.0.0.1'
680 valid_seconds: '500'
681 timeout_seconds: '60'
682 session_tickets: 'on'
683 stapling: 'off'
684 stapling_file: '/path/to/stapling/file'
685 stapling_responder: 'http://ocsp.example.com/'
686 stapling_verify: 'on'
687 verify_client: 'on'
688 client_certificate:
689 file: '/etc/ssl/client_cert.pem'
690 enabled: False
691 verify_depth: 1
692 session_cache: 'shared:SSL:15m'
693 session_timeout: '15m'
Adam Tengler5c64af62018-06-22 12:17:55 +0000694 strict_transport_security:
695 max_age: 16000000
696 include_subdomains: False
697 always: true
698 enabled: true
Vasyl Saienkod3348f62018-02-09 20:08:01 +0200699
Vasyl Saienkof5c57e22018-09-28 08:54:33 +0000700Setting custom proxy headers:
701
702.. code-block:: yaml
703
704 nginx:
705 server:
706 enabled: true
707 site:
708 custom_headers:
709 type: nginx_proxy
710 proxy_set_header:
711 Host:
712 enabled: true
713 value: "$host:8774"
714 X-Real-IP:
715 enabled: true
716 value: '$remote_addr'
717 X-Forwarded-For:
718 enabled: true
719 value: '$proxy_add_x_forwarded_for'
720 X-Forwarded-Proto:
721 enabled: true
722 value: '$scheme'
723 X-Forwarded-Port:
724 enabled: true
725 value: '$server_port'
726
Dzmitry Stremkouski13772462018-10-27 14:56:47 +0200727Define site catalog indexes:
728
729.. code-block:: yaml
730
731 nginx:
732 server:
733 enabled: true
734 site:
735 nginx_catalog:
736 enabled: true
737 type: nginx_static
738 name: server
739 indexes:
740 - index.htm
741 - index.html
742 host:
743 name: 127.0.0.1
744 port: 80
745
746Define site catalog autoindex:
747
748.. code-block:: yaml
749
750 nginx:
751 server:
752 enabled: true
753 site:
754 nginx_catalog:
755 enabled: true
756 type: nginx_static
757 name: server
758 autoindex: True
759 host:
760 name: 127.0.0.1
761 port: 80
762
763Nginx stats server (required by collectd nginx plugin) (DEPRECATED):
Adam Tenglerc3916572016-01-25 17:46:52 +0100764
Aleš Komárek296a8442017-04-11 13:22:35 +0200765.. code-block:: yaml
Adam Tenglerc3916572016-01-25 17:46:52 +0100766
767 nginx:
768 server:
769 enabled: true
770 site:
771 nginx_stats_server:
772 enabled: true
773 type: nginx_stats
774 name: server
775 host:
776 name: 127.0.0.1
777 port: 8888
778
Dzmitry Stremkouski13772462018-10-27 14:56:47 +0200779or:
780
781.. code-block:: yaml
782
783 nginx:
784 server:
785 enabled: true
786 site:
787 nginx_stats_server:
788 enabled: true
789 root: disabled
790 indexes: []
791 stats: True
792 type: nginx_static
793 name: stat_server
794 host:
795 name: 127.0.0.1
796 address: 127.0.0.1
797 port: 8888
798
OlgaGusarenkoe2f808f2018-07-30 23:42:18 +0300799Nginx configured to wait for another service/s before
800starting (currently only with systemd):
Adam Tengler86893942018-06-20 14:06:24 +0000801
802.. code-block:: yaml
803
804 nginx:
805 server:
806 wait_for_service:
807 - foo-bar.mount
808 enabled: true
809 site:
810 ...
811
Aleš Komárek296a8442017-04-11 13:22:35 +0200812More Information
813================
Filip Pytloun6b5bb652015-10-06 16:28:32 +0200814
815* http://wiki.nginx.org/Main
816* https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
817* http://nginx.com/resources/admin-guide/reverse-proxy/
818* https://mozilla.github.io/server-side-tls/ssl-config-generator/