NginxConf - file /etc/nginx/nginx.conf and other Nginx configuration files

NginxConfPEG - file /etc/nginx/nginx.conf and other Nginx configuration files

class insights.parsers.nginx_conf.NginxConf(*args, **kwargs)[source]

Bases: insights.core.Parser, insights.core.LegacyItemAccess

Warning

This parser is deprecated, please import insights.combiners.nginx_conf.NginxConfTree instead.

Parse the keyword-and-value of a Nginx configuration file.

Generally, each line is split on the first space into key and value, leading and trailing space being ignored.

Example nginx.conf file:

user       root
worker_processes  5;
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;
}

mail {
  server_name mail.example.com;
  auth_http  localhost:9000/cgi-bin/auth;
  server {
    listen   143;
    protocol imap;
  }
}

http {
  include  /etc/nginx/conf.d/*.conf
  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
                    '"$request" $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log   logs/access.log  main;
  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128;

  server { # php/fastcgi
    listen       80;
    server_name  domain1.com www.domain1.com;
    access_log   logs/domain1.access.log  main;
    root         html;

    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:1025;
    }
  }

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;
    access_log   logs/domain2.access.log  main;

    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
      expires 30d;
    }

    location / {
      proxy_pass   http://127.0.0.1:8080;
    }
  }

  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  upstream websocket {
    server 10.66.208.205:8010;
  }

  upstream big_server_com {
    server 127.0.0.3:8000 weight=5;
    server 127.0.0.3:8001 weight=5;
    server 192.168.0.1:8000;
    server 192.168.0.1:8001;
  }

  server { # simple load balancing
    listen          80;
    server_name     big.server.com;
    access_log      logs/big.server.access.log main;

    location / {
      proxy_pass      http://big_server_com;
    }
  }
}

Examples

>>> type(nginxconf)
<class 'insights.parsers.nginx_conf.NginxConf'>
>>> nginxconf['user']
'root'
>>> nginxconf['events']['worker_connections'] # Values are all kept as strings.
'4096'
>>> nginxconf['mail']['server'][0]['listen']
'143'
>>> nginxconf['http']['access_log']
'logs/access.log  main'
>>> nginxconf['http']['server'][0]['location'][0]['fastcgi_pass']
'127.0.0.1:1025'
parse_content(content)[source]

This method must be implemented by classes based on this class.

class insights.parsers.nginx_conf.NginxConfPEG(*args, **kwargs)[source]

Bases: insights.core.ConfigParser

Parse the keyword-and-value of a Nginx configuration file.

Example nginx.conf file:

user       root
worker_processes  5;
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;
}

mail {
  server_name mail.example.com;
  auth_http  localhost:9000/cgi-bin/auth;
  server {
    listen   143;
    protocol imap;
  }
}

http {
  include  /etc/nginx/conf.d/*.conf
  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
                    '"$request" $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log   logs/access.log  main;
  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128;

  server { # php/fastcgi
    listen       80;
    server_name  domain1.com www.domain1.com;
    access_log   logs/domain1.access.log  main;
    root         html;

    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:1025;
    }
  }

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;
    access_log   logs/domain2.access.log  main;

    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
      expires 30d;
    }

    location / {
      proxy_pass   http://127.0.0.1:8080;
    }
  }

  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  upstream websocket {
    server 10.66.208.205:8010;
  }

  upstream big_server_com {
    server 127.0.0.3:8000 weight=5;
    server 127.0.0.3:8001 weight=5;
    server 192.168.0.1:8000;
    server 192.168.0.1:8001;
  }

  server { # simple load balancing
    listen          80;
    server_name     big.server.com;
    access_log      logs/big.server.access.log main;

    location / {
      proxy_pass      http://big_server_com;
    }
  }
}

Examples

>>> type(nginxconfpeg)
<class 'insights.parsers.nginx_conf.NginxConfPEG'>
>>> nginxconfpeg['user'][-1].value
'root'
>>> nginxconfpeg['events']['worker_connections'][-1].value
4096
>>> nginxconfpeg['mail']['server'][0]['listen'][-1].value
143
>>> nginxconfpeg['http']['access_log'][-1].value
'logs/access.log main'
>>> nginxconfpeg['http']['server'][0]['location'][0]['fastcgi_pass'][-1].value
'127.0.0.1:1025'