使用 Nginx 时缺少 HTTP 状态代码名称

HTTP status code names are missing when using Nginx

我正在使用 Nginx

redirect all HTTP requests to HTTPS

在我的 spring 引导中 application.This 是我正在使用的 nginx 配置,通过它我能够将所有请求重定向到 Https 但是当我这样做时我得到 状态代码 正确返回,但它没有 状态代码名称 anymore.if 我删除了 nginx 和 运行 spring 启动应用程序我一个人就可以得到 http 状态及其 code name 和 code.

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,或者我我遗漏了 here.that 中的任何内容,如果你能提供帮助,那就太好了。

在同一个配置文件中

侦听 80 以将请求重定向到 https (443)

server {
    listen 80;
    listen [::]:80;
    server_name your_url.com www.your_url.com; 
    return 301 https://your_url.com$request_uri;
}

收听 433

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

    location / {
            # proxy pass to your app

            proxy_pass http://localhost:7070;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

这就是我的做法,非常适合我,干杯!

  • 你的 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 状态代码 可能会在没有状态的情况下返回 "names"。首先,status code "names", like Moved Temporarily after 302, or Created after 201, 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 the ngx_http_status_lines array of strings within src/http/ngx_http_header_filter_module.c).

    但是,最近在邮件列表中出现了一个关于 HTTP 状态代码 的相关问题 — "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;解决方案是将 proxy_http_version 1.1 添加到 nginx.