Skip to content

Commit 2774cc7

Browse files
author
Till Brehm
committed
Add access and error log controls for nginx servers.
Add description text for logging options.
1 parent 5ce5edb commit 2774cc7

File tree

8 files changed

+63
-2
lines changed

8 files changed

+63
-2
lines changed

install/dist/lib/fedora.lib.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,17 @@ public function configure_nginx(){
812812
//* add a sshusers group
813813
$command = 'groupadd sshusers';
814814
if(!is_group('sshusers')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
815+
816+
// add anonymized log option to nginxx.conf file
817+
$nginx_conf_file = $conf['nginx']['config_dir'].'/nginx.conf';
818+
if(is_file($nginx_conf_file)) {
819+
$tmp = file_get_contents($nginx_conf_file);
820+
if(!stristr($tmp, 'log_format anonymized')) {
821+
copy($nginx_conf_file,$nginx_conf_file.'~');
822+
replaceLine($nginx_conf_file, 'http {', "http {\n\n".file_get_contents('tpl/nginx_anonlog.master'), 0, 0);
823+
}
824+
}
825+
815826
}
816827

817828
public function configure_bastille_firewall()

install/dist/lib/opensuse.lib.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,16 @@ public function configure_nginx(){
823823
//* add a sshusers group
824824
$command = 'groupadd sshusers';
825825
if(!is_group('sshusers')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
826+
827+
// add anonymized log option to nginxx.conf file
828+
$nginx_conf_file = $conf['nginx']['config_dir'].'/nginx.conf';
829+
if(is_file($nginx_conf_file)) {
830+
$tmp = file_get_contents($nginx_conf_file);
831+
if(!stristr($tmp, 'log_format anonymized')) {
832+
copy($nginx_conf_file,$nginx_conf_file.'~');
833+
replaceLine($nginx_conf_file, 'http {', "http {\n\n".file_get_contents('tpl/nginx_anonlog.master'), 0, 0);
834+
}
835+
}
826836
}
827837

828838
public function configure_bastille_firewall()

install/lib/installer_base.lib.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,17 @@ public function configure_nginx(){
18431843
//* add a sshusers group
18441844
$command = 'groupadd sshusers';
18451845
if(!is_group('sshusers')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
1846+
1847+
// add anonymized log option to nginxx.conf file
1848+
$nginx_conf_file = $conf['nginx']['config_dir'].'/nginx.conf';
1849+
if(is_file($nginx_conf_file)) {
1850+
$tmp = file_get_contents($nginx_conf_file);
1851+
if(!stristr($tmp, 'log_format anonymized')) {
1852+
copy($nginx_conf_file,$nginx_conf_file.'~');
1853+
replaceLine($nginx_conf_file, 'http {', "http {\n\n".file_get_contents('tpl/nginx_anonlog.master'), 0, 0);
1854+
}
1855+
}
1856+
18461857
}
18471858

18481859
public function configure_fail2ban() {

install/tpl/nginx_anonlog.master

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
map $remote_addr $ip_anonym1 {
2+
default 0.0.0;
3+
"~(?P<ip>(\d+)\.(\d+)\.(\d+))\.\d+" $ip;
4+
"~(?P<ip>[^:]+:[^:]+):" $ip;
5+
}
6+
7+
map $remote_addr $ip_anonym2 {
8+
default .0;
9+
"~(?P<ip>(\d+)\.(\d+)\.(\d+))\.\d+" .0;
10+
"~(?P<ip>[^:]+:[^:]+):" ::;
11+
}
12+
13+
map $ip_anonym1$ip_anonym2 $ip_anonymized {
14+
default 0.0.0.0;
15+
"~(?P<ip>.*)" $ip;
16+
}
17+
18+
log_format anonymized '$ip_anonymized - $remote_user [$time_local] '
19+
'"$request" $status $body_bytes_sent '
20+
'"$http_referer" "$http_user_agent"';

interface/web/admin/lib/lang/en_server_config.lng

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ $wb['skip_le_check_txt'] = 'Skip Lets Encrypt Check';
289289
$wb['migration_mode_txt'] = 'Server Migration Mode';
290290
$wb['nginx_enable_pagespeed_txt'] = 'Makes Pagespeed available';
291291
$wb['logging_txt'] = 'Store website access and error logs';
292-
$wb['logging_desc_txt'] = 'Use Tools > Resync to apply changes to existing sites.';
292+
$wb['logging_desc_txt'] = 'Use Tools > Resync to apply changes to existing sites. For Apache, access and error log can be anonymized. For nginx, only the access log is anonymized, the error log will contain IP addresses.';
293293
$wb['log_retention_txt'] = 'Log retention (days)';
294294
$wb['log_retention_error_ispositive'] = 'Log retention must be a number > 0';
295295
?>

interface/web/admin/templates/server_config_web_edit.htm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ <h4 class="panel-title">
110110
<div class="col-sm-9">
111111
<select name="logging" id="logging" class="form-control">
112112
{tmpl_var name='logging'}
113-
</select>
113+
</select> {tmpl_var name='logging_desc_txt'}
114114
</div>
115115
</div>
116116
<div class="form-group">

server/conf/nginx_vhost.conf.master

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ server {
110110
}
111111
</tmpl_if>
112112

113+
<tmpl_if name='logging' op='==' value='yes'>
113114
error_log /var/log/ispconfig/httpd/<tmpl_var name='domain'>/error.log;
114115
access_log /var/log/ispconfig/httpd/<tmpl_var name='domain'>/access.log combined;
116+
</tmpl_var>
117+
<tmpl_if name='logging' op='==' value='anon'>
118+
error_log /var/log/ispconfig/httpd/<tmpl_var name='domain'>/error.log;
119+
access_log /var/log/ispconfig/httpd/<tmpl_var name='domain'>/access.log anonymized;
120+
</tmpl_var>
115121

116122
## Disable .htaccess and other hidden files
117123
location ~ /\. {

server/plugins-available/nginx_plugin.inc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ function update($event_name, $data) {
15241524
}
15251525
unset($tmp_output, $tmp_retval);
15261526
}
1527+
1528+
// set logging variable
1529+
$vhost_data['logging'] = $web_config['logging'];
15271530

15281531
$tpl->setVar($vhost_data);
15291532

0 commit comments

Comments
 (0)