Skip to content

Commit 0c191a1

Browse files
authored
Merge pull request #18 from flownative/access-logs
Introduce access log support
2 parents dafb733 + 7adda3c commit 0c191a1

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,35 @@ redirected to STDERR. That way, you can follow logs by watching
3838
container logs with `docker logs` or using a similar mechanism in
3939
Kubernetes or your actual platform.
4040

41+
Additionally, logs are also stored in /opt/flownative/log/nginx-error.log
42+
and /opt/flownative/log/nginx-access.log. If the log format is "json",
43+
the access log file is /opt/flownative/log/nginx-access.json.log
44+
4145
The log level for error can be defined via the `NGINX_LOG_LEVEL`
4246
environment variable. See the
4347
[Nginx documentation](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)
4448
for possible values. The default value is `warn`.
4549

50+
The access log is disabled by default, it can be enabled by setting
51+
`NGINX_ACCESS_LOG_ENABLE` to "true".
52+
53+
The access log's default format is similar to the standard Nginx
54+
"combined" format with a few additions, so that the IP address of
55+
the original request is shown since this Nginx is usually operated
56+
behind a reverse proxy.
57+
58+
Instead of the default format, a JSON format can be used by setting
59+
`NGINX_ACCESS_LOG_FORMAT` to "json".
60+
4661
### Environment variables
4762

4863
| Variable Name | Type | Default | Description |
4964
|:-----------------------------------------|:--------|:--------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
5065
| NGINX_BASE_PATH | string | /opt/flownative/nginx | Base path for Nginx |
51-
| NGINX_LOG_LEVEL | string | warn | Nginx log level (see [documentation](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)) |
66+
| NGINX_ERROR_LOG_LEVEL | string | warn | Nginx log level (see [documentation](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)) |
67+
| NGINX_ACCESS_LOG_ENABLE | boolean | no | Nginx log level (see [documentation](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)) |
68+
| NGINX_ACCESS_LOG_FORMAT | string | default | Format of the access log; possible values are "default" and "json" |
69+
| NGINX_ACCESS_LOG_MODE | string | dynamic | Defines which requests should be logged: "dynamic" only logs dynamic requests to PHP, "all" also includes requests to static files |
5270
| NGINX_CACHE_ENABLE | boolean | no | If the FastCGI cache should be enabled; see section about caching |
5371
| NGINX_CACHE_NAME | string | application | Name of the memory zone Nginx should use for caching |
5472
| NGINX_CACHE_DEFAULT_LIFETIME | string | 5s | Default cache lifetime to use when caching is enabled |

root-files/build.sh

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ groupadd --gid 1000 nginx
1010
mkdir -p \
1111
"${NGINX_BASE_PATH}/cache" \
1212
"${NGINX_BASE_PATH}/etc" \
13-
"${NGINX_BASE_PATH}/log" \
1413
"${NGINX_BASE_PATH}/modules" \
1514
"${NGINX_BASE_PATH}/sbin" \
1615
"${NGINX_BASE_PATH}/tmp"
@@ -27,7 +26,6 @@ chmod -R g+rwX "${NGINX_BASE_PATH}"
2726

2827
chown -R nginx:nginx \
2928
"${NGINX_BASE_PATH}/cache" \
30-
"${NGINX_BASE_PATH}/log" \
3129
"${NGINX_BASE_PATH}/tmp"
3230

3331
# Fix ownership of syslog-ng's etc directory because COPY in this Dockerfile

root-files/opt/flownative/lib/nginx-legacy.sh

+30-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# ---------------------------------------------------------------------------------------
1919
# nginx_legacy_env() - Load global environment variables for configuring Nginx
2020
#
21-
# @global NGINX_* The NGINX_ evnironment variables
21+
# @global NGINX_* The NGINX_ environment variables
2222
# @return "export" statements which can be passed to eval()
2323
#
2424
nginx_legacy_env() {
@@ -146,7 +146,30 @@ EOM
146146
EOM
147147
fi
148148

149+
dynamicAccessLogDirective=""
150+
staticAccessLogDirective=""
151+
152+
if is_boolean_yes "${NGINX_ACCESS_LOG_ENABLE}"; then
153+
if [ "${NGINX_ACCESS_LOG_FORMAT}" == "json" ]; then
154+
info "Nginx: Enabling access log using format 'json' ..."
155+
dynamicAccessLogDirective=" access_log ${FLOWNATIVE_LOG_PATH}/nginx-access.json.log main_json buffer=256k flush=5s;"
156+
else
157+
info "Nginx: Enabling access log using format 'default' ..."
158+
dynamicAccessLogDirective=" access_log ${FLOWNATIVE_LOG_PATH}/nginx-access.log main_ext buffer=256k flush=5s;"
159+
fi
160+
else
161+
info "Nginx: Access log is disabled"
162+
fi
163+
164+
if [ "${NGINX_ACCESS_LOG_MODE}" == "all" ]; then
165+
info "Nginx: Enabling access log for all types of requests ..."
166+
staticAccessLogDirective=${dynamicAccessLogDirective}
167+
fi
168+
149169
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
170+
171+
$staticAccessLogDirective
172+
150173
location ~ \\.php\$ {
151174
include fastcgi_params;
152175
@@ -157,7 +180,10 @@ EOM
157180
158181
fastcgi_pass ${BEACH_PHP_FPM_HOST}:${BEACH_PHP_FPM_PORT};
159182
fastcgi_index index.php;
183+
184+
$dynamicAccessLogDirective
160185
EOM
186+
161187
if [ -n "${NGINX_CUSTOM_ERROR_PAGE_TARGET}" ]; then
162188
info "Nginx: Enabling custom error page pointing to ${BEACH_NGINX_CUSTOM_ERROR_PAGE_TARGET} ..."
163189
nginx_config_fastcgi_custom_error_page >>"${NGINX_CONF_PATH}/sites-enabled/site.conf"
@@ -219,7 +245,7 @@ EOM
219245
elif [ -n "${BEACH_PERSISTENT_RESOURCES_FALLBACK_BASE_URI}" ]; then
220246
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
221247
location ~* "^${BEACH_PERSISTENT_RESOURCES_BASE_PATH}(.*)$" {
222-
access_log off;
248+
${staticAccessLogDirective}
223249
expires ${NGINX_STATIC_FILES_LIFETIME};
224250
add_header Via '\$hostname' always;
225251
${addHeaderStrictTransportSecurity}
@@ -237,7 +263,7 @@ EOM
237263
else
238264
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
239265
location ~* ^/_Resources/Persistent/(.*)$ {
240-
access_log off;
266+
${staticAccessLogDirective}
241267
expires ${NGINX_STATIC_FILES_LIFETIME};
242268
add_header Via '\$hostname' always;
243269
${addHeaderStrictTransportSecurity}
@@ -256,7 +282,7 @@ EOM
256282
# for all static resources
257283
location ~ ^/_Resources/Static/ {
258284
add_header X-Static-Resource '\$hostname' always;
259-
access_log off;
285+
${staticAccessLogDirective}
260286
expires ${NGINX_STATIC_FILES_LIFETIME};
261287
}
262288
}

root-files/opt/flownative/lib/nginx.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export NGINX_BASE_PATH="${NGINX_BASE_PATH}"
2424
export NGINX_CONF_PATH="${NGINX_BASE_PATH}/etc"
2525
export NGINX_TMP_PATH="${NGINX_BASE_PATH}/tmp"
2626
export NGINX_LOG_PATH="${NGINX_BASE_PATH}/log"
27-
export NGINX_LOG_LEVEL="${NGINX_LOG_LEVEL:-info}"
27+
export NGINX_ERROR_LOG_LEVEL="${NGINX_ERROR_LOG_LEVEL:-${NGINX_LOG_LEVEL:-warn}}"
28+
export NGINX_ACCESS_LOG_ENABLE="${NGINX_ACCESS_LOG_ENABLE:-false}"
29+
export NGINX_ACCESS_LOG_MODE="${NGINX_ACCESS_LOG_MODE:-dynamic}"
30+
export NGINX_ACCESS_LOG_FORMAT="${NGINX_ACCESS_LOG_FORMAT:-default}"
2831
2932
export NGINX_CACHE_PATH="${NGINX_CACHE_PATH:-${NGINX_BASE_PATH}/cache}"
3033
export NGINX_CACHE_ENABLE="${NGINX_CACHE_ENABLE:-no}"

root-files/opt/flownative/logrotate/etc/logrotate.d/nginx.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/opt/flownative/nginx/log/access.log /opt/flownative/nginx/log/error.log /opt/flownative/nginx/log/error_log.json /opt/flownative/nginx/log/access_log.json {
1+
/opt/flownative/log/nginx-access.log /opt/flownative/log/nginx-access.json.log /opt/flownative/log/nginx-error.log {
22

33
rotate 1
44
daily
55
missingok
66
notifempty
77
minsize 100k
8+
maxsize 50M
89

910
sharedscripts
1011
postrotate

root-files/opt/flownative/nginx/etc/nginx.conf.template

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ daemon off;
22
worker_processes auto;
33

44
pid ${NGINX_TMP_PATH}/nginx.pid;
5-
error_log ${FLOWNATIVE_LOG_PATH}/nginx_error.log ${NGINX_LOG_LEVEL};
5+
error_log ${FLOWNATIVE_LOG_PATH}/nginx-error.log ${NGINX_ERROR_LOG_LEVEL};
66

77
load_module ${NGINX_BASE_PATH}/modules/ngx_http_headers_more_filter_module.so;
88

@@ -27,14 +27,14 @@ http {
2727
tcp_nopush on;
2828
tcp_nodelay on;
2929

30-
log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" '
31-
'$status $body_bytes_sent "$http_referer" '
32-
'"$http_user_agent" "$http_x_forwarded_for" '
33-
'"$host" sn="$server_name" '
34-
'rt=$request_time '
35-
'ua="$upstream_addr" us="$upstream_status" '
36-
'ut="$upstream_response_time" ul="$upstream_response_length" '
37-
'cs=$upstream_cache_status' ;
30+
log_format main_ext '$$remote_addr - $$remote_user [$$time_local] "$$request" '
31+
'$$status $$body_bytes_sent "$$http_referer" '
32+
'"$$http_user_agent" "$$http_x_forwarded_for" '
33+
'"$$host" sn="$$server_name" '
34+
'rt=$$request_time '
35+
'ua="$$upstream_addr" us="$$upstream_status" '
36+
'ut="$$upstream_response_time" ul="$$upstream_response_length" '
37+
'cs=$$upstream_cache_status' ;
3838

3939
log_format main_json escape=json '{'
4040
'"msec": "$$msec", ' # request unixtime in seconds with a milliseconds resolution
@@ -65,9 +65,6 @@ http {
6565
'"server_protocol": "$$server_protocol"' # request protocol, like HTTP/1.1 or HTTP/2.0
6666
'}';
6767

68-
# access_log ${FLOWNATIVE_LOG_PATH}/nginx_access.log main_ext buffer=256k flush=5m;
69-
# access_log ${FLOWNATIVE_LOG_PATH}/nginx_access.log.json main_json buffer=256k flush=5m;
70-
7168
client_body_buffer_size 5M;
7269
client_max_body_size 500M;
7370

root-files/opt/flownative/syslog-ng/etc/conf.d/nginx.conf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
source s_nginx_access_json {
2-
file("`FLOWNATIVE_LOG_PATH`/nginx_access.log.json"
2+
file("`FLOWNATIVE_LOG_PATH`/nginx-access.json.log"
33
program-override("nginx")
44
follow_freq(1)
55
default-priority(info)
@@ -9,7 +9,7 @@ source s_nginx_access_json {
99
};
1010

1111
source s_nginx_access_common {
12-
file("`FLOWNATIVE_LOG_PATH`/nginx_access.log"
12+
file("`FLOWNATIVE_LOG_PATH`/nginx-access.log"
1313
program-override("nginx")
1414
default-priority(info)
1515
follow_freq(1)
@@ -18,7 +18,7 @@ source s_nginx_access_common {
1818
};
1919

2020
source s_nginx_error {
21-
file("`FLOWNATIVE_LOG_PATH`/nginx_error.log"
21+
file("`FLOWNATIVE_LOG_PATH`/nginx-error.log"
2222
program-override("nginx")
2323
default-priority(error)
2424
follow_freq(1)

0 commit comments

Comments
 (0)