#####################################################
#  This file is autogenerated by Phusion Passenger Standalone
#  from <%= template_filename %>
#  Please edit that file instead.
#####################################################


master_process on;
worker_processes 1;
daemon on;
error_log '<%= @options[:log_file] %>' <% if debugging? %>info<% end %>;
pid '<%= @options[:pid_file] %>';

events {
    worker_connections 1024;
}

http {
    log_format debug '[$time_local] $msec  "$request" $status conn=$connection sent=$bytes_sent body_sent=$body_bytes_sent';
    include '<%= PhusionPassenger.resources_dir %>/mime.types';
    passenger_ruby <%= PlatformInfo.ruby_command %>;
    passenger_root '<%= location_config_filename %>';
    passenger_abort_on_startup_error on;
    passenger_user_switching off;
    passenger_max_pool_size <%= @options[:max_pool_size] %>;
    passenger_min_instances <%= @options[:min_instances] %>;
    <% if @options[:user] %>
        passenger_user <%= @options[:user] %>;
        passenger_default_user <%= @options[:user] %>;
        passenger_analytics_log_user <%= @options[:user] %>;
    <% else %>
        passenger_user <%= current_user %>;
        passenger_default_user <%= current_user %>;
        passenger_analytics_log_user <%= current_user %>;
    <% end %>
    <% if debugging? %>passenger_log_level 2;<% end %>
    <% if @options[:rolling_restarts] %>passenger_rolling_restarts on;<% end %>
    <% if @options[:resist_deployment_errors] %>passenger_resist_deployment_errors on;<% end %>

    <% unless @options[:friendly_error_pages] %>passenger_friendly_error_pages off;<% end %>

    <% if @options[:union_station_gateway_address] %>
        union_station_gateway_address <%= @options[:union_station_gateway_address] %>;
        union_station_gateway_port <%= @options[:union_station_gateway_port] %>;
        union_station_gateway_cert -;
    <% end %>
    
    default_type application/octet-stream;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;
    client_max_body_size 1024m;
    access_log off;
    keepalive_timeout 60;
    underscores_in_headers on;
    gzip on;
    gzip_comp_level 3;
    gzip_min_length 150;
    gzip_proxied any;
    gzip_types text/plain text/css application/javascript application/x-javascript;
    
    <% if @apps.size > 1 %>
    # Default server entry.
    server {
        listen <%= nginx_listen_address %>;
        root '<%= PhusionPassenger.resources_dir %>/standalone_default_root';
    }
    <% end %>

    <% if @options[:ping_port] %>
    server {
        listen <%= nginx_listen_address(@options, true) %>;
        root '<%= PhusionPassenger.resources_dir %>/standalone_default_root';
    }
    <% end %>
    
    <% for app in @apps %>
    server {
        listen <%= nginx_listen_address(app) %> <%= "ssl" if app[:ssl] %>;
        server_name <%= app[:server_names].join(' ') %>;
        root '<%= app[:root] %>/public';
        passenger_app_root '<%= app[:root] %>';
        passenger_enabled on;
        rails_env <%= app[:env] %>;
        passenger_spawn_method <%= app[:spawn_method] %>;
        <% if app[:concurrency_model] != DEFAULT_CONCURRENCY_MODEL %>passenger_concurrency_model <%= app[:concurrency_model] %>;<% end %>
        <% if app[:thread_count] != DEFAULT_THREAD_COUNT %>passenger_thread_count <%= app[:thread_count] %>;<% end %>
        <% if app[:min_instances] %>passenger_min_instances <%= app[:min_instances] %>;<% end %>
        <% if app[:union_station_key] %>
            union_station_support on;
            union_station_key <%= app[:union_station_key] %>;
        <% end %>
        <% if app[:ssl] %>
            ssl on;
            ssl_certificate <%= app[:ssl_certificate] %>;
            ssl_certificate_key <%= app[:ssl_certificate_key] %>;
        <% end %>

        # Rails asset pipeline support.
        location ~ ^/assets/ {
            error_page 490 = @static_asset;
            error_page 491 = @dynamic_request;
            recursive_error_pages on;

            if (-f $request_filename) {
                return 490;
            }
            if (!-f $request_filename) {
                return 491;
            }
        }
        location @static_asset {
            gzip_static on;
            expires max;
            add_header Cache-Control public;
            add_header ETag "";
        }
        location @dynamic_request {
            passenger_enabled on;
        }
    }
    passenger_pre_start http://<%= nginx_listen_address(app) %>;
    <% end %>
}
