修改Nginx 301响应主体

修改Nginx 301响应主体

因此,当curl -i http://example.com我在服务器上执行此操作时,我会收到以下响应:

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

它显示我正在运行 nginx 并且我想删除此信息。

这是我的 nginx.conf 重定向到 HTTPS(连同我尝试更改 301 响应主体):

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;
}

知道如何更改 301 响应主体吗?

答案1

以下 URL 上有一篇关于自定义错误页面的精彩文章。以下是如何使用它从 HTTP 响应中删除 nginx 品牌的简略301版本302

一个 NGINX 错误页面解决所有问题

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;
    }
}

/错误.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>

答案2

创建文件301.html,该文件应该包含您想要显示的内容。如果文件路径为,请/usr/share/nginx/html/301.html调整配置为:

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/;
    }

}

这将返回您的自定义 301.html 文件

答案3

尝试将return 301 https://$host$request_uri;指令放在块内location / {。只需一行即可。

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

答案4

phbits 接受的答案很棒,但如果你想将它应用于反向代理的情况proxy_intercept_errors on,那么就会有一些陷阱,这样错误页面也会替换后端发送的错误(相关邮件列表主题)。

如果您的后端回复了 301 或 302,nginx 将Location:在向客户端提供错误页面时删除标头,尽管保留了正确的状态代码。因此,客户端将看到一个显示<h1>302 Found</h1>但不会被重定向的页面。解决方案是使用 从上游响应中重新添加 Location 标头add_header Location "$upstream_http_location"

我花了一些时间才弄清楚,所以下面是我的完整设置,基于Adriaan 的博客,以防将来有人遇到同样的情况。

/etc/nginx/sites-enabled/myserver.conf

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name example.com;

    include snippets/errorpage.conf;

    location / {
        proxy_pass http://127.0.0.1:7080/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

/etc/nginx/snippets/errorpage.conf

error_page 01 302 303 304 307 308 400 401 402 403 404 405 406 407 408 409 410      
    411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 
    431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;
                                                                    
location /redirect-test {        # Just for testing                                   
    return 302 /foo-bar;         # Use nc example.com 80 <<<$'GET /redirect-test HTTP/1.0\r\nHost: example.com\r\n\r\n'                                   
}                                                                   
                                                                    
proxy_intercept_errors on;                                          
location = /error.html {                                            
  ssi on;                                                           
  internal;                                                         
  auth_basic off;                                                   
  root /var/www/default;                                            
  add_header Location "$upstream_http_location";                    
}                                                                   

/etc/nginx/conf.d/status-codes.conf

map $status $status_text {
  301 'Moved Permanently';
  302 'Found';
  303 'See Other';                                                                                      
  304 'Not Modified';                                                                                   
  307 'Temporary Redirect';                                                                             
  308 'Permanent Redirect';      
  400 'Bad Request';
  401 'Unauthorized';
  402 'Payment Required';
  403 'Forbidden';
  404 'Not Found';
  405 'Method Not Allowed';
  406 'Not Acceptable';
  407 'Proxy Authentication Required';
  408 'Request Timeout';
  409 'Conflict';
  410 'Gone';
  411 'Length Required';
  412 'Precondition Failed';
  413 'Payload Too Large';
  414 'URI Too Long';
  415 'Unsupported Media Type';
  416 'Range Not Satisfiable';
  417 'Expectation Failed';
  418 'I\'m a teapot';
  421 'Misdirected Request';
  422 'Unprocessable Entity';
  423 'Locked';
  424 'Failed Dependency';
  425 'Too Early';
  426 'Upgrade Required';
  428 'Precondition Required';
  429 'Too Many Requests';
  431 'Request Header Fields Too Large';
  451 'Unavailable For Legal Reasons';
  500 'Internal Server Error';
  501 'Not Implemented';
  502 'Bad Gateway';
  503 'Service Unavailable';
  504 'Gateway Timeout';
  505 'HTTP Version Not Supported';
  506 'Variant Also Negotiates';
  507 'Insufficient Storage';
  508 'Loop Detected';
  510 'Not Extended';
  511 'Network Authentication Required';
  default 'Something is wrong';
}

/var/www/default/error.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>example.com Error</title>
  </head>
<body>
    <h1><!--# echo var="status" default="" --> <!--# echo var="status_text" default="Something goes wrong" --></h1>
</body>
</html>

/etc/nginx/conf.d/no-headers.conf

#more_clear_headers Server;   # Remove Server header, or
more_set_headers 'Server: haproxy'; # Or whatever

相关内容