4

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.

Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • 1
    What exactly do you mean when you say that "it doesnt have the status code name anymore"? – cnst Jan 28 '18 at 20:05
  • im getting `201` but i want it to be `201 created` – Priyamal Jan 29 '18 at 04:21
  • 1
    Why exactly do you care? The status code names don't really mean anything to any machine. – cnst Jan 30 '18 at 20:59
  • Please could you clarify "it doesnt have the status code name anymore"? Are you talking about the response from nginx to HTTPS proxied requests? How are you reading the status line? Could you include a request + response log and explain the problem by contrasting expected and observed measurements? – Rich Feb 02 '18 at 10:48

2 Answers2

4

In the same config file

listen on 80 to redirect req to https (443)

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

listen on 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;
    }

This is the way I do it, and works perfectly for me, cheers!

2
  • 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.

cnst
  • 25,870
  • 6
  • 90
  • 122