HTTP status code names are missing when using Nginx

末鹿安然 提交于 2019-12-10 19:19:14

问题


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.


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/48438068/http-status-code-names-are-missing-when-using-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!