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

标签 redirect nginx

当我转到 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/

相关文章:

5 秒后 PHP 重定向

php - Nginx 将新文件 www-data 设置为所有者而不是我的用户

nginx - 有哪些 proxy_pass 协议(protocol)?

variables - 此处不允许使用 "set"指令

jquery - CORS 预检问题

ruby-on-rails - 使用约束和 block 的 Rails 路由重定向?

servlets - 如何将新 header 传递给 sendRedirect

C 程序未接收到所有输入

node.js - 与 'ws://.../socket.io/' 的 WebSocket 连接失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET

postgresql - 如何设置docker架构