大网数据广告活动
服务器运维问答

Modify Nginx 301 response body

· 52CE 我爱测

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>ngin…

问题原文

原帖链接 · Lars · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · phbits · CC BY-SA 4.0

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>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。