/// <summary> /// Serializes a single NGINX configuration and appends this to the StringBuilder. /// </summary> /// <param name="configSection">NGINX configuration section to serialize.</param> /// <param name="sb">StringBuilder to append the serialized NGINX configuration.</param> /// <param name="depth">Depth of serialization.</param> private void SerializeNginxConfig(NginxConfigSection configSection, StringBuilder sb, int depth) { var nextDepth = depth + 1; var indentation = new string('\t', depth); var nextIndentation = new string('\t', nextDepth); var sectionHead = indentation + configSection.Name + (string.IsNullOrWhiteSpace(configSection.Parameters) ? string.Empty : " " + configSection.Parameters); sb.AppendLine(sectionHead + " {"); foreach (var keyValuePair in configSection.Attributes) { if (keyValuePair.Key == "set_encode_base64") { sb.AppendLine($"{nextIndentation}{ keyValuePair.Key }{" "}{ keyValuePair.Value };"); } else { var escapedConfigValue = (sectionHead.Equals("server") || sectionHead.Equals("\tserver") || sectionHead.Equals("events") || sectionHead.Equals("http")) ? string.Empty : "\""; sb.AppendLine($"{nextIndentation}{ keyValuePair.Key } {escapedConfigValue}{ keyValuePair.Value }{escapedConfigValue};"); } } foreach (var subsection in configSection.Subsections) { SerializeNginxConfig(subsection, sb, nextDepth); } sb.AppendLine(indentation + "}"); }
private string CreateEventsSection() { var sectionEvents = new NginxConfigSection { Name = "events", Attributes = new List <NginxAttribute> { new NginxAttribute("worker_connections", "1024") } }; return(SerializeNginxConfigList(new List <NginxConfigSection> { sectionEvents })); }
/// <summary> /// Generates the NGINX configuration for all given resources and adds standard NGINX server information to the returned configuration. /// </summary> /// <param name="resources">List of resources to generate the NGINX proxy configuration from.</param> /// <returns>NGINX configuration sections for the NGINX servers and all given resources.</returns> private IList <NginxConfigSection> CreateConfigForResources(IList <ResourceProxyDTO> resources) { var sectionHttp = new NginxConfigSection { Name = "server", Attributes = new List <NginxAttribute> { new NginxAttribute("listen", "80"), new NginxAttribute("server_name", string.Concat(_colidDomain, " www.", _colidDomain)), new NginxAttribute("return", "301 https://$server_name$request_uri"), } }; var sectionHttps = new NginxConfigSection { Name = "server", Attributes = new List <NginxAttribute> { new NginxAttribute("listen", "443 ssl http2"), new NginxAttribute("include", "snippets/self-signed.conf"), new NginxAttribute("include", "snippets/ssl-params.conf"), new NginxAttribute("server_name", string.Concat(_colidDomain, " www.", _colidDomain)), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Origin' \"*\""), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Credentials' 'true'"), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'"), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With'"), }, Subsections = resources != null?GenerateConfigSections(resources) : new List <NginxConfigSection>() }; sectionHttps.Subsections.Add(new NginxConfigSection { Name = "location", Parameters = "/", Attributes = new List <NginxAttribute> { new NginxAttribute("rewrite ^.*", _dmpFrontEndUrl) //new NginxAttribute("proxy_pass", colidFrontEndUrl) //new NginxAttribute("proxy_set_header", "X-Real-IP $remote_addr") //new NginxAttribute("proxy_set_header", "X-Forwarded-For $proxy_add_x_forwarded_for") } }); return(new List <NginxConfigSection> { sectionHttp, sectionHttps }); }
private string CreateHttpSection(IList <ResourceProxyDTO> resources) { var tmpPath = "/var/cache/nginx/ 1 2"; var configs = GenerateConfigSections(resources); var sectionHttp = new NginxConfigSection { Name = "http", Attributes = new List <NginxAttribute> { new NginxAttribute("client_body_temp_path", tmpPath), new NginxAttribute("proxy_temp_path", tmpPath), new NginxAttribute("fastcgi_temp_path", tmpPath), new NginxAttribute("uwsgi_temp_path", tmpPath), new NginxAttribute("scgi_temp_path", tmpPath), new NginxAttribute("proxy_buffering", "off") }, Subsections = new List <NginxConfigSection> { new NginxConfigSection { Name = "server", Attributes = new List <NginxAttribute> { new NginxAttribute("server_tokens", "off"), new NginxAttribute("listen", "8081"), new NginxAttribute("access_log", "/dev/null"), new NginxAttribute("error_log", "/dev/null") }, Subsections = new List <NginxConfigSection> { new NginxConfigSection { Name = "location", Parameters = "= /", Attributes = new List <NginxAttribute> { new NginxAttribute("rewrite ^.*", "https://www.bayer.com/") } } } }, new NginxConfigSection { Name = "server", Attributes = new List <NginxAttribute> { new NginxAttribute("server_tokens", "off"), new NginxAttribute("listen", "8080"), new NginxAttribute("root", "/usr/share/nginx/html/"), new NginxAttribute("index", "index.html index.htm"), new NginxAttribute("include", "/etc/nginx/mime.types"), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Origin' \"*\""), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Credentials' 'true'"), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'"), new NginxAttribute("proxy_set_header", "'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With'"), }, Subsections = resources != null ? configs : new List <NginxConfigSection>() } } }; return(SerializeNginxConfigList(new List <NginxConfigSection> { sectionHttp })); }