高性能 HTTP 和反向代理 web 服务器Nginx

高性能 HTTP 和反向代理 web 服务器 Nginx#

Nginx (engine x) 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务,在这篇文章里,我们一起来简单看看。

1、准备依赖包#

yum -y install zlib-devel pcre-devel openssl-devel gcc

2、准备 nginx 用户#

useradd -M -s /sbin/nologin nginx

-M 没有家目录,没有登录,-s 不能用来登录

3、准备存放软件目录与源码编译与安装#

mkdir /Software
cd /Software

Nginx 下载网站:http://nginx.org/en/download.html 生产环境选择 Stable 版本


wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.19.10.tar.gz
cd nginx-1.19.10
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module --with-http_sub_module --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module  --with-file-aio --with-http_realip_module 
make && make install
启动 nginx:
/usr/local/nginx/sbin/nginx

4、安装后查看#

查看是否开启80端口:

netstat   -tunpl  |grep  80

查看是否开启nginx进程:

pstree  | grep  nginx

查看nginx进程守护者:

pstree   -u  | grep  nginx

查看nginx版本号:

6、服务管理:#

1.检测配置语法
/usr/local/nginx/sbin/nginx -t

2.启动服务
/usr/local/nginx/sbin/nginx

3.重载服务
/usr/local/nginx/sbin/nginx -s reload
pkill -HUP nginx

4.关闭服务

 (1).快速关闭 Nginx:
     /usr/local/nginx/sbin/nginx -s stop
     pkill nginx

  (2).关闭 Nginx:
     /usr/local/nginx/sbin/nginx -s quit

5.开机启动
(1)vim /etc/rc.local
     加一行即可:
    /usr/local/nginx/sbin/nginx
    取消开机自启:
    删除以上配置即可
(2systemctl(或者service) enable nginx
    取消开机自启
    systemctl disable nginx

7、编写启动脚本#

cd /usr/lib/systemd/system/

sudo vim nginx.service

[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载服务文件:

sudo systemctl daemon-reload 
sudo systemctl start|stop|reload|restart|status nginx.service

第二种开机自启:

systemctl enable nginx.service

关闭开机自启:

systemctl disable nginx.service

8、配置环境变量#

现在 nginx 需要在/usr/local/nginx/sbin 目录下才能执行:

接下来我们配置nginx全局环境变量:

vim /etc/profile
# 在最后加上下面一行保存退出:
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile

# 测试效果:
cd /Software/nginx-1.24.0/

nginx -v
输出:
nginx version: nginx/1.24.0

nginx -t
输出:
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

9、网站部署#

先安装上传下载工具lrzsz:

yum install -y lrzsz

创建网站:

mkdir /var/www
cd /var/www
上传源码:
执行 rz

#先备份 nginx 出厂配置文件:
cd /usr/local/nginx/conf 
cp nginx.conf nginx.conf.bak

# 创建 server 配置文件目录
mkdir vhosts
# 将 vhosts下server包含进 nginx.conf中
vim nginx.conf
#在 http 模块最后加上 include vhosts/*.conf 保存退出
http {
    include       mime.types;
    default_type  application/octet-stream;
    .
    .
    .
    include vhosts/*.conf;
}
# 创建要部署的server配置
cd vhosts
vim html5-admin.conf
#配置如下:
server {
    listen 8001; # 端口,服务器安全组入方向需开放
    server_name 8.xx.xx; # 域名解析用域名即可,无域名用IP也可以
    root /var/www/Html5-admin; # 网站根目录
    index index.html; # 配置网站首页 index 、index.html 文件
}

#重启 nginx
systemctl restart nginx

访问网站:

高性能 HTTP 和反向代理 web 服务器Nginx

10、Stub_status 测试#

1、安装 ab 压测工具
yum install -y httpd-tools

检测 ab 安装
ab -v
输出:
ab: option requires an argument -- v
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                    This implies -n 50000
                    .
                    .
                    .
-E certfile     Specify optional client certificate chain and private key

2、nginx 配置文件添加 Stub_status 配置:
   vim /usr/local/nginx/conf/vhosts/html5-admin.conf 

server {
    listen 8001;
    server_name 8.xx.xx;
    root /var/www/Html5-admin;
    index index.html;
    # stub_status ab 压测配置
    location /nginxstatus {
       stub_status;
    }
}

#重载配置文件:
systemctl reload nginx

ab 压测:
高性能 HTTP 和反向代理 web 服务器Nginx

本作品采用《CC 协议》,转载必须注明作者和本文链接
ThinkQ
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。