Ghost Cat's Blog

  • 首页

  • 归档

  • 标签

  • 分类

  • 关于

SpringBoot + Nginx 配置HTTPS的一次经历

发表于 2018-08-03 | 分类于 Blogs

HTTPS 安全加密传输势在必行。

最近公司开发了一款小程序的应用,但是小程序为了保证数据安全,强制要求使用HTTPS,然后就不得不去配置了一下。

之前在php开发的项目上配置过一次,使用的是wdcp的控制台程序,配置起来很简单,不需要自己修改参数,按照教程将证书文件放进去就OK了。

这次是Java项目,而且用了SpringBoot,他内部集成了tomcat,没找到怎么修改tomcat参数的方法,而且看了SpringBoot配置起来好像也不是很方便。

因此就折中选择了Nginx做服务器,转发到Java项目,经过以前的几个项目,对Nginx 的基本用法和配置还是了然于胸的。

第一步,安装Nginx

服务器使用的是CentOS 7的版本,因此,直接选择了Nginx 的源码安装,在Nginx官网下载了最新的安装包,开始按照教程进行安装。

1
2
3
4
5
6
7
8
9
10
 #下载后进行解压
tar -xvzf nginx-1.12.2.tar.gz
cd nginx-1.12.2
#执行以下命令 这一步很重要
./configure --with-http_ssl_moudle
#如果出现关于OpenSSL的错误,请安装openssl ,然后重复之前的操作
yum -y install openssl openssl-devel
#进行make 安装
make
make install

安装完成后可以启动Nginx进行测试,如果出现Nginx 的欢迎页面,则安装成功。

申请SSL证书

HTTPS需要SSL证书,SSL证书的申请可以在阿里云申请免费的证书。

配置SpringBoot

在SpringBoot 的application.properties文件中进行配置:

1
2
3
4
5
6
7
 #如果使用IDEA进行开发的话,这些参数都有提示
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true
server.port=8080
#端口号不在使用80,可以改为其他的端口

配置Nginx

将申请的证书放在Nginx的安装目录下,我放在了 /usr/local/nginx/conf/cert/ 下,有两个文件,文件后缀分别为 .key和.pem

接下来在nginx.conf进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
server {
listen 80;
server_name xx.example.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;
#其实应该配置到这里就可以了,将80端口的访问转发到443端口
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://xx.example.com:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;

}
}

server {
listen 443;
server_name xx.example.com;
ssl on;
#配置证书的路径
ssl_certificate cert/1534768019908.pem;
ssl_certificate_key cert/1534768019908.key;
#ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
#配置转发到8080端口
proxy_pass http://xx.example.com:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}

上面配置完成后就可以启动Nginx 了,如果没有错误的话, 现在应该是可以进行访问了。

虽然实际步骤并不多,但是对于第一次来说,也走了很多弯路,比如安装Nginx 的时候没有安装http_ssl_moudel,然后配置完了无法启动,nginx报错提示:

1
nginx:[emerg] unknown directive "ssl"

配置80端口没有转发到443,仍旧以http访问。
总之,配置的时候会出现很多意想不到的问题,还是需要耐心,并且遇到问题不要慌,仔细查看错误提示,然后上网查询,基本你遇到的问题,网上都已经有了各种各样的解答,那就需要从中筛选自己需要的解决方案。
祝所有人都有一次配置成功。

文章首发于我的公众号 欢迎关注
https://mp.weixin.qq.com/s/vF-pe8n2fho1hyArO0WYog

微信公众号

LeetCode-刷题

发表于 2018-07-26 | 分类于 LeetCode

LeetCode

一向不是很喜欢题海战术,但这又是一个提升自我能力最有效最简单最暴力的方法。

前几日心血来潮在地址栏输入了leetcode的地址,不出意外的没打开,哈哈哈 因为要科学上网,没架梯子,出不去。但梯子还是常备的,架上梯子,成功逃生。
惊喜的发现竟然有了中文站,虽然这个中文站的名字实在不敢恭维—-领扣中国。为了时间,忍了,入了中文站。

不出所料,所有中文站都没有免费的午餐(都是一个尿性),必须注册并且还要验证手机号邮箱。

刷第一题

Hexo Theme

发表于 2018-07-25 | 分类于 Hexo

Hexo博客系统有很多的主题可供选择,跟随大潮,我同样选择了NexT主题

博客配置

Hexo博客的配置文件为_config.yml,是使用YAML语言的配置文件,不懂得可自行搜索。
这中语言的基本特点如下:

  1. YAML大小写敏感;
  2. 使用缩进代表层级关系;
  3. 缩进只能使用空格,不能使用TAB,不要求空格个数,只需要相同层级左对齐(一般2个或4个空格)

博客站点相关信息配置:

在博客根目录下找到_config.yml文件,打开,并找到Site 关键字,其下的配置为站点基本信息的配置,如下所示:

1
2
3
4
5
6
7
8
# Site
title: Ghost Cat's Blog
subtitle:
description: Ghost Cat.
keywords: Ghost Cat
author: PardonHan
language: zh-CN
timezone:

关于主题

在Hexo的官网中提供了很多收录的主题模板https://hexo.io/themes/,我选择了非常流行的NexT主题主题作为我的博客的主题模板。

主题的设置同样在根目录的 _config.yml 文件中:

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: hexo-theme-next

菜单设置

Hexo默认只有首页和归档两个菜单,创建多个菜单的教程如下。

在下载好的./themes/hexo-theme-next/主题目录中找到 _config.yml 文件(对,你没看错,就是这个文件),搜索关键字 Menu Settings ,将需要的目录取消注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ---------------------------------------------------------------
# Menu Settings
# ---------------------------------------------------------------

# When running the site in a subdirectory (e.g. domain.tld/blog), remove the leading slash from link value (/archives -> archives).
# Usage: `Key: /link/ || icon`
# Key is the name of menu item. If translate for this menu will find in languages - this translate will be loaded; if not - Key name will be used. Key is case-senstive.
# Value before `||` delimeter is the target link.
# Value after `||` delimeter is the name of FontAwesome icon. If icon (with or without delimeter) is not specified, question icon will be loaded.
menu:
home: / || home
archives: /archives/ || archive
tags: /tags/ || tags
categories: /categories/ || th
about: /about/ || user
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
#commonweal: /404/ || heartbeat

# Enable/Disable menu icons / item badges.
menu_settings:
icons: true
badges: false

使用命令 创建对应的目录,会创建 ./source/***/index.md 文件。

1
hexo new page [tags/categories/about]

将index.md文件中的文字进行修改,例如 tags/index.md

1
2
3
4
5
6
---
title: 标签
date: 2018-07-25 09:53:49
type: "tags"
comments: false
---

在创建的文章中添加对应的标签,文章发布时会对应生成。

1
2
3
4
5
6
---
title: Why is Hexo
date: 2018-07-25 09:17:56
tags: "Blogs"
categories: "Blogs"
---

Why is Hexo

发表于 2018-07-25 | 分类于 Blogs

时隔两年,我又一次的创建了一个Hexo 的Blog个人站点,最近将手头的几个项目问题处理了一下,开始有时间整理近段时间以来自己的积累,以前在GitHub上创建了一个Hexo的站点了,但是年久失修,已不堪使用,维修成本过高(主要是源文件找不到了,不知道怎么下手,索性创建一个新的站点)。

Why is Hexo?

之间断断续续的捣鼓过WordPress,Jekyll,一些其他语言的博客站点,发现以我匮乏的知识不足以支撑他们的运行,故而一再放弃。
以前的站点使用Hexo搭建也是看到很多人使用,然后出了许许多多的中文教程(其实英文官网的教程现在已经很详细–Hexo)。主要还是以前自己英文很差,现在还勉强能够理解作者所讲解的步骤。

1
Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other languages) and Hexo generates static files with a beautiful theme in seconds.

上面这句话是Hexo官网上面对Why is Hexo?的一个解释,这句话应该不用翻译也能看得懂(谁会说自己翻译的不好)。

Hello World

发表于 2018-07-25 | 分类于 Blogs

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

PardonHan

Ghost Cat.

5 日志
3 分类
6 标签
© 2018 PardonHan
由 Hexo 强力驱动 v3.7.1
|
主题 — NexT.Mist v6.3.0 鲁ICP备17025815号-1