已安装SSL认证,但HTTPS无法正常工作

时间:2019-06-16 18:55:33

标签: ubuntu ssl nginx ssl-certificate lets-encrypt

我检查了许多博客,但无处可分辨。请帮助

问题: 我有一台ec2机器和我的子域subdomain.website.com的A记录。我的代码在ec2计算机上的端口5000上运行。

我已完成所有步骤以使我的子域https:


sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

sudo certbot --nginx -d sudomain.website.com
#Some QnA

#Finally received msg
Congratulations! You have successfully enabled https://sudomain.website.com
.... /etc/letsencrypt/live/sudomain.website.com/fullchain.pem
.... /etc/letsencrypt/live/sudomain.website.com/privkey.pem


然后我更改了我的nginx conf 我在/etc/nginx/sites-available/webhook.conf

中有一个配置文件

我将文件更新为

server {
    if ($host = sudomain.website.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


 listen 80;
 listen [::]:80;
 server_name sudomain.website.com;

 ssl_certificate /etc/letsencrypt/live/sudomain.website.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/sudomain.website.com/privkey.pem;

 location / {
   proxy_pass http://localhost:5000;
   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;
  }


}

server {
 listen 443 ssl;
 server_name sudomain.website.com;
 listen [::]:443 ssl;
 ssl_certificate /etc/letsencrypt/live/sudomain.website.com/fullchain.pem; # managed by Certbot
 ssl_certificate_key /etc/letsencrypt/live/sudomain.website.com/privkey.pem; # managed by Certbot
}

然后我重新启动了Nginx

sudo nginx -t
sudo service nginx restart

仍然https://sudomain.website.com无效,

如果我评论返回301行,则http://sudomain.website.com工作正常

如果我想念任何东西,请让我知道吗?

注意:例如sudomain.website.com

1 个答案:

答案 0 :(得分:1)

这里有几种配置错误。

  • 主要问题是您在HTTP服务器中拥有到后端(端口5000)的代理,而不是在HTTPS服务器上拥有它。您正在将HTTP通信重定向到HTTPS(使用return 301 https://$host$request_uri;),但HTTPS配置为空。

  • 请记住在sites-enabled内创建指向sites-available的符号链接。

  • 不包括certbot配置(仅包含证书),因此如果使用HTTP验证,可能会在翻新方面遇到问题。

  • 最好使用不同的服务器来处理不同的服务器名称,因此可以删除if ($host ...)

  • 无需将SSL证书放在NON HTTPS服务器上。

配置应如下所示:

server {
 listen 80;
 listen [::]:80;
 server_name sudomain.website.com;

 # Redirect all http traffic to https
 return 301 https://$host$request_uri;

}

server {
 listen 443 ssl;
 server_name sudomain.website.com;
 listen [::]:443 ssl;

 ssl_certificate /etc/letsencrypt/live/sudomain.website.com/fullchain.pem; # managed by Certbot
 ssl_certificate_key /etc/letsencrypt/live/sudomain.website.com/privkey.pem; # managed by Certbot

 location / {
   proxy_pass http://localhost:5000;
   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;
  }

}

我不确定您是否真的需要那些proxy_set_headers

假设不使用HTTP(使用DNS或其他方式)进行Cerbot验证,则进行协议升级和代理到后端的通用,简单方法是:

  • /etc/nginx/sites-available中使用上述内容创建名为sudomain.website.com.conf的文件。
  • 创建从/etc/nginx/sites-available/sudomain.website.com.conf/etc/nginx/sites-available/sudomain.website.com.conf的符号链接以启用该站点。

那将是:

server {
 listen 80;
 server_name sudomain.website.com;

 # Redirect all http traffic to https
 return 301 https://$host$request_uri;
}

server {
 listen 443 ssl;
 server_name sudomain.website.com;

 # Managed by Certbot
 ssl_certificate /etc/letsencrypt/live/sudomain.website.com/fullchain.pem; 
 ssl_certificate_key /etc/letsencrypt/live/sudomain.website.com/privkey.pem;

 location / {
   include proxy_params;
   proxy_pass http://localhost:5000;
  }
}
相关问题