Skip to content

Commit 1cb6df3

Browse files
committed
update readme, gzip, includes, security CSP and fix typos:
add another projects into readme set default gzip level to 1 change includes to relative path disable CSP in security fix typos and missed semicolons time spent: 3.25h
1 parent 76a0d29 commit 1cb6df3

9 files changed

+53
-21
lines changed

README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@ At the same time there are a lot good documentation and best practices:
1515
[nginx docs](https://nginx.org/en/docs/),
1616
[digitalocean config generator](https://www.digitalocean.com/community/tools/nginx),
1717
[mozilla ssl best practices](https://ssl-config.mozilla.org/#server=nginx&version=1.17.7&config=intermediate&openssl=1.1.1d&guideline=5.4),
18-
etc... And here I'm trying to put together all good patterns and knowledges so anyone will be able to copy this configs and get good nginx setup out of the box :)
19-
20-
There is also interesting [openbridge nginx](https://github.com/openbridge/nginx) docker image,
21-
but I haven't checked it properly yet, their configs require addition nginx modules and setup
18+
etc...
19+
20+
And there are also some more interesting projects and examples:
21+
- [nginx-admins-handbook](https://github.com/trimstray/nginx-admins-handbook)
22+
Huge total guide, must read for any nginx admin.
23+
- [html5-boilerplate nginx configs](https://github.com/h5bp/server-configs-nginx)
24+
Most popular collection of configuration snippets.
25+
- [nginx-boilerplate](https://github.com/nginx-boilerplate/nginx-boilerplate)
26+
Another one common boilerplate.
27+
- [elasticweb/nginx-configs](https://github.com/elasticweb/nginx-configs)
28+
Collection of Nginx configs for most popular CMS/CMF/Frameworks based on PHP.
29+
- [openbridge/nginx](https://github.com/openbridge/nginx)
30+
Docker image, but I haven't checked it properly yet, their configs require addition nginx modules and setup
2231
and it can't be just copied to usual nginx. However, you can use it with docker.
2332
Also I don't agree with nginx microcache for every site, see known traps.
2433

34+
So here I'm trying to put together all (my) good patterns and knowledges, and organize it as simple as possible in compare with complex examples above. So anyone will be able to copy this configs and get good nginx setup out of the box :)
35+
2536
Time track:
26-
- [Filipp Frizzy](https://github.com/Friz-zy/) 35.84h
37+
- [Filipp Frizzy](https://github.com/Friz-zy/) 39.09h
2738

2839
### Support
2940

conf.d/basic.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sendfile on; # default is off
55
tcp_nopush on; # default is off
66
tcp_nodelay on;
77

8-
include /etc/nginx/mime.types;
8+
include mime.types;
99
default_type application/octet-stream; # default is text/plain
1010

1111
charset utf-8; # default is off

conf.d/gzip.conf

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# https://nginx.org/en/docs/http/ngx_http_gzip_module.html
22
# https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
33

4+
# default ubuntu setup is 6 level
5+
# but best speed\cpu\compression ratio is levels from 1 to 5.
6+
# for best compression you should put prepared gz files near main one,
7+
# see gzip_static module.
8+
gzip_comp_level 1;
9+
410
gzip on;
511
gzip_min_length 512;
612
gzip_vary on;
713
gzip_proxied any;
8-
gzip_comp_level 6;
914
gzip_buffers 16 8k;
1015
gzip_http_version 1.0;
1116
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

conf.d/security.conf

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ add_header X-Content-Type-Options "nosniff" always;
1212

1313
add_header Referrer-Policy "no-referrer-when-downgrade" always;
1414

15-
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
15+
# CSP reduce the risk and impact of XSS attacks in modern browsers.
16+
# Whitelisting known-good resource origins,
17+
# refusing to execute potentially dangerous inline scripts,
18+
# and banning the use of eval are all effective mechanisms
19+
# for mitigating cross-site scripting attacks.
20+
# WARNING
21+
# Enabling this defaults will break all subdomains, CDNs and javascript eval
22+
# https://content-security-policy.com/
23+
# add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
1624

1725
# disable if some of your pages should work through http also
1826
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

nginx.conf

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ worker_processes auto;
66

77
error_log /var/log/nginx/error.log warn;
88

9-
include /etc/nginx/modules-enabled/*.conf;
9+
include modules-enabled/*.conf;
1010

1111
# ulimit -n 100000
1212
worker_rlimit_nofile 100000;
@@ -24,7 +24,7 @@ http {
2424
# basic.conf cache.conf gzip.conf log_format.conf
2525
# real_ip.conf request_id.conf ssl.conf
2626

27-
include /etc/nginx/conf.d/*.conf;
28-
include /etc/nginx/sites-enabled/*.conf;
27+
include conf.d/*.conf;
28+
include sites-enabled/*.conf;
2929

3030
}

snippets/corps.conf.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ if ($http_origin ~* "^https?://(?:.+\.)?{{ item.domain }}(?::\d{1,5})?$") {
55
add_header "Access-Control-Allow-Origin" "$corps_origin";
66

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

snippets/headers.conf

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ add_header X-Content-Type-Options "nosniff" always;
1717

1818
add_header Referrer-Policy "no-referrer-when-downgrade" always;
1919

20-
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
20+
# CSP reduce the risk and impact of XSS attacks in modern browsers.
21+
# Whitelisting known-good resource origins,
22+
# refusing to execute potentially dangerous inline scripts,
23+
# and banning the use of eval are all effective mechanisms
24+
# for mitigating cross-site scripting attacks.
25+
# WARNING
26+
# Enabling this defaults will break all subdomains, CDNs and javascript eval
27+
# https://content-security-policy.com/
28+
# add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
2129

2230
# disable if some of your pages should work through http also
2331
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

snippets/site.conf.j2

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ server {
3939
# ssl_certificate_key /etc/ssl/private/{{ item.domain }}.key;
4040

4141
# corps hack
42-
# include /etc/nginx/snippets/corps.conf
42+
# include snippets/corps.conf;
4343

4444
# referer protection
45-
# include /etc/nginx/snippets/referer.conf
45+
# include snippets/referer.conf;
4646

4747
# location ~* \.(js|css|png|jpg|jpeg|gif|ico|swf|eot|ttf|otf|woff|woff2)$
48-
include /etc/nginx/snippets/static_location.conf
48+
include snippets/static_location.conf;
4949

5050
location /backend {
51-
proxy_pass http://127.0.0.1:8080
52-
include /etc/nginx/snippets/proxy.conf
51+
proxy_pass http://127.0.0.1:8080;
52+
include snippets/proxy.conf;
5353
}
5454

5555
location / {
@@ -58,10 +58,10 @@ server {
5858

5959
location ~ \.php$ {
6060
fastcgi_pass 127.0.0.1:9000;
61-
include /etc/nginx/snippets/fastcgi.conf
61+
include snippets/fastcgi.conf;
6262
}
6363

6464
# location ~ (/\.|^/protected)
65-
include /etc/nginx/snippets/protected_locations.conf
65+
include snippets/protected_locations.conf;
6666

6767
}

snippets/static_location.conf

+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 /etc/nginx/snippets/headers.conf
10+
include snippets/headers.conf;
1111
}

0 commit comments

Comments
 (0)