Repository navigation
Expand file tree
/
Copy pathvhost-reverse-proxy.conf.j2
More file actions
80 lines (67 loc) · 2.73 KB
/
Copy pathvhost-reverse-proxy.conf.j2
File metadata and controls
80 lines (67 loc) · 2.73 KB
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
{#
Example nginx vhost template for pyinfra-nginx's `add_vhost()`.
This file is NOT deployed by pyinfra-nginx — the library deliberately doesn't manage vhost
content (see README "How vhosts work"). Copy this into your own pyinfra project (eg
`files/vhosts/example.conf.j2`) and adapt it, then deploy it yourself:
from pyinfra_nginx import add_vhost
add_vhost(
"example.com",
src="files/vhosts/example.conf.j2",
server_name="example.com",
upstream="127.0.0.1:8080",
ssl_certificate="/etc/ssl/certs/example.com.crt",
ssl_certificate_key="/etc/ssl/private/example.com.key",
restrict_to_cloudflare=False,
)
Variables used below (all passed as `**context` to `add_vhost`): server_name, upstream,
ssl_certificate, ssl_certificate_key, restrict_to_cloudflare.
restrict_to_cloudflare=True also requires install(cloudflare_ip_allowlist=True) (off by
default) — see examples/vhost-cloudflare-restricted.conf.j2.
#}
server {
listen 80;
listen [::]:80;
server_name {{ server_name }};
# ACME HTTP-01 challenge, if you terminate certificates with certbot/acme.sh.
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name {{ server_name }};
http2 on;
ssl_certificate {{ ssl_certificate }};
ssl_certificate_key {{ ssl_certificate_key }};
ssl_protocols TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
{% if restrict_to_cloudflare %}
# Trust Cloudflare's CF-Connecting-IP header for this vhost (sets $remote_addr to the real
# visitor IP instead of Cloudflare's edge IP), then reject anything that isn't loopback or a
# published Cloudflare IP range. $is_cloudflare_allowed itself requires
# install(cloudflare_ip_allowlist=True) (off by default) — see
# examples/vhost-cloudflare-restricted.conf.j2 for a minimal, dedicated version of this pattern.
include /etc/nginx/conf.d/cloudflare-real-ip.include;
if ($is_cloudflare_allowed != 1) {
return 403;
}
{% endif %}
# Security headers.
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
location / {
proxy_pass http://{{ upstream }};
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}