gpt4 book ai didi

redirect - 如何在没有 html header 的情况下将 nginx 重定向到非 http

转载 作者:行者123 更新时间:2023-12-01 05:21:32 24 4
gpt4 key购买 nike

当我转到 test.com/123456 时,我正在尝试将 nginx 中的 url 重定向到非 http 协议(protocol),例如 test://123456

我尝试了以下重写:
重写 ^/(.*)$ test://$1 永久;

它的工作原理是奇怪的部分,它添加了 html/body header ,这弄乱了我的代码,没有 html header 或任何其他推荐的重写方法有什么办法吗?

HTTP/1.1 301 Moved Permanently  
Server: nginx/1.1.19
Date: Tue, 30 Apr 2013 14:14:47 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: test://123456
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>

最佳答案

这并不奇怪,这就是它应该的样子。

RFC 2616 specifies that the entity bodies you want to remove should be present.

10.3.2 301 Moved Permanently

The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).



和...

10.3.3 302 Found

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).



在这种情况下,应该在 RFC 2119 中定义。 :

This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.



答案来自 NGINX 301 and 302 serving small nginx document body. Any way to remove this behaviour?

当然你仍然可以这样做,一种可能性是代理请求并将请求方法从 GET 更改为 HEAD。这应该确保只发送 HTTP header 。

这是未经测试的,但它应该是一个很好的起点:
server {
listen 8080;
server_name localhost;
return 301 test://$request_uri;
}

server {
listen 80;
server_name example.com;
location / {
proxy_method HEAD;
proxy_pass http://localhost:8080;
}
}

同样来自对这方面的兴趣 NGINX convert HEAD to GET requests .

关于redirect - 如何在没有 html header 的情况下将 nginx 重定向到非 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16302361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com