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

时间:2018-01-25 07:55:35

标签: java spring-boot nginx load-balancing reverse-proxy

我正在使用Nginx

  

将所有HTTP请求重定向到HTTPS

在我的春季启动应用程序中。这是我正在使用的nginx配置,我可以将所有请求重定向到Https,但当我这样做时,我得到状态代码正确返回,但它不再具有状态代码名称。如果我删除nginx并单独运行spring boot应用程序,我可以使用代码名称和代码获取http状态。 / p>

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 ,或者我在这里缺少任何东西。如果你能做到的话,那就太棒了帮助

2 个答案:

答案 0 :(得分:4)

在同一个配置文件中

听80以将req重定向到https(443)

{
    "data": [
        {
            "message": "'A reader lives a thousand lives before he dies. The man who never reads lives only one.'\n- George RR Martin",
            "created_time": "2018-01-28T12:17:54+0000",
            "id": "113326729482474_113029916178822",
            "full_picture": "https://scontent.xx.fbcdn.net/v/t1.0-9/26993527_113029839512163_7617357733573673432_n.jpg?oh=c617cf94e3cdd62320fde60e445f760e&oe=5B22BC72",
            "status_type": "added_photos",
            "likes": {
                "data": [],
                "summary": {
                    "total_count": 0,
                    "can_like": true,
                    "has_liked": true
                }
            },
            "comments": {
                "data": [
                    {
                        "created_time": "2018-01-28T12:23:04+0000",
                        "from": {
                            "name": "Ragesh D Antony",
                            "id": "1845099915531898"
                        },
                        "message": "Such a wonderful Quote ..",
                        "id": "113029916178822_113036856178128"
                    },
                    {
                        "created_time": "2018-01-28T13:59:22+0000",
                        "from": {
                            "name": "Vignesh Lakshmanen",
                            "id": "1558536637578045"
                        },
                        "message": "Superb",
                        "id": "113029916178822_113251916156622"
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURNMk9EVTJNVGM0TVRJNE9qRTFNVGN4TkRJeE9EUT0ZD",
                        "after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TWpVeE9URTJNVFUyTmpJeU9qRTFNVGN4TkRjNU5qST0ZD"
                    }
                },
                "summary": {
                    "order": "chronological",
                    "total_count": 2,
                    "can_comment": true
                }
            }
        },
        {
            "message": "\"It's an insane world, but in it there's one sanity, the loyalty of old friends. Friends, we must believe in one another.\" \n\n- Ben Hur",
            "created_time": "2018-01-28T12:14:27+0000",
            "id": "113326729482474_113026646179149",
            "status_type": "mobile_status_update",
            "likes": {
                "data": [
                    {
                        "id": "1845099915531898",
                        "name": "Ragesh D Antony"
                    },
                    {
                        "id": "1558536637578045",
                        "name": "Vignesh Lakshmanen"
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "MTg0NTA5OTkxNTUzMTg5OAZDZD",
                        "after": "MTU1ODUzNjYzNzU3ODA0NQZDZD"
                    }
                },
                "summary": {
                    "total_count": 2,
                    "can_like": true,
                    "has_liked": false
                }
            },
            "comments": {
                "data": [
                    {
                        "created_time": "2018-01-28T12:24:40+0000",
                        "from": {
                            "name": "Ragesh D Antony",
                            "id": "1845099915531898"
                        },
                        "message": "I like Ben Hur very much",
                        "id": "113026646179149_113040629511084"
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURRd05qSTVOVEV4TURnME9qRTFNVGN4TkRJeU9EQT0ZD",
                        "after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEV6TURRd05qSTVOVEV4TURnME9qRTFNVGN4TkRJeU9EQT0ZD"
                    }
                },
                "summary": {
                    "order": "chronological",
                    "total_count": 1,
                    "can_comment": true
                }
            }
        }
],
"paging": {
        "previous": "https://graph.facebook.com/v2.11/113326729482474/posts?fields=message,created_time,id,full_picture,status_type,source,comments.summary%28true%29,likes.summary%28true%29&limit=100&format=json&since=1517141874&access_token=EAATh9tg5DMIBANl59BIPExJRzwZAhedH6PSsV2ZAO9FvzFiGqAW3HKafH9b7Bb0gSKnQMJktzUhX2DZCB29BezRUfI2HOumZAYp6FFYwOBJA1ZCdaE4ZCNLXB1ctUZBaKwbp8lXlZBwZBLLCG25CnGAckZClJXZA5omRDFtVvnJFTkJpqigUmy22QNGFmF4CgfC6sWEuVin7mAI9WZCkKkNVzSEeDGDoitt7xeVFiHXCetKOVoLZAl3qyfrYj&__paging_token=enc_AdC03uweQZCRZBN6tZCzhAQ50BZCKzx5koYtAZBRUmgY94TKcCrj6owvzXw9lpoL4iBKJUkW1ZAz4MoZAZBBaGSLZAHTHsKuEst7gtKaQrsQoiZBx8sO2QjQZDZD&__previous=1",
        "next": "https://graph.facebook.com/v2.11/113326729482474/posts?fields=message,created_time,id,full_picture,status_type,source,comments.summary%28true%29,likes.summary%28true%29&limit=100&format=json&access_token=EAATh9tg5DMIBANl59BIPExJRzwZAhedH6PSsV2ZAO9FvzFiGqAW3HKafH9b7Bb0gSKnQMJktzUhX2DZCB29BezRUfI2HOumZAYp6FFYwOBJA1ZCdaE4ZCNLXB1ctUZBaKwbp8lXlZBwZBLLCG25CnGAckZClJXZA5omRDFtVvnJFTkJpqigUmy22QNGFmF4CgfC6sWEuVin7mAI9WZCkKkNVzSEeDGDoitt7xeVFiHXCetKOVoLZAl3qyfrYj&until=1517140875&__paging_token=enc_AdC1r6ZBwBybSmO16DuZCEWGDbdqTeiKCbTsa7h6WDenaZBgB40Xbc0flGcwYqiYPFiEf9wEvuBSoEvbfyl25J0JlbyfhKA1ZBufZC5ZAJMqLMg6bAXgZDZD"
    }
}

听433

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

这就是我这样做的方式,对我来说非常有效,欢呼!

答案 1 :(得分:2)

  • 您拥有的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状态代码 可能会返回状态"名称" 。首先,status code "names", like Moved Temporarily after 302, or Created after 201,对任何事情都不是必不可少的,因此,即使在不太可能发生的情况下,他们也会失踪 - 但为什么他们不能做到这一点并不十分清楚首先缺少nginx,并且您没有提供进一步的详细信息以启用故障排除 - 它不应该真正影响任何其他功能(但是,再次,没有证据证明它是nginx导致它们丢失,事实上,nginx确实在ngx_http_status_lines array of strings within src/http/ngx_http_header_filter_module.c中定义了"201 Created"

    但是,最近在邮件列表中出现了与 HTTP状态代码 相关的问题 - "Re: prevent nginx from translate 303 responses (see other) to 302 (temporary redirect)" - 并指出将nginx放在前面根据{{​​3}},您的后端可能会默认导致HTTP / 1.1方案更改为HTTP / 1.0,这可能导致您的非nginx后端对http://nginx.org/r/proxy_http_version进行不同的处理;解决方案是将proxy_http_version 1.1添加到nginx。