1.什么是Nginx?

  • Nginx: engine X 2002年开发,分为社区版和商业版(nginx plus);
  • 2019年被F5 Networks收购
  • Nginx是免费、开源、高性能的HTTP和反向代理服务器、邮件代理服务器、以及TCP/UDP代理服务器
  • Nginx官网: https://nginx.org

1.2.为什么选择Nginx?

  • 静态的web资源服务器html、图片、js、css,txt等静态资源;
  • http/https协议的反向代理;
  • 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求;
  • tcp/udp协议的请求转发;
  • 模块化设计、较好的拓展性;
  • 支持热部署: 不停机更新配置文件,升级版本,更换日志文件;
  • 低内存消耗: 10000个keep-alive连接模式下的非活动连接仅需要2.5M内存;
  • event-driven、aio、mmap、sendifile;

1.3.服务相关的功能

  1. 虚拟主机(Server)
  2. 支持keep-alive和管理连接(利用一个连接做多次请求);
  3. 访问日志(支持基于日志缓冲提高其性能);
  4. 支持rul rewirte重写
  5. 路径别名
  6. 基于IP及用户的访问控制
  7. 支持速率限制及并发限制
  8. 重新配置和在线升级无须中断客户的工作进程;

2.Nginx的编译安装

2.1官网下载Nginx的安装包

https://nginx.org/en/download.html

2.2安装编译一些工具;

必须要安装pcre包,否则无法编译安装;会报错

./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=<path> option.
# 安装编译工具;
root@nginx-21-web:~# apt install gcc openssl libssl-dev zlib1g-dev libpcre3 libpcre3-dev

2.3上传安装包至机器并解压;

root@nginx-21-web:/apps/nginx/html# cat /etc/issue
Ubuntu 18.04.3 LTS \n \l
root@nginx-21-web:~/nginx-app# tar -xvf nginx-1.23.1.tar.gz 


2.4创建Nginx用户并安装Nginx;

# 因为启动的时候需要Nginx用户,所以创建用户;
root@nginx-21-web:~/nginx-app# useradd -r -s /sbin/nologin nginx

# 更多编译参数可以使用下面命令来查看;
root@nginx-21-web:/usr/local/src/nginx-1.23.1# ./configure  --help
--help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support


# 执行编译;
root@ubuntu:/usr/local/src/nginx-1.23.1# ./configure --prefix=/apps/nginx \  # 指定安装路径
--user=nginx \     # 以Nginx账户启动
--group=nginx \    # 以Nginx组运行
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre --with-stream \
--with-stream_ssl_module \
--with-stream_realip_module 
checking for OS
 + Linux 4.15.0-55-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found

2.5 执行make && make install;

root@ubuntu:/usr/local/src/nginx-1.23.1# # make && make install
make -f objs/Makefile
make[1]: Entering directory '/usr/local/src/nginx-1.23.1'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
cp conf/nginx.conf '/apps/nginx/conf/nginx.conf.default'
test -d '/apps/nginx/logs' \
	|| mkdir -p '/apps/nginx/logs'
test -d '/apps/nginx/logs' \
	|| mkdir -p '/apps/nginx/logs'
test -d '/apps/nginx/html' \
	|| cp -R html '/apps/nginx'
test -d '/apps/nginx/logs' \
	|| mkdir -p '/apps/nginx/logs'
make[1]: Leaving directory '/usr/local/src/nginx-1.23.1'


2.6Nginx安装成功;

root@ubuntu:/apps/nginx# ll
total 24
drwxr-xr-x 6 root root 4096 Aug  2 18:32 ./
drwxr-xr-x 3 root root 4096 Aug  2 18:32 ../
drwxr-xr-x 2 root root 4096 Aug  2 18:32 conf/
drwxr-xr-x 2 root root 4096 Aug  2 18:32 html/
drwxr-xr-x 2 root root 4096 Aug  2 18:32 logs/
drwxr-xr-x 2 root root 4096 Aug  2 18:32 sbin/

#  授权nginx用户nginx组为这个目录的所有者;
root@ubuntu:/apps# chown -R nginx.nginx /apps/nginx

# 创建链接到/usr/sbin/;因为执行nginx -v是无法找到路径的所以须链接;
root@ubuntu:~# ln -s /apps/nginx/sbin/nginx  /usr/sbin/

# 查看nginx的版本;
root@nginx-21-web:~# nginx -v
nginx version: nginx/1.23.1

# 查看nginx的编译参数;
root@nginx-21-web:~# nginx -V
nginx version: nginx/1.23.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

2.7启动nginx;

root@nginx-21-web:~# nginx # 启动Nginx
root@nginx-21-web:~# nginx -s stop # 暂停Nginx

2.8查看端口;

查看端口是80;
root@nginx-21-web:~# ss -tnl | grep 80
LISTEN   0         128                 0.0.0.0:80               0.0.0.0:*   

2.9访问测试;

root@nginx-21-web:~# curl 10.0.0.21
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

3.Nginx的Service启动文件自定义;

3.1准备Service启动文件;

准备和yum安装一样的Nginx的启动文件修改参数即可;

root@nginx-21-web:~# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

3.2创建目录且修改配置文件

root@nginx-21-web:/apps/nginx# mkdir /apps/nginx/run/
root@nginx-21-web:/# cat /apps/nginx/conf/nginx.conf | grep pid
pid        /apps/nginx/run/nginx.pid;

3.3验证启动文件并设置为开机自启动;

root@nginx-21-web:/apps/nginx# systemctl daemon-reload
root@nginx-21-web:/apps/nginx# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

3.4查看Pid以及验证端口

root@nginx-21-web:/apps/nginx# ll /apps/nginx/run/
total 12
drwxr-xr-x  2 root  root  4096 Aug  2 18:59 ./
drwxr-xr-x 12 nginx nginx 4096 Aug  2 18:58 ../
-rw-r--r--  1 root  root     6 Aug  2 18:59 nginx.pid

# 查看进程是以nginx.nginx运行;
root@nginx-21-web:/apps/nginx# ss -tnlp | grep 80
LISTEN   0         128                 0.0.0.0:80               0.0.0.0:*        users:(("nginx",pid=28999,fd=6),("nginx",pid=28998,fd=6))                      

root@nginx-21-web:/apps/nginx# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-08-02 18:59:15 CST; 25s ago
  Process: 28997 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, s
 Main PID: 28998 (nginx)
    Tasks: 2 (limit: 2847)
   CGroup: /system.slice/nginx.service
           ├─28998 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
           └─28999 nginx: worker process

Aug 02 18:59:15 nginx-21-web systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 02 18:59:15 nginx-21-web systemd[1]: Started The nginx HTTP and reverse proxy server.

# 测试访问;
root@nginx-21-web:/apps/nginx/html# curl 10.0.0.21:80
hai tang