【问题标题】:http to https redirection on nginxnginx 上的 http 到 https 重定向
【发布时间】:2017-03-04 01:20:57
【问题描述】:

我有一个网站在 Amazon ELB 后面的 EC2 机器上运行。
我已经在 ELB 上配置了 SSL,因此它为我处理了 http 和 https。 https 上的所有请求都可以完美运行。但我想强制(重定向)http请求到https。由于某种原因,它不起作用

我在 nginx 中添加了重定向规则,但是每当我启用该规则时,nginx 服务器就会停止响应。

server {
listen 80;
server_name domain1.com;
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;

access_log /var/log/nginx/domain1.access.log;

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass    http://127.0.0.1:4000/;
   ###  Redirect http to https ####
   if ($http_x_forwarded_proto != "https") {
    rewrite ^(.*)$ https://$server_name$1 permanent;
   }
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;";
}
}

负载均衡器的配置如下:

请帮助我在哪里配置错误。 TIA。

【问题讨论】:

  • 你有什么理由不能使用return 301 https://$host$request_uri;

标签: nginx amazon-ec2 amazon-elb


【解决方案1】:

尝试以下方法:

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

【讨论】:

    【解决方案2】:

    我提出这个代码。在我的 VPS 上测试,但不是 Amazon ELB

    server {
    server_name example.com www.example.com;
            listen 80;
            return 301 https://example.com$request_uri;
    }
    server {
    server_name example.com;
            root /home/user/www/example/;
            include global.conf;
            include php.conf;
            include ssl.conf;
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
    }
    server{
    server_name www.example.com;
            include ssl.conf;
            return 301 https://example.com$request_uri;
            ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    }
    

    文件 ssl.conf 包含:

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AES$
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 2017-09-20
      • 2017-05-25
      相关资源
      最近更新 更多