- 环境要求
- Linux服务器:可以是自己电脑的VM,也可以是阿里云或Amazon ECS(建议大家都配备一台自己的云服务器)
- OS:CentOS or Debian 二选一
- 软件:
- Nginx 最新稳定版(1.18),请到官网下载源代码编译安装
- shell环境:zsh、oh-my-zsh、Tmux、Vim(8.2以上版本,源代码编译安装,要求支持multibyte)
- Nginx 至少需要支持以下特性:SSL、image-filter、gzip
- 用户能够在浏览器通过域名访问静态网页资源,不需要公网,用户可通过修改自己系统host域名指向服务器的IP
- 部署node/java服务(最简单的demo即可),用户能够在浏览器通过域名访问服务。
- 域名要支持HTTPS,可以是自签发的证书(使用LetsEncrypt签发的可加分)
- 演示会上通过命令列出以上需要【源代码编译】安装的所有软件的【编译参数】,并能简要介绍若干参数的意义
- 除上述成果外,需要召开成果演示会议,在会议上演示整个任务的操作过程(徒手操作,从0到1,演示熟练的shell操作,快捷键等)
1、获取nginx源代码的两种方式
①可到官网直接下载,接着把下载好的安装包nginx-1.18.0.tar.gz
②利用wget通过链接进行下载获取
wget https://nginx.org/download/nginx-1.18.0.tar.gz ##如果环境中,找不到wget命令,可执行以下命令进行安装 yum install -y wget
2、在/usr/local/src中建个nginx-src,将获取到的源代码放入
mkdir nginx-src && cd nginx-src
3、解压nginx-1.18.0.tar.gz,解压完删除压缩包
tar -zvxf nginx-1.18.0.tar.gz rm -rf nginx-1.18.0.tar.gz
4、切换到解压后的nginx-1.18.0目录,进行配置
cd nginx-1.18.0 #使用./configure进行配置 #--prefix=path,是配置安装路径的,如果不配置,默认/usr/local #--with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module 配置一些需要的模块 ./configure
5、安装出错
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=option.
原因:缺少了一些环境
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
解析:
gcc-c++:nginx是C语言写的,所以编译需要该软件
pcre:nginx编译的时候,需要用pcre正则表达式语法
pcre-devel:进行pcre二次开发所需要的库
zlib:nginx启用压缩功能的时候,会用到该模块
zlib-devel:对zlib进行二次开发会用到的库
openssl:进行https访问的话,需要用到该密码库进行加密解密
openssl-devel:对openssl进行二次开发会用到的库
6、接着重新进行配置,配置完成,进行编译安装
./configure #编译安装 make && make install
7、执行了./configure,所以安装在了/usr/local
cd /usr/local #我们可以看到nginx目录 ls
8、为了我们更方便地查看到nginx目录下的文件,我们可以安装tree命令
yum install -y tree cd nginx #执行tree命令 tree
9、启动nginx
./sbin/nginx
10、查看是否启动成功
方式1:查找进程
ps -aux | grep nginx
方式2:curl命令访问
curl 127.0.0.1:80
方式3:浏览器访问
shell环境:zsh、oh-my-zsh、Tmux、Vim(8.2以上版本,源代码编译安装,要求支持multibyte)
1、安装zsh我们安装前要保证系统中有git,没有的话可以通过yum install git进行安装
#安装zsh yum install zsh #自动安装 0h-my-zsh sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
安装完之后,可以进行插件的配置
①进入配置文件
vi ~/.zshrc
②通过./plugins找到对应位置进行插件的配置
配置插件
plugins=(
git
extract
z
autojump
zsh-autosuggestions
zsh-syntax-highlighting
sublime
git-open
web-search
last-working-dir
wd)
③修改完配置后,执行source ~/.zshrc保存运行
此时又会报错,反馈没有找到所配置的插件
安装插件【安装过程可能会出拉取不下来,多拉取几次即可】
#zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
#zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
#autojump
git clone https://github.com/wting/autojump.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/autojump
#git-open
git clone https://github.com/paulirish/git-open.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-open
2、安装Tmux
安装前需要安装一些支持的组件:
yum install libevent-devel ncurses-devel
安装完,通过源代码开始安装Tmux
sudo yum install tmux
Tmux 使用教程
3、安装Vim8.2以上1、卸载当前vim
yum remove vim
2、卸载完,再通过链接下载VIM8.2版本源代码
#切换到/usr/local/src路径 cd /usr/local/src #新建并切换到vim-src目录 mkdir vim-src && cd vim-src #将vim源代码克隆下来 git clone https://github.com/vim/vim.git
3、进入vim目录,进行配置,编译安装
cd vim #进入src配置环境 ./configure make && make install四、验收成果 1、访问静态页面
用户能够在浏览器通过域名访问静态网页资源,不需要公网,用户可通过修改自己系统host域名指向服务器的IP
①修改c:windowssystem32driversetchost文件
121.37.3.191 g-git.com.cn
②进行访问
2、部署访问服务部署node/java服务(最简单的demo即可),用户能够在浏览器通过域名访问服务。
①修改nginx.conf文件
#修改server去匹配到java服务接口进行访问,进行反向代理
server{
location /{
proxy_pass http://localhost:8080/docker;
}
}
②进行检验并重新加载
#检查nginx.conf文件有没有问题 /usr/local/nginx/sbin/nginx -t #将其进行重新加载 /usr/local/nginx/sbin/nginx -s reload
③新建一个sprinboot项目,写一个简单的接口docker,然后进行打包
mvn clean package
④将打包好的jar上传到系统中
⑤新建dockerfile文件,编写内容
vi Dockerfile #编写其内容 FROM openjdk:8 COPY dockerfile-demo-0.0.1-SNAPSHOT.jar dockerfile-image.jar CMD ["java","-jar","dockerfile-image.jar"]
⑥进行构建镜像并进行运行
docker build -t test-docker-image . docker run -d --name demo -p 8080:8080 test-docker-image
⑦最后进行访问
3、SSL证书配置域名要支持HTTPS,可以是自签发的证书(使用LetsEncrypt签发的可加分)
1、在华为云上可以弄免费的证书
2、证书的申请和签发,完成审核之后
3、将证书下载进行安装
①会得到两个文件:crt和key
②在Nginx的安装目录下创建cert目录,并且将“server.key”和“server.crt”拷贝到nginx的“cert”目录下。
③进行nginx.conf配置
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert/server.crt;
ssl_certificate_key cert/server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root html; #站点目录
index index.html index.htm;
}
}
此时会报错,因为我们那时候.configure没有配参数,导致ssl模块没有开启
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #编译 make #备份原有已安装好的nginx cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak #将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态) /usr/local/nginx/sbin/nginx -s quit cp ./objs/nginx /usr/local/nginx/sbin/ #查看是否开启ssl模块 /usr/local/nginx/sbin/nginx -V #再进行启动 /usr/local/nginx/sbin/nginx
可见可以进行https域名访问
#可用curl进行访问,利用证书上的域名访问 curl https://testplay.top4、Nginx的image-filter、gzip特性
image-filter的作用是实现缩略图:
①得看是否开启image-filter
/usr/local/nginx/sbin/nginx -V
②没有的话,又要开始重新.configure进行配置
总结:一开始一定要配置好需要的参数
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module #http_image_filter_module需要gd-devel支持 yum -y install gd-devel #编译 make #备份原有已安装好的nginx cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak #将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态) /usr/local/nginx/sbin/nginx -s quit cp ./objs/nginx /usr/local/nginx/sbin/ #查看是否开启ssl模块 /usr/local/nginx/sbin/nginx -V
③先准备好图片,接着就是缩略图的配置了
location ~ (.+).(jpg|gif|png|JPG|JPEG|PNG|GIF)@(d+)w_(d+)h_(d+)Q_([rc])$ {
set $w $3; #宽
set $h $4; #高
set $q $5; #图片质量
set $type $6;
set $image_path $1.$2; #真实图片地址
set $cache_path $1_$3w_$4h_$5Q_$6.$2; #临时文件地址
if ($type = 'r') {
set $type 'image-resize';
}
if ($type = 'c') {
set $type 'image-crop';
}
set $image_uri /$type$image_path?w=$w&h=$h&q=$q;
if (-f $cache_path) {
rewrite (.+).(jpg|gif|png|JPG|JPEG|PNG|GIF)@(d+)w_(d+)h_(d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
break;
}
if (!-f $cache_path) {
proxy_pass http://127.0.0.1$image_uri;
break;
}
expires 30d; # 设置图片过期时间30天
}
#图片裁剪
location ~ /image-resize(.+).(jpg|gif|png|JPG|JPEG|PNG|GIF) {
root static;
rewrite /image-resize(.+).(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
image_filter resize $arg_w $arg_h;
image_filter_jpeg_quality $arg_q;
image_filter_buffer 50M;
try_files $1.$2 /img/notfound.jpg;
}
#图片缩放
location ~ /image-crop(.+).(jpg|gif|png|JPG|JPEG|PNG|GIF) {
root static;
rewrite /image-crop(.+).(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
image_filter crop $arg_w $arg_h;
image_filter_jpeg_quality $arg_q;
image_filter_buffer 50M;
try_files $1.$2 /img/notfound.jpg;
}
location ~ .*.(gif|jpg|ico|png|css|svg|js)$ {
root static;
}
④最后启动查看
#再进行启动 /usr/local/nginx/sbin/nginx http://121.37.3.191/xxx.jpg【原图访问】 http://121.37.3.191/xxx.jpg@100w_100h_100Q_r 【缩略图访问】 http://121.37.3.191/xxx.jpg@100w_100h_100Q_c 【剪切图访问】
原图
缩略图
剪切图
gzip的作用是实现网页和图片的压缩
①进行nginx.conf配置,可以进行动静分离,将静态文件分离出来进行单独访问
gzip on;
gzip_min_length 5k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types application/javascript image/jpeg;
gzip_vary on;
server{
listen 80;
server_name localhost;
location /{
proxy_pass http://localhost:8080/docker;
}
location ~ .*.(gif|jpg|ico|png|css|svg|js)$ {
root static;
}
}
②创建静态文件夹,存放图片
#切换路径 cd /usr/local/nginx #创建并切换到当前路径 mkdir static && cd static #将图片文件拖拉进来
③重启访问
图片已经经过压缩,虽然压缩出的效果不大,通常页面和js压缩之后的效果比较好
参数解析:
Gzip on|off #是否开启gzip压缩 Gzip_buffers 4 16k #设置gzip申请内存的大小,作用是按指定大小的倍数申请内存空间。4 16k代表按照原始数据大小以16k为单位的4倍申请内存。 Gzip_comp_level[1-9] #压缩级别, 级别越高,压缩越小,但是会占用CPU资源 Gzip_disable #正则匹配UA 表示什么样的浏览器不进行gzip Gzip_min_length #开始压缩的最小长度(小于多少就不做压缩),可以指定单位,比如 1k Gzip_http_version 1.0|1.1 #表示开始压缩的http协议版本 Gzip_proxied #(nginx 做前端代理时启用该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩) Gzip_type text/pliain,application/xml #对那些类型的文件做压缩 (conf/mime.conf) Gzip_vary on|off #是否传输gzip压缩标识; 启用应答头"Vary: Accept-Encoding";给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
注意事项:
1.图片、mp3这样的二进制文件,没必要做压缩处理,因为这类文件压缩比很小,压缩过程会耗费CPU资源
2.太小的文件没必要压缩,因为压缩以后会增加一些头信息,反而导致文件变大
3.Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,我们需要手动来配置
1、安装
yum install docker
2、需要修改其配置daemon.json
cd /etc/docker
vi daemon.json
#加入以下代码
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
3、重启docker
systemctl restart docker.service



