对于NGINX的研究,我的学习目标就是使用他的三大功能:
1.静态web服务器
2.反向代理
3.负载均衡


目录

  • 总纲目录
  • NGINX常用操作
  • NGINX配置
  • 反向代理
  • 环境搭建
  • 环境检查
  • 一键安装
  • 源码编译
  • 官网源码下载地址
  • 源码解压
  • 配置编译安装路径
  • PCRE
  • zlib
  • openssl
  • gcc/g++
  • 编译 安装


总纲目录

Ubuntu操作系统服务器搭建总纲

NGINX常用操作

未配置环境变量的时候,对 NGINX 的操作需进入到NGINX的sbin目录下,如/usr/local/nginx/sbin

启动nginx ./nginx

查看NGINX进程 ps -ef|grep nginx

root@itaso:/usr/local/nginx/sbin# ps -ef|grep nginx
root     22734     1  0 19:59 ?        00:00:00 nginx: master process ./nginx
nobody   22735 22734  0 19:59 ?        00:00:00 nginx: worker process
root     22797 16756  0 19:59 pts/0    00:00:00 grep --color=auto nginx
root@itaso:/usr/local/nginx/sbin#

验证nginx配置文件是否正确 ./nginx -t

root@itaso:/usr/local/nginx/sbin# ./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
root@itaso:/usr/local/nginx/sbin#

重载配置文件 ./nginx -s reload 或者 kill -HUP 进程ID

NGINX配置

反向代理

http {
在http块中进行配置
}

1.http块中配置upstream

upstream myapi{
		server ip1:port1;
		server ip2:port2;
    }

2.http块中的server下配置location

location /api{
	    #要转发的地址
	    proxy_pass http://myapi/api;

	    #添加header避免swagger-ui出现no response from server的错误
	    proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
	}

环境搭建

环境检查

确认当前系统是否已安装 NGINX 以及安装的版本信息。

  • 查看当前是否有 NGINX 进程在运行
root@itaso:~# ps -ef|grep nginx
root      7966   943  0 11:21 pts/0    00:00:00 grep --color=auto nginx
root@itaso:~# 
以上信息表示无 NGINX 进程在运行
以下信息则表示则 NGINX 进程正在运行
root@itaso:~# ps -ef|grep nginx
root      1170     1  0 17:37 ?        00:00:00 nginx: master process ./nginx
nobody    1171  1170  0 17:37 ?        00:00:00 nginx: worker process
root      1940   814  0 17:38 pts/0    00:00:00 grep --color=auto nginx
root@itaso:~#
  • 查看当前 NGINX 版本信息
root@itaso:~# nginx -v

Command 'nginx' not found, but can be installed with:

apt install nginx-core  
apt install nginx-extras
apt install nginx-full  
apt install nginx-light 

root@itaso:~#
以上信息表示当前无 NGINX 的安装信息

往下步骤在开始之前,建议 apt-get update

一键安装

安装:apt-get install nginx 卸载:apt-get --purge autoremove nginx 目录:/usr/sbin/nginx 配置文件:/etc/nginx/nginx.conf

以下信息则表示则 NGINX 进程正在运行
root@itaso:/etc/nginx# ps -ef|grep nginx
root     12571     1  0 19:27 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 12572 12571  0 19:27 ?        00:00:00 nginx: worker process
root     12833  1144  0 19:27 pts/0    00:00:00 grep --color=auto nginx
root@itaso:/etc/nginx# pwd

备注:对源码编译不感兴趣的可使用该方案,且本文章仅简略提及,暂不做深入教程

nginx 监听443端口 但什么都没有 nginx监听已经被使用的端口_linux

nginx 监听443端口 但什么都没有 nginx监听已经被使用的端口_源码编译_02

源码编译

安装目录:/usr/local/ 配置文件/usr/local/nginx/conf/nginx.conf

以下信息则表示则 NGINX 进程正在运行
root@itaso:~# ps -ef|grep nginx
root      1170     1  0 17:37 ?        00:00:00 nginx: master process ./nginx
nobody    1171  1170  0 17:37 ?        00:00:00 nginx: worker process
root      1940   814  0 17:38 pts/0    00:00:00 grep --color=auto nginx
root@itaso:~#
官网源码下载地址

这里包含主线版本、稳定版本、历史版本,根据自己需要下载合适版本即可。
云下载源码包: wget http://nginx.org/download/nginx-1.20.2.tar.gz 备注:当前最新版本为 nginx-1.20.2.tar.gz,故暂且以该版本为例

root@itaso:~# cd software/
root@itaso:~/software# wget http://nginx.org/download/nginx-1.20.2.tar.gz
--2022-05-29 11:38:01--  http://nginx.org/download/nginx-1.20.2.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1062124 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.20.2.tar.gz’

nginx-1.20.2.tar.gz                                                      100%[===============================================================================================================================================================================>]   1.01M   914KB/s    in 1.1s    

2022-05-29 11:38:02 (914 KB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124]

root@itaso:~/software# ls
nginx-1.20.2.tar.gz
root@itaso:~/software#

备注:以上 wget 下载的源码压缩包会保存在当前 pwd 所在的目录

源码解压

解压已下载的源码压缩包: tar -zxvf nginx-1.20.2.tar.gz

root@itaso:~/software# tar -zxvf nginx-1.20.2.tar.gz
	......
root@itaso:~/software# ls
nginx-1.20.2  nginx-1.20.2.tar.gz
root@itaso:~/software#
配置编译安装路径

初始系统中的/usr/local/(个人习惯的源码编译的安装目录)

nginx 监听443端口 但什么都没有 nginx监听已经被使用的端口_nginx_03


进入解压出来的 nginx-1.20.2 目录执行如下指令:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 指令说明如下

指定安装路径:--prefix=/usr/local/nginx

配置支持http、https的网络请求:--with-http_stub_status_module--with-http_ssl_module

root@itaso:~/software# cd nginx-1.20.2/
root@itaso:~/software/nginx-1.20.2# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for OS
 + Linux 4.15.0-177-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
	......
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

./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@itaso:~/software/nginx-1.20.2# 

1、
./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.

2、
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

3、
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

解决了 PCRE 和 zlib 依赖库的问题之后,在继续安装

root@itaso:~/software/nginx-1.20.2# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for OS
 + Linux 4.15.0-177-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 getaddrinfo() ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

root@itaso:~/software/nginx-1.20.2# 

以上末尾的信息中给出了如下信息点:
1.成功使用了 system 安装的PCRE 和 zlib 依赖库
2.OpenSSL 库尚未被成功使用
3.列举出了 nginx 安装后的一些相关路径

根据上述 信息,解决了 OpenSSL 库尚未被成功使用 的问题之后,日志信息更新为如下:

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

此时 NGINX 安装的配置即以完成,且在配置的目录下生成 nginx 文件夹,可 next 进行 make install

nginx 监听443端口 但什么都没有 nginx监听已经被使用的端口_linux_04

PCRE

PCRE 是 perl 语言兼容正则表达式,是一个用C语言编写的正则表达式函数库,Nginx编译需要PCRE(Perl Compatible Regular Expression),因为 Nginx 的 Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表达式语法。另外,pcre-devel 是使用 PCRE 做二次开发时所需要的开发库,包括头文件等,这里需要安装两个安装包 pcre 和 pcre-devel 。
第一个安装包提供编译版本的库
第二个提供开发阶段的头文件和编译项目的源代码,
如果不安装这些,那么在编译 NGINX 时就会抛出缺少 PCRE library 的异常

./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.

提示安装NGINX时需要依赖 pcre 库,依赖方式可由如下三种方式:
1.使用支持 --without-http_rewrite_module 进行支持
2.安装 PCRE 库到当前系统中
3.自行编译 PCRE 库并使用 --with-pcre=<path> 进行支持

安装方式如下
apt-get install libpcre3 libpcre3-dev

root@itaso:~/software/nginx-1.20.2# apt-get install libpcre3 libpcre3-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libpcre3 is already the newest version (2:8.39-9ubuntu0.1).
libpcre3-dev is already the newest version (2:8.39-9ubuntu0.1).
0 upgraded, 0 newly installed, 0 to remove and 31 not upgraded.
root@itaso:~/software/nginx-1.20.2# 

以上信息表示 libpcre3、libpcre3-dev 已安装最新版本
zlib

zlib是一个用于数据压缩的开源库,类似于zip。我们在linux下看到的*.gz文件,就是用zlib压缩的.
这里需要用到这个主要是因为接下来的NGINX安装是由我去官网下载的一个gz压缩包,所以需要用这个进行解压,而且在Nginx的各种模块中同样也需要使用gzip压缩。如同安装PCRE一样,同样需要安装库和它的源代码:zlibzlib-devel

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

提示安装NGINX时需要依赖 zlib 库,依赖方式可由如下三种方式:
1.使用支持 --without-http_gzip_module 进行支持
2.安装 zlib 库到当前系统中
3.自行编译 PCRE 库并使用 --with-zlib=<path> 进行支持

安装方式如下
apt-get install zlib1g

root@itaso:~/software/nginx-1.20.2# apt-get install zlib1g zlib1g-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
zlib1g is already the newest version (1:1.2.11.dfsg-0ubuntu2.1).
zlib1g-dev is already the newest version (1:1.2.11.dfsg-0ubuntu2.1).
0 upgraded, 0 newly installed, 0 to remove and 31 not upgraded.
root@itaso:~/software/nginx-1.20.2#

以上信息表示 zlib 已安装最新版本
openssl

libssl-dev是OpenSSL通用库,openssl是一个安全套接字层密码库,囊括主要的密码算法、常用密钥、证书封装管理功能及实现SSL协议(Secure Sockets Layer-安全套接层协议)
在Nginx中,如果服务器提供安全网页时则会用到OpenSSL库,所以需要安装库和它的源代码:opensslopenssl-devel

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

提示安装NGINX时需要依赖 OpenSSL 库,依赖方式可由如下三种方式:
1.当前操作可以不启用 OpenSSL 模块(备注:当不启动时,如果使用了https时将会提示找不到ssl模块)
2.安装 OpenSSL 库到当前系统中
3.自行编译 OpenSSL 库并使用 --with-openssl=<path> 进行支持

安装方式如下
apt-get install openssl libssl-dev

root@itaso:~/software/nginx-1.20.2# apt-get install openssl libssl-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libssl-dev is already the newest version (1.1.1-1ubuntu2.1~18.04.17).
openssl is already the newest version (1.1.1-1ubuntu2.1~18.04.17).
0 upgraded, 0 newly installed, 0 to remove and 29 not upgraded.
root@itaso:~/software/nginx-1.20.2#

以上信息表示 openssl 已安装最新版本
gcc/g++

gccg++分别是GNU基于linux系统下c 和 c++ 的程序编译器,其中
版本查看方式分别如下
gcc -vg++ -v 安装方式分别如下
apt-get install gccapt-get install g++

root@itaso:~/software/nginx-1.20.2# apt-get install gcc g++
Reading package lists... Done
Building dependency tree       
Reading state information... Done
g++ is already the newest version (4:7.4.0-1ubuntu2.3).
gcc is already the newest version (4:7.4.0-1ubuntu2.3).
0 upgraded, 0 newly installed, 0 to remove and 29 not upgraded.
root@itaso:~/software/nginx-1.20.2#
编译 安装

在解压的 NGINX 源码目录下,使用 make install 命令进行编译安装
说明:make install 是用来安装的,它从 Makefile 中读取指令,安装到 ./configure --prefix=<path> 指定的 path 位置

root@itaso:~/software/nginx-1.20.2# make install
	......
root@itaso:~/software/nginx-1.20.2#

安装完成之后,需进入 /usr/local/nginx/sbin 执行 ./nginx 对 NGINX 进行启动

root@itaso:/usr/local/nginx/sbin# ./nginx 
root@itaso:/usr/local/nginx/sbin# ps -ef|grep nginx
root     22734     1  0 19:59 ?        00:00:00 nginx: master process ./nginx
nobody   22735 22734  0 19:59 ?        00:00:00 nginx: worker process
root     22797 16756  0 19:59 pts/0    00:00:00 grep --color=auto nginx
root@itaso:/usr/local/nginx/sbin#

至此!日复一日的坚持就是进步!