Nginx – Modify Nginx 301 response body

301-redirectnginx

So, when doing curl -i http://example.com on my server, I get this response in body:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

It shows that I'm running nginx and I'd like to remove this information.

Here's my nginx.conf redirect to HTTPS (together with my try to change 301 response body):

server {
    listen       80;
    server_name  localhost;
    error_page 301 = /301.html;
    location /301.html {
        return 301 "<h1>use https</h1>";
    }
    return 301   https://$host$request_uri;
}

Any idea how to change 301 response body?

Best Answer

There's a great post about custom error pages at the following URL. The following is an abbreviated version of how it can be used to remove nginx branding from the 301 and 302 HTTP responses.

One NGINX error page to rule them all

nginx.conf

http {

  map $status $status_text {
    301 'Moved Permanently';
    302 'Found';
  }

  server {

    listen 1.1.1.1;
    server_name _;

    error_page 301 302 /error.html;

    location = /error.html {
      ssi on;
      internal;
      auth_basic off;
      root /var/website/custom_error_pages;
    }

    root /var/website/empty_dir;
  
    location / {
      return 301 https://www.website.com$request_uri;
    }
}

/error.html

<html>
  <head><title>www.website.com</title></head>
  <body><h1>
    <!--# echo var="status" default="000" --> - <!--# echo var="status_text" default="Error" -->
  </h1></body>
</html>