Ad

HTTP Status Code Names Are Missing When Using Nginx

I'm using Nginx to

redirect all HTTP requests to HTTPS

in my spring boot application.This is the nginx configuration that i'm using,with that i was able to redirect all requests to Https but when i do it i get the status code returned correctly but it doesnt have the status code name anymore.if i remove nginx and run spring boot application alone i can get the http status with its code name and 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;
  }

}

what am i doing wrong in here should i use proxy_redirect instead of proxy_pass, or am i missing anything in here.that'd be great if you can help.

Ad

Answer

  • 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 of default, 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 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).

    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.

Ad
source: stackoverflow.com
Ad