使用Nginx时缺少HTTP状态代码名称 [英] HTTP status code names are missing when using Nginx
问题描述
我正在使用Nginx
将所有HTTP请求重定向到HTTPS
在我的Spring Boot应用程序中.这是我正在使用的Nginx配置,通过它我可以将所有请求重定向到Https,但是当我这样做时,我会正确返回状态代码但是它不再具有状态代码名称.如果我删除nginx并单独运行spring boot应用程序,则可以使用其代码名称和代码获得http状态.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _ ;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ( $http_x_forwarded_proto != 'https' ) {
return 307 https://$host$request_uri;
}
location / {
proxy_set_header X-Forwarded-Proto http;
proxy_pass http://localhost:7070;
expires -1;
}
}
我在这里做错了什么,我应该使用 proxy_redirect 而不是 proxy_pass ,或者我在这里遗漏了任何东西.如果您可以提供帮助,那将是很棒的.
-
您拥有的nginx.conf代码有点混乱和不完整,因为您实际上没有显示任何实际执行
https
服务的代码,因此,尚不清楚整个设置如何工作完全没有. -
proxy_redirect
通常应保留其默认值default
,除非您特别知道要将其更改为什么.请参见 http://nginx.org/r/proxy_redirect 上的文档. -
通常仅在后端需要条件重定向,例如
if ( $http_x_forwarded_proto != 'https' ) {return 307 https://$host$request_uri;}
;目前尚不清楚为什么要在nginx中使用它,除非在它前面有另一个nginx,这将是多余的,而且可能是不必要的. -
最后,您最主要的担心是可能会返回 HTTP状态代码 ,而不会显示状态名称" .首先,状态代码名称",例如
302
后的Moved Temporarily
,或者在201
之后的Created
并不是真正重要的内容,因此,即使在极少的情况下它们都丢失了,也不清楚-不清楚为什么它们首先会被nginx丢失,并且您没有提供进一步的详细信息来进行故障排除-无论如何,它实际上并不会影响任何其他功能(但是,再次,没有证据表明是nginx导致了这些功能的缺失,实际上,nginx确实定义了"201 Created"
在ngx_http_status_lines
在src/http/ngx_http_header_filter_module.c
中的字符串数组中.). >但是,最近在邮件列表中出现了与 HTTP状态代码 相关的问题- —并指出,按照 http://nginx.org/r/proxy_http_version ,这可能会导致您的非nginx后端对 解决方案
The nginx.conf code you have is a bit confusing and incomplete, because you don't actually show any code that does the actual serving of
https
, so, it's unclear how the whole setup would be working at all.The
proxy_redirect
should generally be left at its default value ofdefault
, unless you specifically know what you want to change it to; see the documentation at http://nginx.org/r/proxy_redirect.The conditional redirect, e.g.,
if ( $http_x_forwarded_proto != 'https' ) {return 307 https://$host$request_uri;}
, would normally only be needed on your backend; it's unclear why you'd have this in your nginx, unless you have another nginx in front of it, which would be kinda redundant and likely unnecessary.Finally, your main concern is that HTTP Status Codes may be returned without status "names". First of all, status code "names", like
Moved Temporarily
after302
, orCreated
after201
, aren't really essential to anything, so, even in the unlikely event that they're missing — it's not very clear why'd they be missing with nginx in the first place, and you provided no further details to enable the troubleshooting — it shouldn't really affect any other functionality anyways (but, again, there's no proof that it's nginx that causes them to be missing, and, in fact, nginx does define"201 Created"
in thengx_http_status_lines
array of strings withinsrc/http/ngx_http_header_filter_module.c
).However, a related issue regarding HTTP Status Codes came up in the mailing lists recently — "Re: prevent nginx from translate 303 responses (see other) to 302 (temporary redirect)" — and it was pointed out that putting nginx in front of your backend may by default cause a change of HTTP/1.1 scheme to HTTP/1.0, as per http://nginx.org/r/proxy_http_version, which may cause your non-nginx backend to have different handling of HTTP to comply with the 1.0 spec; solution would be to add
proxy_http_version 1.1
to nginx.
这篇关于使用Nginx时缺少HTTP状态代码名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!