class phabricator::nginx {
  class {'nginx': 
    client_max_body_size => '32M'
  }
  
  $access_log = "${phabricator::logs_dir}nginx-access.log"
  $error_log = "${phabricator::logs_dir}nginx-error.log"
  
  nginx::resource::server {$phabricator::server_url:
    ensure => present,
    use_default_location => false,
    www_root        => "${phabricator::install_dir}phabricator/webroot",
    access_log      => $access_log,
    error_log       => $error_log,
    rewrite_www_to_non_www => true,
    notify => Class['php::fpm::service']
  }
  
  nginx::resource::location{'/':
    ensure => present,
    server => $phabricator::server_url,
    index_files => ['index.php'],
    rewrite_rules => ['^/(.*)$ /index.php?__path__=/$1 last']
  }
  
  nginx::resource::location{'/index.php':
    ensure => present,
    server => $phabricator::server_url,
    fastcgi => "unix:${phabricator::fpm_socket}",
    fastcgi_index  => 'index.php',
    fastcgi_param => {
      'REDIRECT_STATUS'=>    '200',
      'SCRIPT_FILENAME'=>    '$document_root$fastcgi_script_name',
      'QUERY_STRING'=>       '$query_string',
      'REQUEST_METHOD'=>     '$request_method',
      'CONTENT_TYPE'=>       '$content_type',
      'CONTENT_LENGTH'=>     '$content_length',
      'SCRIPT_NAME'=>        '$fastcgi_script_name',
      'GATEWAY_INTERFACE'=>  'CGI/1.1',
      'SERVER_SOFTWARE'=>    'nginx/$nginx_version',
      'REMOTE_ADDR'=>        '$remote_addr'
    }
  }
  
  logrotate::rule { 'nginx':
    ensure        => 'present',
    path          => [$access_log, $error_log],
    compress      => true,
    delaycompress => true,
    ifempty       => false,
    missingok     => true,
    rotate_every  => 'week',
    rotate        => 40
  }
}
