{"id":19869,"date":"2021-02-03T00:27:44","date_gmt":"2021-02-02T18:57:44","guid":{"rendered":"https:\/\/valeurbit.com\/blog\/?p=19869"},"modified":"2021-02-12T18:15:15","modified_gmt":"2021-02-12T12:45:15","slug":"nginx-web-server-hardening-guide","status":"publish","type":"post","link":"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/","title":{"rendered":"Nginx Web Server Hardening Guide"},"content":{"rendered":"\n<p><a href=\"https:\/\/nginx.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Nginx<\/a>&nbsp;&nbsp;is the fastest growing web server in the industry and currently holds 31% of the market share.<\/p>\n\n\n\n<p>It was originally released in 2004 and has since earned an excellent reputation and serves the servers of many high-load Russian sites such as Yandex, VKontakte, Mail.Ru, Rambler, etc.<\/p>\n\n\n\n<p>There&#8217;s a reason for this &#8211; Nginx is fast.<\/p>\n\n\n\n<p>In this article, I will share some important guides for securing an Nginx web server.&nbsp;So, let&#8217;s begin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SSL \/ TLS<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">SSL Certificate Implementation<\/h3>\n\n\n\n<p>The first step in web security is to implement SSL so that you can access web applications over&nbsp;<strong>https<\/strong>&nbsp;and add a layer of encryption when communicating.<\/p>\n\n\n\n<ul><li>Use OpenSSL to generate CSR with 2048 bits and sha-2<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>openssl req -nodes -new -sha256 -newkey rsa:2048 -keyout bestflare.key -out bestflare.csr<\/code><\/pre>\n\n\n\n<ul><li>The above command will generate CSR files and key files on current job directly.&nbsp;Don&#8217;t forget to change the file name&nbsp;<strong>.csr<\/strong>&nbsp;and&nbsp;<strong>.key<\/strong>&nbsp;.<\/li><\/ul>\n\n\n\n<p>Get a CSR signed by a CA, and once you get a certificate, you can implement it in Nginx as shown below.<\/p>\n\n\n\n<ul><li>Login to nginx server<\/li><li>Go to&nbsp;<strong>conf<\/strong>&nbsp;folder&nbsp;where you have&nbsp;<strong>ssl.conf<\/strong>&nbsp;file&nbsp;.<\/li><\/ul>\n\n\n\n<p>Note.&nbsp;For a default installation on Linux, this file will be located in \/etc\/nginx\/conf.d.<\/p>\n\n\n\n<ul><li>Edit the file and add the following to allow Nginx to listen on port 443<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\nlisten&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 443 ssl;\n&nbsp;&nbsp; server_name bestflare.com;\n&nbsp;&nbsp; ssl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; on;\n&nbsp;&nbsp; ssl_certificate&nbsp;&nbsp;&nbsp;&nbsp; \/opt\/cert\/bestflare.pem;\n&nbsp;&nbsp; ssl_certificate_key \/opt\/cert\/bestflare.key;\n&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>&nbsp;: don&#8217;t forget to change the path to the certificate and key file.<\/p>\n\n\n\n<ul><li>Save your configuration and restart Nginx.&nbsp;The SSL certificate has been successfully deployed.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disable SSL 3 and leave only TLS<\/h3>\n\n\n\n<p>SSL 3 is vulnerable and we will only allow secure TLS.<\/p>\n\n\n\n<ul><li>Edit&nbsp;<strong>ssl.conf<\/strong>&nbsp;file&nbsp;and add below in server block<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssl_protocols&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TLSv1.2;<\/code><\/pre>\n\n\n\n<p>Save the&nbsp;<strong>ssl.conf<\/strong>&nbsp;file&nbsp;and restart Nginx<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Disable weak encryption suites<\/h3>\n\n\n\n<p>Weak cipher suites can lead to vulnerabilities such as an outage and therefore we only need to allow strong ciphers.<\/p>\n\n\n\n<ul><li>Add the following to the server block in your&nbsp;<strong>ssl.conf<\/strong>&nbsp;file<strong><\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ssl_ciphers \"EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA HIGH !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS\";<\/code><\/pre>\n\n\n\n<ul><li>Save the file and restart Nginx<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Install Chain Certificate<\/h3>\n\n\n\n<p>The lack of a chain certificate also affects the overall rating, and this can lead to an error when viewed in a modern browser like Chrome.&nbsp;You need to get a chain certificate.<\/p>\n\n\n\n<ul><li>Add the contents of the chain certificate to the site certificate as shown below.&nbsp;In my example, this would be&nbsp;<strong>\/opt\/cert\/bestflare.pem<\/strong><\/li><\/ul>\n\n\n\n<ul><li>Save the file and restart Nginx<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Web Application Security<\/h2>\n\n\n\n<p>The default Nginx configuration is not perfect and can have many vulnerabilities, so we are hardening them to make it secure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Disable unwanted HTTP methods<\/h3>\n\n\n\n<p>In most cases, you just need to receive an HTTP GET, HEAD &amp; POST request in your web application.&nbsp;Allowing TRACE or DELETE is risky as it could allow a cross-site tracking attack and potentially allow a hacker to steal cookie information.<\/p>\n\n\n\n<ul><li>Modify&nbsp;<strong>default.conf<\/strong>&nbsp;and add the following below the server block<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($request_method !~ ^(GET|HEAD|POST)$ )\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 405;\n}<\/code><\/pre>\n\n\n\n<p>Save the file and restart Nginx.&nbsp;Now 405 Not Allowed will be displayed if someone tries to use TRACE, DELETE, PUT, OPTIONS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Chandans-iMac:~ chandan$ telnet bestflare.com 80\nTrying 128.199.100.162...\nConnected to bestflare.com.\nEscape character is '^]'.\nTRACE \/ HTTP\/1.1\nHost: testing\nHTTP\/1.1 405 Not Allowed\nServer: nginx\nDate: Sat, 11 Jul 2015 06:04:34 GMT\nContent-Type: text\/html\nContent-Length: 166\nConnection: close<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Clickjacking attack<\/h2>\n\n\n\n<p>You can enter the X-FRAME-OPTIONS HTTP header in order to prevent an attack using the&nbsp;Click-Jacking&nbsp;.<\/p>\n\n\n\n<p>This is achieved by adding below to the&nbsp;<strong>nginx.conf<\/strong>&nbsp;file<strong><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_header X-Frame-Options \"SAMEORIGIN\";<\/code><\/pre>\n\n\n\n<p>The header will tell the browser to load resources ONLY from the same source.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">X-XSS Protection<\/h2>\n\n\n\n<p>Implement an X-XSS protected HTTP header to prevent cross-site scripting attacks.<\/p>\n\n\n\n<p>Modify your&nbsp;<strong>default.conf<\/strong>&nbsp;or&nbsp;<strong>ssl.conf<\/strong>&nbsp;file&nbsp;to add the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_header X-XSS-Protection \"1; mode=block\";<\/code><\/pre>\n\n\n\n<ul><li>Save the config file and restart Nginx.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Nginx&nbsp;&nbsp;is the fastest growing web server in the industry and currently holds 31% of the market share. It was originally released in 2004 and has since earned an excellent reputation and serves the servers of many high-load Russian sites such as Yandex, VKontakte, Mail.Ru, Rambler, etc. There&#8217;s a reason for this &#8211; Nginx is fast&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Nginx Web Server Hardening Guide | ValeurBit Infosec<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nginx Web Server Hardening Guide | ValeurBit Infosec\" \/>\n<meta property=\"og:description\" content=\"Nginx&nbsp;&nbsp;is the fastest growing web server in the industry and currently holds 31% of the market share. It was originally released in 2004 and has since earned an excellent reputation and serves the servers of many high-load Russian sites such as Yandex, VKontakte, Mail.Ru, Rambler, etc. There&#8217;s a reason for this &#8211; Nginx is fast....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"ValeurBit Infosec\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/valeurbitinfo\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-02T18:57:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-12T12:45:15+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@valeurbit\" \/>\n<meta name=\"twitter:site\" content=\"@valeurbit\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\",\"name\":\"Valeurbit Infosec\",\"url\":\"https:\/\/valeurbit.com\/blog\/\",\"sameAs\":[\"https:\/\/www.facebook.com\/valeurbitinfo\/\",\"https:\/\/www.instagram.com\/valeurbit\",\"https:\/\/www.linkedin.com\/company\/valeurbit-infosec\/\",\"https:\/\/twitter.com\/valeurbit\"],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/valeurbit.com\/blog\/wp-content\/uploads\/2021\/02\/Valeurbit-new-logo-center.png\",\"contentUrl\":\"https:\/\/valeurbit.com\/blog\/wp-content\/uploads\/2021\/02\/Valeurbit-new-logo-center.png\",\"width\":1080,\"height\":512,\"caption\":\"Valeurbit Infosec\"},\"image\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#website\",\"url\":\"https:\/\/valeurbit.com\/blog\/\",\"name\":\"ValeurBit Infosec\",\"description\":\"Cyber Security Company\",\"publisher\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/valeurbit.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#webpage\",\"url\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/\",\"name\":\"Nginx Web Server Hardening Guide | ValeurBit Infosec\",\"isPartOf\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#website\"},\"datePublished\":\"2021-02-02T18:57:44+00:00\",\"dateModified\":\"2021-02-12T12:45:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/valeurbit.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nginx Web Server Hardening Guide\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#webpage\"},\"author\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#\/schema\/person\/df20c1cd317765fa8677a3056caeccfa\"},\"headline\":\"Nginx Web Server Hardening Guide\",\"datePublished\":\"2021-02-02T18:57:44+00:00\",\"dateModified\":\"2021-02-12T12:45:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/nginx-web-server-hardening-guide\/#webpage\"},\"wordCount\":597,\"publisher\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\"},\"articleSection\":[\"Valeurbit\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#\/schema\/person\/df20c1cd317765fa8677a3056caeccfa\",\"name\":\"ValeurBit\",\"sameAs\":[\"https:\/\/valeurbit.com\/blog\"],\"url\":\"https:\/\/valeurbit.com\/blog\/author\/valeurbit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts\/19869"}],"collection":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/comments?post=19869"}],"version-history":[{"count":0,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts\/19869\/revisions"}],"wp:attachment":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/media?parent=19869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/categories?post=19869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/tags?post=19869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}