nginx 入门到实践

nginx1.21.6 测试与使用,nginx 下载与安装。nginx 作为高性能 web 服务器配置详解,比较重要的一个参数 epoll。nginx 静态资源服务器,nginx 配置反向代理,nginx 配置负载均衡。测试 nginx 反向代理到 Apache(httpd) 和 Tomcat。

正文

初次进入 nginx 官网,映入眼帘。给人的整体感觉就是简洁干净,一眼就能 get 到自己需要的资源。

nginx 官网:http://nginx.org

nginx 官方下载地址:http://nginx.org/en/download.html

在接下来的某些测试中,你可能看到使用的是 root 用户或者是 nginx 普通用户。使用 root 用户是为了方便演示,实际工作中一般你用的更多的是普通用户,一般只有管理员才有使用 root 用户的权限。

一、nginx 快速安装

注意:nginx 默认安装设置的 sever 是 localhost,监听端口是 80。

每一个代码块中都有详细注解进行解释,参考官方文档然后进行的翻译,并根据实际情况进行优化调整。

Windows 版本的 nginx 官方文档地址:http://nginx.org/en/docs/windows.html

1、Windows 下安装 nginx

1.1、解压安装

1
2
3
4
5
6
#以管理员身份运行CMD窗口,进入D盘
d:
#新建work目录
mkdir work
#切换至work目录解压nginx
unzip nginx-1.21.6.zip

1.2、启动 nginx

1
2
3
4
#进入nginx目录
cd work\nginx-1.21.6
#启动nginx服务
start nginx

1.3、查看 nginx 服务

nginx.conf 文件中的参数:worker_processes 设置参数值为 1,限制只能运行一个工作进程。

1
2
3
4
5
6
7
#查看nginx资源占用相关信息
d:\work\nginx-1.21.6>tasklist /fi "imagename eq nginx.exe"

映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
nginx.exe 4108 Console 6 11,232 K
nginx.exe 4144 Console 6 11,552 K

1.4、配置日志以及默认首页

在 nginx 解压目录中 nginx-1.21.6\conf 目录下打开 nginx.conf 配置文件,可以根据需求进行配置。此步骤下不做详细讲解,在接下来的配置说明进行详细讲解。

1
2
3
#配置日志与根目录首页
access_log logs/site.log;
root D:/web/html;

1.5、Windows 下 nginx 命令管理

nginx 在 Windows 中的一些常用使用命令,在 Linux 中一样可以使用 stop、quit、reload 命令管理 nginx 服务。

  • nginx -s stop:快速关闭服务;
  • nginx -s quit:优雅的关闭服务;
  • nginx -s reload:改变配置,启动一个新的工作进程配置,优雅地关闭旧的工作进程;
  • nginx -s reopen:重新打开日志文件;
  • nginx -t -c nginx.conf :检测 nginx 语法配置。
1
2
3
4
5
6
7
nginx -s stop   #快速关闭服务
nginx -s quit #优雅的关闭服务
nginx -s reload #改变配置,启动一个新的工作进程配置,优雅地关闭旧的工作进程
nginx -s reopen #重新打开日志文件
nginx -t -c conf\nginx.conf #检测nginx语法配置
nginx: the configuration file D:\work\nginx-1.21.6/conf\nginx.conf syntax is ok
nginx: configuration file D:\work\nginx-1.21.6/conf\nginx.conf test is successful

2、Linux 下安装 nginx

仅供参考,应该以实际工作场景为准,关于 Linux 中权限知识可以自己进行测试。

支持的 Linux 发行版也很丰富,同样可以参考官方文档,下载 Debian 系列,Ubuntu 系列,Redhat 系列以及 Centos 系列等等。部分制作成了表格形式,便于参考。

nginx 支持操作系统(列出部分) 版本 支持平台
RHEL/CentOS 7.4+ x86_64, ppc64le, aarch64/arm64
RHEL/CentOS 8.x x86_64, aarch64/arm64, s390x
Debian 10.x “buster” x86_64, i386, aarch64/arm64
Debian 11.x “bullseye” x86_64, aarch64/arm64
Ubuntu 18.04 “bionic” x86_64, aarch64/arm64
Ubuntu 18.04 “bionic” x86_64, aarch64/arm64, s390x
Ubuntu 21.10 “impish” x86_64, aarch64/arm64

Linux 下安装 nginx,官网给出了便捷方式 yum 源、apt 源等等:http://nginx.org/en/linux_packages.html

个人演示使用下载的源码包。Redhat7 系列以及 Centos7 系列安装 nginx-1.21.6,目前最新版本。可以使用 nginx 官方提供的 yum 源进行安装,或者使用 wget 命令进行下载安装。个人给出使用 tar 包(源码包)安装方式,下载到本机然后上传至虚拟机搭建的 Linux 环境 Centos7.5 服务器上。

2.1、安装依赖环境

安装需要的依赖环境,记住普通用户需要使用 sudo 提权,root 用户则不需要。

1
2
[root@localhost ~]# yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel zlib-devel yum-utils
[nginx@localhost ~]$ sudo yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel zlib-devel yum-utils

2.2、解压安装 nginx

配置可以参考 nginx 的官方文档,很详细。

http://nginx.org/en/docs/configure.html

解压 tar 包,编译指定路径。不指定安装路径,默认安装到 /usr/local/nginx,源码包默认安装位置。配置–prefix 参数,指定安装路径以及需要的模块 (module),使用 make && make install 命令编译并安装。

1
2
3
4
5
6
7
8
#01、解压tar包
tar -zxvf nginx-1.21.6.tar.gz
#02、编译指定路径,不指定一样默认安装到/usr/local,源码包默认安装位置
cd nginx-1.21.6/
#03、配置,--prefix指定安装路径以及需要的模块(module)
./configure --prefix=/usr/local/nginx-t --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
#04、编译并安装
make && make install

查看 nginx 的版本

  • 参数 - v:nginx -v 命令查看 nginx 中间件的版本;
  • 参数 - V:nginx -V 命令查看 nginx 版本以及系统使用 GCC 版本、OpenSSL 版本和配置的–prefix 参数。
1
2
3
4
5
6
7
8
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.21.6
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module

2.3、管理 nginx 服务

安装 nginx 后的目录 /usr/local/nginx/,使用 ls 以及 ll 命令查看安装后的文件。然后以绝对路径方式启动 nginx 服务: sudo /usr/local/nginx/sbin/nginx,在测试环境 root 用户下无需加 sudo 提权。新建用户,使用 root 用户身份权限新建。改变 nginx 安装目录所有者和所属组,赋予给 nginx 用户,此时登录 nginx 用户也可进行管理。

如何区分你使用的是超级管理用户 root 还是普通用户。

  • #:带有 #前缀符号则是超级管理员用户;
  • $:带有 $ 前缀符号则是普通用户。
1
2
3
4
5
6
7
8
9
10
11
#新建用户,使用root用户身份权限新建
[root@localhost ~]# useradd nginx #新建nginx用户
[root@localhost ~]# passwd nginx #修改密码
#仅供参考,应该以实际应用场景为准。
[root@localhost ~]# chown -R root:nginx /usr/local/nginx/
[root@localhost nginx]# ls /usr/local/nginx/
client_body_temp conf fastcgi_temp html logs
proxy_temp sbin scgi_temp uwsgi_temp
#安装nginx后的目录
[root@localhost ~]# ls /usr/local/nginx/
conf html logs sbin

授予普通用户 nginx 管理的权限。使用 visudo 或者 vim /etc/sudoers,在文件末尾加上 nginx ALL=(ALL) /usr/local/nginx/sbin/nginx。作用是给 nginx 用户使用 nginx 脚本命令的权限(使用 sudo)。给用户的权限范围越精确,用户权限则越小。在你赋予权限的时候,理应思考是否合理。开个小玩笑,一不小心将服务器拱手让人了,哈哈。我经常在说的一句话,你能够将 Linux 的权限玩的明明白白,就已经领先很大一部分人。

1
2
3
# visudo
# vim /etc/sudoers
nginx ALL=(ALL) /usr/local/nginx/sbin/nginx

启动 nginx 服务。root 身份则无需提权,以绝对路径形式启动服务。普通用户,则需要使用 sudo 权限提权管理 nginx 服务。sudo 的用法,可以使用 man 帮助命令查看。简单的提一下,使用命令 visudo 添加普通用户,或者编辑 /etc/sudoers 文件加入普通用户可执行的命令。

1
2
3
4
#root身份启动nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx
#普通用户身份启动nginx服务
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx

优雅的关闭 nginx 服务,实际上找的是 nginx.pid 文件中存储的 pid 号。可以通过 cat 查看 /usr/local/nginx/logs/nginx.pid。在 nginx.conf 配置文件中去找到相应的设置,将 #注释去掉:

1
2
pid        logs/nginx.pid;
[root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid

优雅的关闭 nginx 服务,nginx -s quit

1
2
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s quit
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -s quit

重载 nginx 服务,nginx -s reload

1
2
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -s reload

2.4、查看 nginx 进程

可以使用 ps 命令配合 grep 搜素命令查看 nginx 服务进程状态,然后查看启动后的 nginx 目录多出了 client_body_tempfastcgi_tempproxy_tempscgi_tempuwsgi_temp 模块。

1
2
3
4
5
6
[nginx@localhost ~]$ ps -aux | grep nginx
root 7355 0.0 0.0 45992 1136 ? Ss nginx: master process /usr/local/nginx/sbin/nginx
nobody 7356 0.0 0.1 48528 1988 ? S nginx: worker process
root 7368 0.0 0.0 112720 972 pts/1 S+ grep --color=auto nginx
[nginx@localhost ~]$ ls /usr/local/nginx/
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp

2.5、验证 nginx 服务

使用 netstat 命令查看监听到的 nginx 服务,默认使用的是 80 端口,一般 80 是不对外开放的。为了演示,使用 firewalld 命令开启 80 端口,然后使用 firewall-cmd --reload 命令重载防火墙。

1
2
3
4
5
6
7
8
[root@localhost ~]# netstat -tlunp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7355/nginx: master
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=8081/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success

使用 Chrome 浏览器登录 nginx,比如我个人使用虚拟环境搭建的,访问即可看到 nginx 服务启动完毕。即可看到,欢迎访问 nginx 服务。这个 index.html 页面默认存放在 nginx 的安装目录中 html 目录下。

1
http://192.168.245.147/

访问出现错误页面则为 50x.html 静态页面的内容,直接在 url 后拼接访问 50x.html 文件即可进行测试

1
http://192.168.245.147/50x.html

至此,在 Windows 与 Linux 服务器上安装 nginx 服务以及服务的管理介绍完毕。在 Linux 版本中,我介绍的是比较详细的,这也是为了照顾初学者。

二、nginx 做静态资源 web 服务器

1、nginx 中常见的错误码

http 消息 状态码 含义
已移动 http 301 请求的数据具有新的位置,并且永久更改。
已找到 http 302 请求的数据临时具有不同 URI。
请参阅其它 http 303 可在另一 URI 下找到对请求的响应,并且使用 get 请求检索。
未修改 http 304 未按照预期修改文档。
使用代理 http 305 必须通过位置字段中提供的代理来访问请求的资源。
未使用 http 306 不再使用,但保留此代码以便将来使用。
无法找到网页 http 400 可以连接到 web 服务器,但由于 web 地址(URL)的问题无法找到网页。
网站拒绝显示此网页 http 403 可以连接到网站,但 Internet Explorer 没有访问网页文件的权限。
无法找到网页 http 404 可以连接到网站,但找不到网页。可能是网页暂不可用或者已被删除。
网站无法显示此网页 http 405 可以连接到网站,但网页内容无法下载到用户的计算机。可能是网页编码格式问题。
无法读取此网页格式 http 406 能从网站接收信息,但 Internet Explorer 无法识别格式,不能正确地显示消息。
网站忙,无法显示此网页 http 408 或 409 服务器显示网页时间过长,或对同一网页请求过多。
网页不复存在 http 410 可以连接到网站,但找不到网页。此错误为永久性的,而且由网站管理员打开。
网站无法显示该页面 http 500 正在访问的网站出现服务器问题,阻止此网页显示。正在维护或者交互程序出错。
未执行 http 501 没有将正在访问的网站设置为显示浏览器所请求的内容。
不支持的版本 http 505 该网站不支持浏览器用于请求网页的 http 协议。

2、hexo+nginx 静态资源服务器

2.1、hexo 的使用

Windows 下首先安装 node 环境,然后使用 npm 再安装 hexo 模块。这里只介绍 Windows 下安装 hexo 环境:

  • 01、安装 node 环境;
  • 02、在 node 环境下安装 hexo,打开 cmd 命令窗口执行:npm install -g hexo-cli
  • 03、继续在 cmd 窗口命令安装:npm install hexo

具体其它平台安装 hexo 可以参考官网中文文档:https://hexo.io/zh-cn/docs/

在 node 环境下安装 hexo 后生成的 blog 文件目录:

使用 hexo 命令,hexo new 命令生成文件,hexo server 命令启动服务,通过 http://localhost:4000 访问 hexo。

1
2
3
D:\work\createSpace\hexo\blog>hexo new "你要生成的md文件名"
hexo generate #生成静态文件
hexo server #启动服务

2.2、在 nginx 中访问

将 hexo 生成的静态 html 文件上传到虚拟环境中 nginx 服务器的 html 目录下,默认的设置的根目录和和首页配置不变。在虚拟机搭建的 nginx 静态资源服务器,并且使用了反向代理,代理了默认的 80 端口开启的 nginx 服务:

1
http://192.168.245.147:8081/archives/2022/02/

如果想看具体效果可以访问我在 github 上搭建的测试环境。

你可以使用 hexo、jekyI 以及 hugo 去生成静态网页,然后部署到 nginx 服务器上。如果买了云服务器,可以利用起来。再入手一个域名,申请蓝色的幕布,然后进行备案使用 https 解析,nginx 同样也是支持 ssl(解析 https 协议)的。

我同时开启了两个 nginx 服务,使用其中一个反向代理另一个 nginx 服务。配置文件如下设置,反向代理使用到关键字为 proxy_pass

1
2
3
4
5
6
7
8
9
10
11
12
13
#在http模块中配置
http{
upstream test {
server 192.168.245.147;
}
server {
location / {
proxy_pass http://test;
root html;
index index.html index.htm;
}
}
}

查看 nginx 进程,发现有两个不同路径的进程,分别是 nginx 文件和 nginx-t 文件:

1
2
3
4
5
6
7
[nginx@localhost nginx-1.21.6]$ ps -aux | grep nginx
root 15241 0.0 0.0 46004 1132 ? Ss nginx: master process
/usr/local/nginx-t/sbin/nginx
nobody 15242 0.0 0.1 48528 2488 ? S nginx: worker process
root 15274 0.0 0.0 45992 1136 ? Ss nginx: master process /usr/local/nginx/sbin/nginx
nobody 15275 0.0 0.1 48532 2240 ? S nginx: worker process
root 15302 0.0 0.0 112724 968 pts/2 S+ grep --color=auto nginx

进行测试演示。监听的端口,RHEL7 系列使用 firewall-cmd 命令启用了 80 和 8081 端口。

1
2
3
[nginx@localhost nginx-1.21.6]$ netstat -tlunp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15274/nginx: master
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 15241/nginx: master

三、nginx 代理服务

谈到代理,能联想到的有生活中的代理商,还有平时想翻山越海其实也是利用代理服务。国内某大厂原创 game 虽然火不久,但代理出了名,估计大家也猜出来了。在我们的 nginx 中间件中一样可以实现正向代理和反向代理,反向代理恰恰是 nginx 服务的重要功能之一。通过图形化可以更直观的理解代理。

正向代理

nginx 服务配置正向代理的 3 个指令:

  • resolver:用于指定 DNS 服务器的 IP 地址。
  • resolver_timeout:用于设置 DNS 服务器域名解析超时时间。
  • proxy_pass:用于设置代理协议,同时也是配置反向代理的指令。

图 1-2反向代理

  • proxy_pass:配置反向代理的主要参数,注意指明传输协议。
  • proxy_hide_header:用于隐藏一些头域信息。
  • proxy_pass_header:用于处理发送响应报文时接收一些 date、server、x-accel 头域信息。
  • proxy_set_header:用于更改 nginx 服务器接收到客户端请求的请求头信息。

关于反向代理指令就介绍这几个,更多的可以参考 ngx_http_proxy_module,nginx 官网 proxy 模块。我也列出我在工作中实际应用到配置:

1
2
3
4
5
6
proxy_pass_header User-Agent;
proxy_set_header Host $http_host;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;

代理区别:形式上服务的对象不一样。

  • 正向代理代理的对象是客户端,为客户端服务
  • 反向代理代理的对象是服务端,为服务端服务

为了演示进行测试,开启了 nginx 服务、httpd 服务以及 Tomcat 服务。

在使用 nginx 代理服时,看看 nginx 支持哪些代理协议。

主要演示工作中一些场景

  • nginx 代理到 nginx;
  • nginx 代理到 Apache;
  • nginx 代理到 Tomcat;

1、nginx 目录结构简介

nginx 目录作用

  • conf 目录:主要存放 nginx 的配置文件,主要的控制文件。
  • html 目录:存放静态资源目录。
  • logs 目录:存放 nginx 生成的日志文件(包含错误日志)以及 nginx.pid 文件存放 nginx 分进程 pid 号。
  • sbin 目录:nginx 服务脚本,需要使用 root 管理员身份管理服务,或者使用 sudo 提权。
1
2
[nginx@localhost ~]$ ls /usr/local/nginx
conf html logs sbin

2、ngin.conf 文件的介绍

初学者在配置 nginx.conf 文件中的参数时,往往会遇到语法错误,可以使用 nginx 提供命令进行检测语法配置

1
2
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -t
[nginx@localhost ~]$ sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

语法配置正确,则会有以下提示:

1
2
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2.1、用户与进程配置区

  • user 参数:用来配置用户以及用户组,如果配置为 nobody 代表不限制用户。
  • worker_processes:worker_processes 参数配置工作进程。可以根据 CPU 核心数配置,比如 4 核配置 4 个工作进程,提高并发。
1
2
#user  nobody;
worker_processes 1;

2.2、日志与 pid 配置区

  • error_log:配置错误日志;
  • pid:配置 nginx 存储的 pid 号,临时的,服务关闭就消失了。可以根据 pid 号去杀死进程。
1
2
3
4
5
6
#配置日志区
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid存储位置,可以根据pid号去杀死进程
pid logs/nginx.pid;

2.3、events 配置区

events 事件配置区,配置全局的。worker_connections 参数默认配置的 1024,可以根据系统去优化设置最大的工作连接数。

1
2
3
4
#配置连接数
events {
worker_connections 1024;
}

2.4、http 模块

  • 包含 server 模块,可以配置多个。
  • 包含 location 模块,同样可以配置多个。
  • 负载均衡 upstream 同样配置在 http 模块中,server 模块之外。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#配置负载均衡
upstream tomcat {
#可以是域名,或者是ip加端口
server www.example.com;
server 192.168.245.147:8888;
}
}

2.5、server 模块

  • listen:配置 nginx 服务监听端口,默认为 80 端口,可以根据实际需求更改;
  • server_name:配置服务名,可以是 IP 地址也可以是域名;
  • charset:配置字符集;
  • access_log:访问服务接收的日志所在主要目录;
  • location:包含 location 设置,主要有主页以及代理请求头等等参数配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 8088;
#listen 8443 ssl;
#server_name 192.168.0.233;
server_name 127.0.0.1;
#charset koi8-r;
access_log logs/host.access.log main;
#读取根目录
location / {
proxy_pass http://test;
#设置读取的目录
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

2.6、location 配置区,初次安装默认只有 root 配置根目录和 index 配置首页。proxy_pass 是配置代理,我后面加的。

1
2
3
4
5
6
7
8
#读取根目录    
location / {
#配置代理
proxy_pass http://192.168.245.233;
#设置读取的目录
root html;
index index.html index.htm;
}

2.7、配置多个 conf 文件

在复杂的场景下可能会配置多个 conf 文件,使用 include 关键字包含其它的配置文件。

1
2
#配置多个conf文件包含进来
include conf/*.conf;

3、nginx 反向代理配置

反向代理恰恰是 nginx 服务的重要功能之一,着重演示一下反向代理过程。其实配置参数很简单,使用 proxy_pass 即可配置反向代理,http:// 后面配置的可以是 ip 也可以是域名。

1
2
3
4
5
6
7
http{
server{
location / {
proxy_pass http://192.168.245.233:88;
}
}
}

3.1、反向代理到 Apache

3.1.1、在 Redhat7 系列直接使用 yum 命令安装 Apache,通过 rpm 命令验证是否安装 httpd 服务

1
2
3
[nginx@localhost ~]$ rpm -qa | grep httpd
httpd-2.4.6-97.el7.centos.4.x86_64
httpd-tools-2.4.6-97.el7.centos.4.x86_64

3.1.2、安装 Apache 服务

Redhat 系列使用 yum 命令安装 httpd。

1
2
$ sudo yum -y install httpd
# yum -y install httpd

httpd 安装后的目录,主要配置文件存放在 conf 目录下:

  • conf:httpd.conf 配置文件目录;
  • conf.d:其它配置文件,比如用户存储目录配置;
  • logs:接收的日志文件 access_log 、error_log;
  • modules:代理、请求以及重写规则等模块。
1
2
3
4
[nginx@localhost ~]$ ls /etc/httpd/
conf conf.d conf.modules.d logs modules run
[nginx@localhost ~]$ ls /etc/httpd/conf
httpd.conf magic

使用 yum 命令安装后的命令脚本,使用 whereis 命令查看 httpd,默认路径在 /usr/sbin/httpd。

1
2
3
4
[nginx@localhost ~]$ whereis httpd
httpd: /usr/sbin/httpd /usr/lib64/httpd /etc/httpd /usr/share/httpd /usr/share/man/man8/httpd.8.gz
[nginx@localhost ~]$ ll /usr/sbin/httpd
-rwxr-xr-x. 1 root root 523640 /usr/sbin/httpd

3.1.3、修改 Apache 服务监听的端口,修改 Listen 后面的参数为 81 端口进行测试。

1
2
# vim /etc/httpd/conf/httpd.conf
Listen 81

访问的 html 页面/usr/share/httpd/noindex 目录下:

1
2
[nginx@localhost ~]$ ls /usr/share/httpd/noindex/
css images index.html

3.1.4、反向代理 Apache

配置负载均衡,设置了参数 weight 权重。在我们安装的 nginx 服务中进行配置代理,测试使用的 nginx 服务设置监听 8081 端口。并且给反向代理的 Apache 服务配置的权重为 2,执行 3 次有两次会显示 Apache 服务页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http{
upstream test {
server 192.168.245.147:81 weight=2;
server 192.168.245.147;
}
server{
listen 8081;
server_name localhost;
location / {
proxy_pass http://test;
root html;
index index.html index.htm;
}
}
}

在 Chrome 浏览器访问。使用 upstream 配置了负载均衡,访问 3 次有两次定位 Apache 页面,一次定位 hexo 搭建静态页面:

1
http://192.168.245.147:8081

3.2、反向代理到 Tomcat

开启 Tomcat 服务默认使用端口 8080,可以根据实际情况修改。加入到防火墙规则

1
2
[root@localhost conf]# firewall-cmd --zone=public --add-port=8080/tcp --permanent 
success

重载防火墙

1
2
[root@localhost conf]# firewall-cmd --reload 
success

原始启动 tomcat 服务默认 server.xml 配置的是 8080 端口,我进行了反向代理使用 8081 访问。启动 tomcat 服务

1
[root@localhost conf]# /usr/local/apache-tomcat-8.5.49/bin/catalina.sh run &

加入 tomcat 服务的 ip 地址到负载均衡。设置访问 Apache 服务的权重 weight=2,访问两个站点 3 次,其中两次会出现 Apache 页面,第三次则会出现 Apache Tomcat 页面。

1
2
3
4
5
upstream test {
server 192.168.245.147:81 weight=2;
#server 192.168.245.147;
server 192.168.245.147:8080;
}

实际上你可以在 nginx 上再套一层 nginx,nginx 反向代理 nginx,只是没有代理其它中间件来的那么直观。

四、nginx 负载均衡

其实我在演示上面的反向代理过程中,就已经用到了负载均衡。在测试的过程中,请加入需要的防火墙规则,避免造成不必要的麻烦。

tips:server 后面可以接 ip,也可以接域名。

1、负载均衡的几种模式

1.1、负载均衡默认配置

默认的负载均衡设置,采用轮询的形式,权重是均衡的。如果想测试建议配置多个 nginx 监听服务,然后进行测试。非要问个为什么,那就是 nginx 很轻很小但是功能很强大

1
2
3
4
5
6
#默认负载均衡(轮询)
upstream proxy_demo1{
server 192.168.245.233:8086;
server 192.168.245.233:8087;
server 192.168.245.233:8088;
}

1.2、负载均衡加权轮询

做 5 次刷新访问的页面测试,其中有 3 次会定位到设置权重为 3 的 8087 端口对应的 ip 上,剩余两次分别定位到 8086 和 8088 上。

1
2
3
4
5
6
#加权轮询负载均衡
upstream proxy_demo2{
server 192.168.245.233:8086;
server 192.168.245.233:8087 weight=3;
server 192.168.245.233:8088;
}

1.3、负载均衡基于 ip_hash

ip_hash 配置很简单:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash

1
2
3
4
5
6
7
8
9
#配置语法
Syntax: ip_hash; Default: — Context: upstream
upstream backend {
ip_hash;#加入参数即可
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
server backend4.example.com;
}

ip_hash 策略:是将前端的访问 IP 进行 hash 操作,然后根据 hash 结果将请求分配给不同的后端节点。可以将这种策略看成一种特殊的轮询策略,每个前端访问 IP 会固定访问一个后端节点。优势:避免考虑前端用户的 session 在后端多个节点上共享的问题。

1
2
3
4
5
6
7
#基于ip的hash
upstream proxy_demo3{
ip_hash;
server 192.168.245.233:8086;
server 192.168.245.233:8087;
server 192.168.245.233:8088;
}

1.4、负载均衡基于 url 的 hash

url_hash 策略和 ip_hash 类似,属于第三方扩展模块。不同点在于 ip_hash 策略是对前端访问 IP 进行 hash 操作;url_hash 策略是对前端请求的 url 进行了 hash 操作。url_hash 优势:如果后端有缓存服务器,他能够高缓存效率,同时解决 session 的问题。缺点是后端节点出现异常,不能自动排除此节点。说到 web 缓存,相信有不少 web 后端开发者对 Squid 服务器有所了解,经典组合方式 nginx 缓存功能配合 Squid 服务

1
2
3
4
5
6
7
8
#基于url的hash
upstream proxy_demo4{
#url_hash;
hash $request_uri;
server 192.168.245.233:8086;
server 192.168.245.233:8087;
server 192.168.245.233:8088;
}

2、nginx 优化

nginx 的高级配置,针对内核、cpu、网络连接以及事件驱动模型进行配置的思考:

  • ipv4 内核 7 个参数;
  • cpu 配置优化;
  • 网络连接配置 4 个相关指令;
  • 事件驱动模型 8 个指令。

3、nginx 其它应用场景

nginx 做缓存服务器

  • nginx 服务器基于 proxy store 的缓存机制;
  • nginx 服务器基于 memcached 的缓存机制;
  • nginx 服务器基于 proxy cache 的缓存机制。
  • nginx 与 squid 服务器组合配置。

感兴趣的可以对 nginx 的 Rewrite 功能、gzip 模块;时间管理、内存管理以及工作进程进行深度学习。

总结

以上就是本次 nginx 安装与配置的全部内容,希望能对你的工作与学习有所帮助。感觉写的好,就拿出你的一键三连。在公众号上更新的可能要快一点,公众号目前还在完善中。能看到这里的,都是帅哥靓妹

最后希望对你的工作和学习有所帮助哟!

—END—