avatar

10.Nginx服务一

Nginx服务一

第1章 Nginx介绍

1.1 什么是Nginx?

Nginx 是一个开源且高性能、可靠的 Http Web 服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

1.2 为什么选择 Nginx 服务

Nginx 非常轻量
功能模块少 (源代码仅保留 http 与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择 Nginx

  • 1.Nginx 技术成熟,具备的功能是企业最常使用而且最需要的
  • 2.适合当前主流架构趋势, 微服务、云架构、中间层
  • 3.统一技术栈, 降低维护成本, 降低技术更新成本。

1.3 Nginx重要特性

Nginx 采用 Epool 网络模型, Apache 采用 Select 模型
Select: 当用户发起一次请求, select 模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求, epool 模型会直接进行处理,效率高效,并无连接限制

1.4 Nginx应用场景

Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。

第2章 Nginx安装部署

Nginx分为几种

  • 1.源码编译(1.版本随意 2.安装复杂 3.升级繁琐)
  • 2.epel仓库(1.版本较低 2.安装简单 3.配置不易读)
  • 3.官方仓库(1.版本较新 2.安装简单 3.配置易读,(推荐)

下面分别介绍编译安装和yum安装方法

2.1 编译安装方法

创建www用户

1
2
3
4
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 组=666(www)

安装依赖包

1
[root@web01 ~]# yum install openssl-devel pcre-devel gcc gcc+ -y

下载解压软件包

1
2
3
4
[root@web01 ~]# mkdir /data/soft -p
[root@web01 ~]# cd /data/soft/
[root@web01 /data/soft]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@web01 /data/soft]# tar zxvf nginx-1.16.0.tar.gz

配置编译参数

1
2
3
[root@web01 ~]# cd /data/soft/nginx-1.16.0/
[root@web01 /data/soft/nginx-1.16.0]# ./configure --help
[root@web01 /data/soft/nginx-1.16.0]# ./configure --user=www --group=www --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre

编译安装

1
[root@web01 /data/soft/nginx-1.16.0]# make && make install

创建软链接

1
2
3
4
5
[root@web01 /data/soft/nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx
[root@web01 /opt]# ll
total 0
lrwxrwxrwx 1 root root 12 Jul 30 10:13 nginx -> nginx-1.16.0
drwxr-xr-x 11 root root 151 Jul 30 10:14 nginx-1.16.0

检查语法

1
2
3
[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful

启动nginx

1
[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx

检查测试

1
2
3
[root@web01 /opt/nginx]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12828/nginx: master
[root@web01 /opt/nginx]# curl 10.0.1.7

2.2 YUM安装方法

安装依赖包

1
[root@web01 ~]# yum install openssl-devel pcre-devel -y

配置官方yum源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

安装nginx服务

1
[root@web01 ~]# yum -y install nginx

启动服务并配置开机自启动

1
2
3
4
5
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

测试访问

1
[root@web01 ~]# curl 10.0.1.7

Nginx启动方式说明

编译安装启动管理方式:

1
2
3
4
nginx -t  #检查语法
nginx #启动服务
nginx -s reload #重新加载服务
nginx -s stop #关闭服务

yum安装启动管理方法:

1
2
3
4
5
nginx -t                  #检查语法
systemctl start nginx #启动服务
systemctl reload nginx #重新加载服务
systemctl restart nginx #重启服务
systemctl stop nginx #停止服务

第3章 Nginx重要配置文件说明

3.1 查看配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@web01 ~]# rpm -ql nginx 
...................................................
/etc/logrotate.d/nginx #nginx日志切割的配置文件
/etc/nginx/nginx.conf #nginx主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf #默认展示的页面一样
/etc/nginx/mime.types #媒体类型 (http协议中的文件类型)
/etc/sysconfig/nginx #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service #systemctl 管理nginx(开 关 重启 reload)配置文件
/usr/sbin/nginx #nginx命令
/usr/share/nginx/html #站点目录 网站的根目录
/var/log/nginx #nginx日志 access.log 访问日志
...................................................

3.2 查看已经编译的模块

1
[root@web01 ~]# nginx -V

3.3 配置文件注解

Nginx 主配置文件/etc/nginx/nginx.conf 是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
Nginx 主配置文件整体分为三块进行学习,分别是
CoreModule(核心模块)
EventModule(事件驱动模块)
HttpCoreModule(http 内核模块)

3.3.1 第一部分:配置文件主区域配置

1
2
3
4
5
user  nginx;                    #定义运行nginx进程的用户
worker_processes 1; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)

error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid

3.3.2 第二部分:配置文件事件区域

1
2
3
events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}

3.3.3 第三部分:配置http区域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main; #访问日志保存路径

sendfile on; #开启高效传输模式
#tcp_nopush on;
keepalive_timeout 65; #连接超时时间
#gzip on; #开启压缩
include /etc/nginx/conf.d/*.conf; #包含子配置文件
}

3.3.4 第四部分:子配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@web01 ~]# egrep -v "#|^$" /etc/nginx/conf.d/default.conf     
server {
listen 80; #指定监听端口
server_name localhost; #指定监听的域名
location / {
root /usr/share/nginx/html; #定义站点的目录
index index.html index.htm; #定义首页文件
}
error_page 500 502 503 504 /50x.html; #优雅显示页面信息
location = /50x.html {
root /usr/share/nginx/html;
}
}

http server location 扩展了解项
http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体 URL 路径

第4章 Nginx虚拟主机配置实战

4.1 基于域名的虚拟主机

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
48
49
50
51
52
53
54
55
56
57
58
59
[root@wufei /etc/nginx]# cat nginx.conf

user www; #定义运行nginx进程的用户
worker_processes auto; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)

error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid


events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}


http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main; #访问日志保存路径

sendfile on; #开启高效传输模式
#tcp_nopush on;

keepalive_timeout 65; #连接超时时间

#gzip on; #开启压缩

#include /etc/nginx/conf.d/*.conf; #包含子配置文件
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}

server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.oldboy.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}

}

4.2 基于端口的虚拟主机

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
48
49
50
51
52
53
54
55
56
57
58
59
[root@wufei /etc/nginx]# cat nginx.conf

user www; #定义运行nginx进程的用户
worker_processes auto; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)

error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid


events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}


http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main; #访问日志保存路径

sendfile on; #开启高效传输模式
#tcp_nopush on;

keepalive_timeout 65; #连接超时时间

#gzip on; #开启压缩

#include /etc/nginx/conf.d/*.conf; #包含子配置文件
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}

server {
listen 81;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
server {
listen 82;
server_name bbs.oldboy.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}

}

4.3 基于IP的虚拟主机

添加第二IP:

1
ip addr add 10.0.1.11/24 dev eth0

配置文件:

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
48
49
[root@web01 ~]# cat /etc/nginx/nginx.conf    

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

#include /etc/nginx/conf.d/*.conf;
server {
listen 10.0.1.7:81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 10.0.1.11:82;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}

第5章 Nginx虚拟主机配置优化

所有配置都写入一个配置文件维护起来比较麻烦,如果修改错了,影响整个服务起不来,所以我们应该拆分nginx的配置文件为各个子配置

5.1 Nginx主配置文件

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
[root@wufei /etc/nginx]# cat nginx.conf

user www; #定义运行nginx进程的用户
worker_processes auto; #Nginx运行的work进程数量(建议与CPU数量一致或 auto)

error_log /var/log/nginx/error.log warn; #nginx错误日志
pid /var/run/nginx.pid; #nginx运行pid


events {
worker_connections 1024; #每个 worker 进程支持的最大连接数
}


http {
include /etc/nginx/mime.types; #Nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main; #访问日志保存路径

sendfile on; #开启高效传输模式
#tcp_nopush on;

keepalive_timeout 65; #连接超时时间

#gzip on; #开启压缩

include /etc/nginx/conf.d/*.conf; #包含子配置文件
}

5.2 编辑子配置文件www、blog、bbs

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
[root@wufei /etc/nginx/conf.d]# cat 01-www.conf 
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/www.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@wufei /etc/nginx/conf.d]# cat 02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/blog.log main;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
[root@wufei /etc/nginx/conf.d]# cat 03-bbs.conf
server {
listen 80;
server_name bbs.oldboy.com;
access_log /var/log/nginx/bbs.log main;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}

5.3 创建代码目录及首页

1
2
3
4
[root@web01 /etc/nginx/conf.d]# mkdir /usr/share/nginx/html/{www,blog,bbs}
[root@web01 /etc/nginx/conf.d]# echo "www" > /usr/share/nginx/html/www/index.html
[root@web01 /etc/nginx/conf.d]# echo "blog" > /usr/share/nginx/html/blog/index.html
[root@web01 /etc/nginx/conf.d]# echo "bbs" > /usr/share/nginx/html/bbs/index.html

5.4 检查语法重启服务

1
2
3
4
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl reload nginx

5.5 配置/etc/hosts文件

1
2
[root@web01 ~]# tail /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com bbs.oldboy.com

5.6 访问测试

1
2
3
4
5
6
[root@web01 ~]# curl www.oldboy.com
www
[root@web01 ~]# curl blog.oldboy.com
blog
[root@web01 ~]# curl bbs.oldboy.com
bbs

第6章 Nginx状态模块

nginx状态模块: –with-http_stub_status_module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@wufei /etc/nginx/conf.d]# cat status.conf 
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}
[root@wufei /etc/nginx/conf.d]# tail -1 /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com bbs.oldboy.com status.oldboy.com
[root@wufei /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@wufei /etc/nginx/conf.d]# systemctl reload nginx
[root@wufei /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
25 25 22
Reading: 0 Writing: 1 Waiting: 0

nginx状态信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
Active connections  #当前活动的连接数
accepts #当前连接总数TCP
handled #成功的连接数TCP
requests #总的HTTP请求数

Reading #请求
Writing #响应
Waiting #等待的请求数,开启了keepalive

#注意:一次TCP的连接,可以发起多次的http请求,如下参数可配置验证
keepalive——timeout 0; #类似与关闭长连接
keepalive——timeout 65; #65s没有活动则断开连接

第7章 Nginx日志

日志字段解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$remote_addr # 记录客户端 IP 地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录 ISO8601 标准格式下的本地时间
$request # 记录请求的方法以及请求的 http 协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端 IP 地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。

nginx不停服务进行切割日志:

  1. 编辑切割日志脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@web01 /server/scripts]# cat nginx_log.sh 
    #!/bin/bash

    #01.设置日志文件存放目录
    log_path=/var/log/nginx

    #02.设置日志命令变量
    YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`

    #03.日志命令变量
    nginx_cmd="/usr/sbin/nginx"

    #04.重命令日志文件
    /bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
    /bin/mv ${log_path}/error.log ${log_path}/error.$YESTERDAY.log

    #05.重新打开一个新的日志文件
    ${nginx_cmd} -s reopen
  2. 设置定时任务,每天晚12点切割

    1
    2
    3
    [root@web01 /server/scripts]# crontab -l
    #定时切割nginx日志文件
    0 0 * * * /bin/bash /server/scripts/nginx_log.sh
文章作者: Wu Fei
文章链接: http://linuxwf.com/2020/04/13/10-Nginx%E6%9C%8D%E5%8A%A1%E4%B8%80/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WF's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论