Skip to content

Commit 48a14e0

Browse files
committed
Nginx headers
1 parent 234a082 commit 48a14e0

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: "How to set Nginx headers"
3+
author_name: "Edison Garcia"
4+
tags:
5+
- Nginx
6+
- Security
7+
- Configuration
8+
categories:
9+
- Azure App Service on Linux, Azure Linux Virtual Machine
10+
- Nginx
11+
- Configuration
12+
header:
13+
teaser: /assets/images/nginxlogo.png # There are multiple logos that can be used in "/assets/images" if you choose to add one.
14+
# If your Blog is long, you may want to consider adding a Table of Contents by adding the following two settings.
15+
toc: true
16+
toc_sticky: true
17+
date: 2023-02-24 12:00:00
18+
---
19+
20+
This post will cover how you can modify Nginx headers, in specific for security perspective but it applies for any custom header needed.
21+
>**Note**: This is just a reference in how to do it, the values posted here are just examples and these are not recommended to use in production scenarios, every app's purpose is different and will require validation for the best security headers and be tested in a non-production environments first.
22+
23+
# Server Header
24+
25+
As part of the best recommendations for hardening your server is to remove the nginx version, you can do it following the next steps:
26+
27+
![Nginx ServerName Header](/media/2023/02/nginx-headers-01.png)
28+
29+
## App Service Linux
30+
31+
1. SSH your web app through `https://SITE-NAME-HERE.scm.azurewebsites.net/webssh`
32+
2. Copy the existing `nginx.conf` file with `cp /etc/nginx.conf /home/site/`
33+
3. Modify `nginx.conf` with `nano /home/site/nginx.conf`
34+
4. Uncomment `server_tokens off` under http section:
35+
36+
![Nginx ServerName Header](/media/2023/02/nginx-headers-02.png)
37+
38+
5. Save (`Control + o`) and then `Enter` and close (`Control + x`)
39+
6. Create a startup script in any location inside home directory, example: `/home/site/startup.sh` with the following content:
40+
41+
```shell
42+
#!/bin/bash
43+
44+
cp /home/site/nginx.conf /etc/nginx/nginx.conf
45+
service nginx reload
46+
```
47+
7. Update `Startup Command` using Azure Portal from `Configuration` -> `General Settings` with the startup script location `/home/site/startup.sh`
48+
49+
![Nginx ServerName Header](/media/2023/02/nginx-headers-03.png)
50+
51+
Or using Azure CLI:
52+
53+
```shell
54+
az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "/home/site/startup.sh"
55+
```
56+
57+
## Azure Virtual Machine
58+
>These steps can be applied for Debian based distributions.
59+
60+
1. Modify `nginx.conf` with `sudo nano /etc/nginx/nginx.conf`
61+
2. Uncomment `server_tokens off` under http section:
62+
63+
![Nginx ServerName Header](/media/2023/02/nginx-headers-02.png)
64+
65+
3. Save (`Control + o`) and then `Enter` and close (`Control + x`)
66+
4. Restart nginx with `sudo service nginx restart` and test the configuration with `sudo nginx -t` for any typo or issue.
67+
68+
# Security Headers
69+
70+
You can use `add_header` directive to add security headers that your server needs, here is a list of the most common ones:
71+
72+
- X-Frame-Options
73+
- Strict-Transport-Security (HTTP Strict Transport Security (HSTS))
74+
- X-XSS-Protection
75+
- Content-Security-Policy (Content Security Policy (CSP))
76+
- Referrer-Policy
77+
78+
## App Service Linux
79+
80+
1. SSH your web app through `https://SITE-NAME-HERE.scm.azurewebsites.net/webssh`
81+
2. Copy the existing `default` file with `cp /etc/nginx/sites-enabled/default /home/site/`
82+
3. Modify `default` with `nano /home/site/default`
83+
4. Under `server` tag add the security headers:
84+
85+
```bash
86+
add_header X-Frame-Options "SAMEORIGIN";
87+
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
88+
add_header X-XSS-Protection "1; mode=block";
89+
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
90+
add_header Referrer-Policy "strict-origin";
91+
```
92+
93+
![Nginx Security Headers](/media/2023/02/nginx-headers-04.png)
94+
95+
5. Save (`Control + o`) and then `Enter` and close (`Control + x`)
96+
6. Create a startup script in any location inside home directory, example: `/home/site/startup.sh` with the following content:
97+
98+
```shell
99+
#!/bin/bash
100+
101+
cp /home/site/default /etc/nginx/sites-enabled/default
102+
service nginx reload
103+
```
104+
105+
7. Update `Startup Command` using Azure Portal from `Configuration` -> `General Settings` with the startup script location `/home/site/startup.sh`
106+
107+
![Nginx ServerName Header](/media/2023/02/nginx-headers-03.png)
108+
109+
Or using Azure CLI:
110+
111+
```shell
112+
az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "/home/site/startup.sh"
113+
```
114+
115+
![Nginx ServerName Header](/media/2023/02/nginx-headers-05.png)
116+
117+
## Azure Virtual Machine
118+
1. Modify `default` with `sudo nano /etc/nginx/sites-enabled/default`
119+
2. Under `server` tag add the security headers:
120+
121+
```bash
122+
add_header X-Frame-Options "SAMEORIGIN";
123+
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
124+
add_header X-XSS-Protection "1; mode=block";
125+
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
126+
add_header Referrer-Policy "strict-origin";
127+
```
128+
129+
![Nginx Security Headers](/media/2023/02/nginx-headers-04.png)
130+
131+
3. Save (`Control + o`) and then `Enter` and close (`Control + x`)
132+
4. Restart nginx with `sudo service nginx restart` and test the configuration with `sudo nginx -t` for any typo or issue.
133+
134+
>Note: If you are using Let's encrypt, cerbot will add this configuration `include /etc/letsencrypt/options-ssl-nginx.conf` which can be conflicting with security headers, review if there is any header that is overwritten in this file.

media/2023/02/nginx-headers-01.png

49 KB
Loading

media/2023/02/nginx-headers-02.png

15.2 KB
Loading

media/2023/02/nginx-headers-03.png

19.7 KB
Loading

media/2023/02/nginx-headers-04.png

54.6 KB
Loading

media/2023/02/nginx-headers-05.png

48.4 KB
Loading

0 commit comments

Comments
 (0)