Skip to content

Commit 235a1e3

Browse files
committed
up and rename snippets
time spent: 0.67h
1 parent 6285b66 commit 235a1e3

12 files changed

+95
-85
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Official nginx docker image and docs.
3636
So here I'm trying to put together all (my) good patterns and knowledge, and organize it as simply as possible in comparison with complex examples above. So anyone will be able to copy this configs and get a good nginx setup out of the box :)
3737

3838
Time track:
39-
- [Filipp Frizzy](https://github.com/Friz-zy/) 45.56h
39+
- [Filipp Frizzy](https://github.com/Friz-zy/) 46.23h
4040

4141
### Support
4242

@@ -73,25 +73,25 @@ SSL best practice from [mozilla](https://ssl-config.mozilla.org/#server=nginx&ve
7373
#### Snippets
7474
Templates and includes. You can also use [config generator](https://www.digitalocean.com/community/tools/nginx) from digitalocean :)
7575

76-
* `corps.conf.j2`
76+
* `corps.include.template`
7777
Template of corps politic for multiple subdomains setup
7878
* `default.conf`
7979
Example of default config with nginx_status, let's encrypt check and redirect to https
80-
* `fastcgi.conf`
80+
* `fastcgi.include`
8181
Include for php locations: fastcgi parameters, timeouts and cache example
82-
* `headers.conf`
82+
* `headers.include`
8383
Include with all headers, see known traps
84-
* `protected_locations.conf`
84+
* `protected_locations.include`
8585
Include with protected locations with 'deny all'
86-
* `proxy.conf`
86+
* `proxy.include`
8787
Include for proxy locations: proxy headers, parameters, timeouts and cache example
88-
* `referer.conf.j2`
88+
* `referer.include.template`
8989
Template of referer protection for cases when you concurents use your fail2ban protection against you, see known traps
90-
* `resolver.conf`
90+
* `resolver.conf.template`
9191
Include for dynamic dns resolving, see known traps
92-
* `site.conf.j2`
92+
* `site.conf.template`
9393
Template of common site configuration
94-
* `static_location.conf`
94+
* `static_location.include`
9595
Include with location for static files
9696

9797
# Dockerfile
@@ -220,7 +220,7 @@ After all produce only `Name2` header in response.
220220
So use add_header.conf include or copy all headers manually
221221
into sections under HTTP one.
222222
```
223-
include /etc/nginx/snippets/headers.conf
223+
include /etc/nginx/snippets/headers.include
224224
```
225225

226226
#### DNS resolving and cache in Docker, Kubernetes and other dynamic environments
@@ -267,7 +267,7 @@ can be used for attack to you. For example, you competitors can add to their sit
267267
```
268268

269269
Then valid user after visit to the their site will be automatically blocked on your site 😆
270-
You can fight with this practice using `http_referer`, see `snippets/referer.conf.j2` template ;)
270+
You can fight with this practice using `http_referer`, see `snippets/referer.include.template` template ;)
271271
Warning: I have not tested this code yet
272272

273273
#### Default templating engine in official docker image can't proceed variables with default values like `${var:-$DEFAULT}`

conf.d/0-basic.conf

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ reset_timedout_connection on; # default is on
1515

1616
client_max_body_size 15m; # default is 1m
1717

18+
# Fail fast, your pages should load in a seconds, not minutes!
19+
# for heavy pages you can configure this defaults per location or domain
1820
client_body_timeout 15; # default is 60s
1921
client_header_timeout 15; # default is 60s
2022
# keepalive_timeout 15; # default is 75s
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
set $corps_origin https://{{ item.site }};
2-
if ($http_origin ~* "^https?://(?:.+\.)?{{ item.domain }}(?::\d{1,5})?$") {
1+
set $corps_origin https://${SERVER_NAME};
2+
if ($http_origin ~* "^https?://(?:.+\.)?${SERVER_DOMAIN}(?::\d{1,5})?$") {
33
set $corps_origin "$http_origin";
44
}
55
add_header "Access-Control-Allow-Origin" "$corps_origin";
66

77
# add_header will override all previous directives from parent sections
8-
include snippets/headers.conf;
8+
include snippets/headers.include;

snippets/fastcgi.conf renamed to snippets/fastcgi.include

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ fastcgi_param REQUEST_SCHEME $proxy_scheme;
1111
fastcgi_keep_conn on;
1212
# fastcgi_socket_keepalive on; # nginx >= 1.15.6
1313

14+
# Fail fast, your pages should load in a seconds, not minutes!
15+
# for heavy pages you can configure this defaults per location or domain
1416
fastcgi_read_timeout 15s; # default is 60s
1517
fastcgi_send_timeout 15s; # default is 60s
1618
fastcgi_connect_timeout 15s; # default is 60s
File renamed without changes.

snippets/proxy.conf renamed to snippets/proxy.include

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ proxy_http_version 1.1;
1212

1313
# proxy_socket_keepalive on; # nginx >= 1.15.6
1414

15+
# Fail fast, your pages should load in a seconds, not minutes!
16+
# for heavy pages you can configure this defaults per location or domain
1517
proxy_read_timeout 15s; # default is 60s
1618
proxy_send_timeout 15s; # default is 60s
1719
proxy_connect_timeout 15s; # default is 60s

snippets/referer.conf.j2 renamed to snippets/referer.include.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# http://nginx.org/en/docs/http/ngx_http_referer_module.html
22

3-
valid_referers none blocked server_names *.{{ item.domain }};
3+
valid_referers none blocked server_names *.${SERVER_DOMAIN};
44

55
if ($invalid_referer) {
66
return 307 $http_referer;

snippets/resolver.conf renamed to snippets/resolver.conf.template

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# kube-dns.kube-system.svc.cluster.local - k8s resolver
2525
# 172.16.0.23 - aws default resolver
2626

27-
resolver 127.0.0.11 valid=30s ipv6=off;
27+
# This default valuse will work only with modified envsubst
28+
# see known traps
29+
resolver ${RESOLVER_URL:-127.0.0.11} valid=${RESOLVER_TTL:-30s} ipv6=off;
2830

2931
# A timeout for name resolution
3032
resolver_timeout 30s; # default is 30s

snippets/site.conf.j2

-67
This file was deleted.

snippets/site.conf.template

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
5+
server_name ${SERVER_NAME};
6+
7+
# for non docker setup
8+
# access_log /var/log/nginx/${SERVER_NAME}-access.log main buffer=512k flush=1s;
9+
# error_log /var/log/nginx/${SERVER_NAME}-error.log error;
10+
11+
location /.well-known/acme-challenge {
12+
root /var/www/letsencrypt;
13+
try_files $uri $uri/ =404;
14+
}
15+
16+
location / {
17+
return 301 https://${SERVER_NAME}$request_uri;
18+
}
19+
}
20+
21+
server {
22+
listen 443 ssl http2;
23+
listen [::]:443 ssl http2;
24+
25+
server_name ${SERVER_NAME};
26+
27+
# for non docker setup
28+
# access_log /var/log/nginx/${SERVER_NAME}-access.log main buffer=512k flush=1s;
29+
# error_log /var/log/nginx/${SERVER_NAME}-error.log error;
30+
31+
root /var/www/${SERVER_NAME}/public;
32+
33+
# let's encrypt cert
34+
ssl_certificate /etc/letsencrypt/live/${SERVER_NAME}/fullchain.pem;
35+
ssl_certificate_key /etc/letsencrypt/live/${SERVER_NAME}/privkey.pem;
36+
ssl_trusted_certificate /etc/letsencrypt/live/${SERVER_NAME}/fullchain.pem;
37+
38+
# ssl_certificate /etc/ssl/certs/${SERVER_DOMAIN}.crt;
39+
# ssl_certificate_key /etc/ssl/private/${SERVER_DOMAIN}.key;
40+
41+
# corps hack
42+
# it should be configured per site with templating
43+
# include corps.include;
44+
45+
# referer protection
46+
# it should be configured per site with templating
47+
# include referer.include;
48+
49+
# location ~* \.(js|css|png|jpg|jpeg|gif|ico|swf|eot|ttf|otf|woff|woff2)$
50+
include snippets/static_location.include;
51+
52+
location /backend {
53+
proxy_pass http://127.0.0.1:8080;
54+
include snippets/proxy.include;
55+
}
56+
57+
location / {
58+
try_files $uri $uri/ /index.php?$query_string;
59+
}
60+
61+
location ~ \.php$ {
62+
fastcgi_pass 127.0.0.1:9000;
63+
include snippets/fastcgi.include;
64+
}
65+
66+
# location ~ (/\.|^/protected)
67+
include snippets/protected_locations.include;
68+
69+
}

snippets/static_location.conf renamed to snippets/static_location.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ location ~* \.(js|css|png|jpg|jpeg|gif|ico|swf|eot|ttf|otf|woff|woff2)$ {
77
expires 30d;
88

99
# add_header will override all previous directives from parent sections
10-
include snippets/headers.conf;
10+
include snippets/headers.include;
1111
}

0 commit comments

Comments
 (0)