Modify Nginx 301 response body
6,925
Solution 1
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.
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>
Solution 2
create file 301.html
, this file should contains content which you want to display. If path to file is /usr/share/nginx/html/301.html
adjust config to:
server {
listen 80;
server_name localhost;
location / {
error_page 301 = /301.html;
return 301 https://$host$request_uri;
}
location /301.html {
root /usr/share/nginx/html/;
}
}
And this will return your custom 301.html file
Related videos on Youtube
Comments
-
Lars over 1 year
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?
-
Lars about 7 yearsOne option is to edit source code: github.com/nginx/nginx/blob/master/src/http/… But it seems like an overkill.
-
-
Rishabh Singhal over 6 yearsThis actually changes HTTP code to 200 which stops the actual redirect. A very bad solution.
-
svobol13 over 4 yearsI dont think this will work since second param to 301 modifies location header instead of body.