Ubuntu 16.04——配置Nginx及Https服务

前言

之前在网上看过许多为服务器配置Https的教程,其中有很多用apache2配置https的,但大都良莠不齐:教程内容大都是apache而不是apache2的,不是缺这一部分就是缺那一部分,看了以后让人一头雾水。

Nginx作为一个出色的高性能Web和反向代理服务器,其相比Apache2安装非常简单,配置文件也十分简洁,资源占用也更少。

因此我推荐大家使用nginx来配置https。

· 安装及配置Nginx

sudo apt-get install nginx
安装完成后Nginx会自动启动,接着在浏览器输入服务器域名或IP访问,若出现如下页面则表示Nginx安装成功。
Nginx欢迎页面

Nginx常用命令

sudo service nginx start #启动
sudo service nginx stop #停止
sudo service nginx restart #重新启动
sudo service nginx reload #重新加载配置

sudo nginx -t #检查Nginx配置是否正确
sudo nginx -s start #启动
sudo nginx -s stop #停止
sudo nginx -s restart #重新启动
sudo nginx -s reload #重新加载配置

· 申请SSL证书并进行配置

前提

由于服务器方面知识不够充分,一开始我一直以为是为ip:8080(tomcat路径)配置SSL,结果被坑惨了。
小白们一定要注意,当你的项目可以直接通过域名访问配置SSL才会成功!

顺便介绍一下配置Nginx直接通过域名访问的方法:
(由于浏览器访问网站默认访问的是80端口,所以实际上就是将80端口映射至8080端口,
即达到在浏览器地址栏中输入http://localhost:8080 和 输入http://localhost 进入同一页面的效果)

修改/etc/nginx/nginx.conf

sudo nano nginx.conf

在http配置项中添加如下语句:

server {
	listen 80;
	server_name localhost;

	location / {
    proxy_pass http://localhost:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
	}
}

照理说配了此处应该就项目中文件都可以成功访问了,但不知为何我的项目不行,因此还需修改,并在其中加入:

location ~ .*.[jsp|do|action]$ { #所有jsp页面以及do/action请求均交由tomcat处理
	index index.jsp;
	proxy_pass http://localhost:8080; #转向tomcat处理
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { #设定访问静态文件直接读取不经过tomcat
	expires      30d;
	proxy_pass http://localhost:8080; #转向tomcat处理
}
location ~ .*\.(js|css)?$ {
	expires      1h;
}

这样就可以正常访问项目下文件了。

正文

经过百度之后,我了解到免费的SSL证书可通过阿里云、腾讯云或七牛云获取,但由于我项目的服务器域名有些特殊,是二级域名,在以上平台进行域名解析时总是失败,无果只能向Google寻求帮助,最终找到了一个非常好的发放免费SSL证书的平台(几乎可无限期使用)。接下来就介绍一下申请及配置过程。

Let’s Encrypt:一个可以获取免费、自动化、开放的证书的网站
https://letsencrypt.org/

点击Get Started

或直接访问Certbot官网

其中Software为服务器类型,System为服务器上系统类型。
我使用的是Ubuntu 16.04(xenial)和Nginx。

安装Cerbot

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

使用Cerbot中的Nginx插件(自动版,可能会不行)

sudo certbot --nginx

此命令将自动获取证书并且Certbot会编辑Nginx配置并完成所有工作,
只需在命令运行期间根据命令的提示输入你要配置SSL的域名即可。

如果想自定义Nginx配置,则使用certonly子命令即可。

sudo certbot --nginx certonly

以下配置过程比较保险

使用nano修改Nginx的default文件

sudo nano /etc/nginx/sites-available/default


/etc/nginx/sites-available/default

. . .
server_name example.com www.example.com;
. . .


其中example.com换成自己的域名,www那个可加可不加,总之我的加了后崩了。

检测Nginx配置是否正确

sudo nginx -t
无报错则万幸,报错就GG
可通过一下命令查看错误日志信息

重启Nginx

sudo systemctl restart nginx

###获取SSL证书

sudo certbot --nginx -d 自己的域名

若成功则输出信息如下:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.

1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you’re confident your site works on HTTPS. You can undo this
change by editing your web server’s configuration.

Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel):

输入选择后输出信息如下:

IMPORTANT NOTES:

  • Congratulations! Your certificate and chain have been saved at
    /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
    expire on 2017-10-23. To obtain a new or tweaked version of this
    certificate in the future, simply run certbot again with the
    “certonly” option. To non-interactively renew all of your
    certificates, run “certbot renew”

  • Your account credentials have been saved in your Certbot
    configuration directory at /etc/letsencrypt. You should make a
    secure backup of this folder now. This configuration directory will
    also contain certificates and private keys obtained by Certbot so
    making regular backups of this folder is ideal.

  • If you like Certbot, please consider supporting our work by:

    Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
    Donating to EFF: https://eff.org/donate-le

这样SSL证书就配置成功了,在浏览器以https开头试试能否成功访问即可。

自动更新证书期限

Certbot提供的证书期限为90天,执行此命令可自动续订证书。

sudo certbot renew --dry-run

如果没有报错,Certbot就会自动帮你更新证书并重载Nginx;如果更新失败,Let's Encrypt网站会发送一封邮件到你填入的邮箱告知详细情况。

另外再推荐一个很好的有各种服务器相关配置教程的网站:
DigitalOcean
其中也有详细的为Ubuntu 16.04下Nginx配置SSL证书的英文教程:
How To Secure Nginx with Let’s Encrypt on Ubuntu 16.04

· 配置结果

查看证书

大功告成!不枉我从无到有还花了近一星期配置,终于成功了!
感谢爹娘感谢同学感谢祖国,后台的甩的锅我成功接下,背锅侠就是我

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值