【问题标题】:Elastic beanstalk nginx redirect setting not working弹性beantalk nginx重定向设置不起作用
【发布时间】:2017-08-21 18:17:37
【问题描述】:

我正在尝试将 http 重定向到 https,并且我有这个配置:

files:
    "/etc/nginx/conf.d/robots.conf":
        mode: "000544"
        owner: root
        group: root
        content: |
            server {
                listen 80;

                server_name example.com;

                location =/health {
                    return 200 "health-check";
                }

                location / {
                    if ($http_x_forwarded_proto != "https") {
                        return 301 https://$host$request_uri;
                    }
                }
            }
            server {
                listen 80 default_server;

                server_name www.example.com;

                location =/health {
                    return 200 "health-check";
                }

                location / {
                    if ($http_x_forwarded_proto != "https") {
                        return 301 https://$host$request_uri;
                    }
                }
            }

但是当我转到http://example.comhttp://www.example.com 时,没有重定向... 我尝试了多种设置,但似乎始终无法完全发挥作用。

【问题讨论】:

    标签: amazon-web-services nginx amazon-elastic-beanstalk


    【解决方案1】:

    最好的方法是设置你的配置如下

    server {
           listen         80;
           server_name    my.example.com;
           return         301 https://$server_name$request_uri;
    }
    
    server {
           listen         443 ssl;
           server_name    my.example.com;
           # add Strict-Transport-Security to prevent man in the middle attacks
           add_header Strict-Transport-Security "max-age=31536000"; 
    
           [....]
    }
    

    【讨论】:

    • 您是否使用service nginx reload 重新加载了配置?
    • 不,我只是使用eb deploy 提交和部署,我将service nginx reload 放在哪里?还是你的意思是我应该 ssh 进入实例并这样做?
    猜你喜欢
    • 1970-01-01
    • 2019-06-12
    • 2020-12-31
    • 2018-06-12
    • 1970-01-01
    • 2015-12-28
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多