Lxn-Chan!

(~ ̄▽ ̄)→))* ̄▽ ̄*)o

我发现网上有关这个的教程都像有那什么大病一样,不是不全就是话都说不明白 ?_?
本文主要内容包括从纯净系统到配置好ssl的全过程,但不包括购买域名和配置服务器等基础操作。

配置

OS: Ubuntu 20.04.2 LTS (GNU/Linux 5.11.0-38-generic x86_64)
Nginx: 1.20.2

安装Nginx

本文中全部操作均在root账户下操作,若在非root账户下操作请自己加上sudo

首先参考Ubuntu 镜像使用帮助更换软件源为清华源。

然后安装必要依赖项:

1
2
apt update
apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

安装GPG Key:

1
2
3
4
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

等待出现573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62(该值一定要完全匹配)后进入下一步。

添加软件源

1
2
3
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

安装Nginx:

1
2
3
4
5
6
7
apt update

# 若软件源添加成功则应在update的输出中看到如下信息
# Get:1 http://nginx.org/packages/ubuntu <version> InRelease [0 B]
# Get:2 http://nginx.org/packages/ubuntu <version>/nginx amd64 Packages [0 B]

apt install nginx

确认是否安装成功:

1
2
3
4
5
6
7
8
nginx -V

# 若输出如下类似内容则安装成功。
nginx version: nginx/1.20.2
built by gcc 11.2.0 (Ubuntu 11.2.0-7ubuntu2)
built with OpenSSL 1.1.1l 24 Aug 2021
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.20.2/debian/debuild-base/nginx-1.20.2=. -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

配置网站

切换到nginx配置文件目录

1
cd /etc/nginx/conf.d

新建配置文件

1
2
# 其中<name>随意起一个文件名即可
vim <name>.conf

配置文件格式/模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   server {
# 监听端口
listen 80;
# 域名可以有多个,用空格隔开
server_name eg.a.com eg.b.com;
#location块
location / {
#网站文件根目录
root /var/www/nginx/egpages;
#默认文档
index index.html index.htm;
}
#错误页
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx/egpages;
}
#拒绝的ip
#deny 127.0.0.1;
#允许的ip
#allow 172.18.5.54;
}

如果到此为止则可跳转至【运行】节。

SSL

修改刚才的配置文件

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
37
38
39
40
41
42
43
44
45
46
47
  server {
# 监听端口
listen 80;
# 域名,可以有多个,用空格隔开
server_name eg.a.com eg.b.com;
# HTTP 自动跳转 HTTPS
rewrite ^(.*)$ https://${server_name}$1 permanent;
#拒绝的ip
#deny 127.0.0.1;
#允许的ip
#allow 172.18.5.54;
}

server {
# 监听端口 HTTPS
listen 443 ssl;
# 域名
server_name eg.a.com;
# 根目录
root /var/www/nginx/eg-a;

# 配置域名证书,要确保路径存在
ssl_certificate /var/www/nginx/certs/eg-a/bundle.crt;
ssl_certificate_key /var/www/nginx/certs/eg-a/MyCert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

index index.html index.htm index.php;

# 图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 10d;
}

# JS和CSS缓存时间设置
location ~ .*\.(js|css)?$ {
expires 1h;
}

#拒绝的ip
#deny 127.0.0.1;
#允许的ip
#allow 172.18.5.54;
}

运行

测试配置文件

1
2
3
4
5
nginx -t

# 没问题的话会输出
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重载

1
nginx -s reload

反向代理

把配置文件里面location段改一下就行

1
2
3
location / {
proxy_pass http://127.0.0.1:1234;
}

GZIP压缩

在nginx.conf里面改

1
2
3
4
5
6
7
8
9
10
11
   # gzip模块设置
# 开启gzip压缩输出
gzip on;
# 允许压缩的页面的最小字节数,页面字节数从header的content-length中获取,默认是0,不管页面多大都进行压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_min_length 1k;
gzip_buffers 4 16k; #表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果
gzip_http_version 1.1; #压缩版本(默认1.1,目前大部分浏览器已经支持gzip解压.前端如果是squid2.5请使用1.0)
gzip_comp_level 5; #压缩等级.1压缩比最小,处理速度快.9压缩比最大,比较消耗cpu资源,处理速度最慢,但是因为压缩比最大,所以包最小,传输速度快
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn.
gzip_vary on;#选项可以让前端的缓存服务器缓存经过gzip压缩的页面.例如:用squid缓存经过nginx压缩的数据

HTTP2

在对应站点的配置文件中,找到listen(监听端口),在后面添加http2即可。

示例:

1
2
3
4
5
6
server {
# 直接将HTTP2加在后面即可
listen 443 ssl http2;
server_name test.lxnchan.cn;
root /root/main;
}

在响应头中指定字符集

启用该功能可加快网页加载速度。

1
2
3
4
server {
# other server config...
charset utf-8;
}

保存后重载nginx即可。

OSCP 装订

具体 OSCP 会有什么优势我也没太整明白,有特别懂的可以在评论区讲一下,谢谢。

配置文件调整

开启SSL后在对应站点配置文件server段中添加如下参数:

1
2
3
4
ssl_stapling on;                                                      # 开启OCSP
resolver 8.8.8.8 8.8.4.4 223.5.5.5 valid=300s; # 不加这个可能就会开启失败
ssl_stapling_verify on; # 开启OCSP验证
ssl_trusted_certificate /var/www/nginx/certs/eg-a/bundle.crt; # SSL证书路径,要求带完整证书链

保存后重启nginx即可。

测试

在另一台设备上执行如下命令检测是否已启用OSCP装订,将<serverName>替换为你的域名:

1
openssl s_client -connect <serverName>:443 -servername <serverName> -status -tlsextdebug < /dev/null 2>&1 | grep -i "OCSP response"

若配置成功则会返回:

1
2
3
4
OCSP response: 
OCSP Response Data:
OCSP Response Status: successful (0x0)
Response Type: Basic OCSP Response

基础认证

首先使用htpasswd生成认证数据库,参数从左至右分别为数据库位置、用户名和密码:

1
htpasswd -bc /home/lxnchan/test.db test 1008611

然后在Nginx的配置文件中指定认证方式和数据库:

1
2
3
4
5
6
location / {
# 这里是提示信息,随便写就行
auth_basic "Auth with your password Please.";
# 这里填写刚才生成的数据库路径
auth_basic_user_file /home/lxnchan/test.db;
}

全部设置好后测试配置文件并重启Nginx即可。

需要注意的是,这种认证方式并不安全
通过观察网络流可以发现,密码是写在Header里面的,并且仅采用Base64编码。
也就是说,如果存在中间人对网络流进行拦截解析就有可能获得用户名和密码,因此强烈建议必须启用SSL并禁用不安全的加密方式。
而对于本地来说默认的认证数据库是采用MD5编码的,即便是其他人得到了该数据库文件也不会解析到密码。

日志自动分割

自编译的 Nginx 可能无法做到日志自动分割,原因是用于日志分割的 logrotate 配置文件并未被写入到源码中。

/etc/logrotate.d目录下新建空白文件,名称随意:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}

logrotated是rysylogd的一部分,一般来说Linux都默认自带,如果没有自己安装一下就可以;保存后重启rsyslogd即可。

改动记录

时间改动
2022-06-15 23:12:00添加“基础认证”节

参考资料

 简单说两句



联系站长 | 服务状态 | 友情链接

备案号:辽ICP备19013963号

萌ICP备 20219421 号

中国互联网违法和不良信息举报中心

架构版本号:8.1.5 | 本站已全面支持IPv6

正在载入运行数据(1/2)请稍后...
正在载入运行数据(2/2)请稍后...

Copyright 2024 LingXuanNing, All rights reserved.