Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit a87a895

Browse files
committed
Add main nginx configuration
Credits to https://github.com/h5bp/server-configs-nginx
1 parent c2e18e0 commit a87a895

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ insert_final_newline = true
1313
indent_style = space
1414
indent_size = 4
1515

16-
[*.{js,json,yaml,yml}]
16+
[*.{conf,js,json,yaml,yml}]
1717
indent_style = space
1818
indent_size = 2
1919

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ services:
44

55
## Nginx Container ###########
66
nginx:
7-
build: ./nginx
7+
build:
8+
context: .
9+
dockerfile: ./nginx/Dockerfile
810
ports:
911
- "8080:80"
1012
depends_on:

nginx/Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ FROM nginx:1.13-alpine
33
RUN apk upgrade --update && \
44
adduser -D -H -u 1000 -s /bin/sh www-data
55

6+
# Load main nginx configuration.
7+
COPY ./nginx/nginx.conf \
8+
/etc/nginx/
9+
10+
# Set default working directory.
611
WORKDIR /var/www
712

13+
# Expose the ports inside the container itself.
814
EXPOSE 80 443
915

10-
CMD ["nginx", "-g", "daemon off;"]
16+
# Start nginx.
17+
CMD ["nginx"]

nginx/nginx.conf

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Run as a unique, less privileged user for security reasons.
2+
user www-data;
3+
4+
# Sets the worker threads to the number of CPU cores available in the system for best performance.
5+
# Should be > the number of CPU cores.
6+
# Maximum number of connections = worker_processes * worker_connections.
7+
worker_processes auto;
8+
9+
# Maximum number of open files per worker process.
10+
# Should be > worker_connections.
11+
worker_rlimit_nofile 4096;
12+
13+
events {
14+
# The maximum number of connections that each worker process can handle simultaneously.
15+
# The appropriate setting depends on the size of the server and the nature of the traffic.
16+
# Should be < worker_rlimit_nofile.
17+
worker_connections 1024;
18+
}
19+
20+
# The file storing the process ID of the main process.
21+
pid /run/nginx.pid;
22+
23+
# Determines whether nginx should become a daemon.
24+
daemon off;
25+
26+
http {
27+
#------------------------------
28+
# Basic
29+
#------------------------------
30+
31+
# Hide nginx version information.
32+
server_tokens off;
33+
34+
# Speed up file transfers by using sendfile() to copy directly
35+
# between descriptors rather than using read()/write().
36+
# For performance reasons, on FreeBSD systems w/ ZFS
37+
# this option should be disabled as ZFS's ARC caches
38+
# frequently used files in RAM by default.
39+
sendfile on;
40+
41+
# Don't send out partial frames; this increases throughput
42+
# since TCP frames are filled up before being sent out.
43+
tcp_nopush on;
44+
45+
# Bypass Nagle Algorithm and send the data to the sockets buffer as soon as it’s available.
46+
tcp_nodelay on;
47+
48+
# How long to allow each connection to stay idle.
49+
# Longer values are better for each individual client, particularly for SSL,
50+
# but means that worker connections are tied up longer.
51+
keepalive_timeout 25s;
52+
53+
# Sets the maximum size of the types hash tables.
54+
types_hash_max_size 2048;
55+
56+
# Specify MIME types for files.
57+
include /etc/nginx/mime.types;
58+
59+
# Defines the default MIME type of a response.
60+
default_type application/octet-stream;
61+
62+
#------------------------------
63+
# Logging
64+
#------------------------------
65+
66+
# Rich Elasticsearch/Kibana compatible JSON log format.
67+
log_format main escape=json
68+
'{'
69+
'"time": "$time_iso8601",'
70+
'"message": "$request",'
71+
'"request":{'
72+
'"headers":{'
73+
'"accept": "$http_accept",'
74+
'"content-type": "$content_type",'
75+
'"referer": "$http_referer",'
76+
'"user-agent": "$http_user_agent",'
77+
'"x-forwarded-for": "$http_x_forwarded_for"'
78+
'},'
79+
'"host": "$host",'
80+
'"url": "$request_uri",'
81+
'"method": "$request_method",'
82+
'"remote_address": "$remote_addr",'
83+
'"remote_user": "$remote_user"'
84+
'},'
85+
'"response":{'
86+
'"status": $status,'
87+
'"content_length": $body_bytes_sent,'
88+
'"response_time": $request_time'
89+
'}'
90+
'}';
91+
92+
# Log access to this file.
93+
# This is only used when not overwritten on a server{} level.
94+
access_log /dev/stdout main;
95+
96+
# Log errors to this file.
97+
# This is only used when not overwritten on a server{} level.
98+
error_log /dev/stderr;
99+
100+
#------------------------------
101+
# Gzip
102+
#------------------------------
103+
104+
# Enable gzip compression.
105+
gzip on;
106+
107+
# Compression level (1-9).
108+
# 5 is a perfect compromise between size and CPU usage, offering about
109+
# 75% reduction for most ASCII files (almost identical to level 9).
110+
gzip_comp_level 5;
111+
112+
# Don't compress anything that's already small and unlikely to shrink much
113+
# if at all (the default is 20 bytes, which is bad as that usually leads to
114+
# larger files after gzipping).
115+
gzip_min_length 256;
116+
117+
# Compress data even for clients that are connecting via proxies,
118+
# identified by the "Via" header (required for CloudFront).
119+
gzip_proxied any;
120+
121+
# Tell proxies to cache both the gzipped and regular version of a resource
122+
# whenever the client's Accept-Encoding capabilities header varies;
123+
# Avoids the issue where a non-gzip capable client (which is extremely rare
124+
# today) would display gibberish if their proxy gave them the gzipped version.
125+
gzip_vary on;
126+
127+
# The minimum HTTP version of a request required to compress a response.
128+
gzip_http_version 1.1;
129+
130+
# Compress all output labeled with one of the following MIME-types.
131+
gzip_types
132+
application/atom+xml
133+
application/javascript
134+
application/json
135+
application/ld+json
136+
application/manifest+json
137+
application/rss+xml
138+
application/vnd.geo+json
139+
application/vnd.ms-fontobject
140+
application/x-javascript
141+
application/x-font-ttf
142+
application/x-web-app-manifest+json
143+
application/xhtml+xml
144+
application/xml
145+
font/opentype
146+
image/bmp
147+
image/svg+xml
148+
image/x-icon
149+
text/cache-manifest
150+
text/css
151+
text/plain
152+
text/vcard
153+
text/vnd.rim.location.xloc
154+
text/vtt
155+
text/x-component
156+
text/x-cross-domain-policy;
157+
#text/html is always compressed by gzip module.
158+
159+
# Load the individual server configurations.
160+
include /etc/nginx/conf.d/*.conf;
161+
}

0 commit comments

Comments
 (0)