Nginx在转发到其他URL时启用HTTPS / SSL

时间:2014-11-24 14:48:40

标签: ssl amazon-web-services nginx https amazon-ec2

目前,我正在使用AWS Ubuntu EC2实例,在端口3000上运行Node.js应用程序,该应用程序具有Nginx反向代理。我一直在尝试启用HTTPS并添加SSL证书,我已经成功,因为我没有在nginx.conf文件中收到任何错误。但是,我正在重定向我的主网站," example.com"到AWS服务器的公共DNS,当我尝试加载" http://example.com"或" https://example.com"页面,我得到一个"无法连接"来自Firefox的错误,这是我的测试浏览器。此外,当我运行sudo nginx -t时,配置文件中没有语法错误,当我检查/var/log/nginx/error.log文件时它是空的。下面是我当前的nginx.conf文件。

更新:我将server_name从example.com更改为我服务器的公共DNS,我们将其称为amazonaws.com。现在,当我输入https://amazonaws.com时,页面加载,并且通过ssllabs.com运行网站时会显示SSL证书。但是,当我输入amazonaws.comhttp://amazonaws.com时,我会像以前一样获得空白页。

user root;
worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
  # max_clients = worker_processes * worker_connections / 4
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;

  gzip on;
  gzip_comp_level 6;
  gzip_vary on;
  gzip_min_length 1000;
  gzip_proxied any;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_buffers 16 8k;

  # backend applications
  upstream nodes {
    server 127.0.0.1:3000;
    keepalive 64;
  }

  server {
    listen 80;
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/example_com.crt;
    ssl_certificate_key /etc/nginx/ssl/example_com.key;
    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;
    server_name example.com;

    # everything else goes to backend node apps
    location / {
      proxy_pass http://nodes;

      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $host;
      proxy_set_header X-NginX-Proxy true;
      proxy_set_header Connection "";
      proxy_http_version 1.1;
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您应该提供此服务器定义

server {
listen 80;
return 301 https://$host$request_uri;
}

server_name(例如amazonaws.com)。