Debugging nginx config with add_header

2024-03-03

When nginx doesn't work LOL

Problem

Sometimes you are configuring the nginx server and it doesn’t work as expected. You are not sure what’s wrong with the config file. You can use the add_header directive to debug the issue.

Solution

For this tutorial i am using this config file which you can find in my github repo

 
http {
server {
listen 8080;
location / {
root /usr/share/nginx/html;
}
location /site1/ {
# here is the add_header directive
add_header Debug_value "Daniel was here";
root /;
}
location /site2/ {
root /;
}
}
}
events {}

You can check this value on your browser by inspecting the network tab. You can see the Debug_value in the response header.

debug_value