安装nginx服务安装既简单配置

nginx 服务简介

HTTP和反向代理web服务器

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
中文名 nginx 外文名 Nginx 别 名 engine x 软件许可 BSD许可 软件类型 开源软件,服务器软件 兼容性 Linux系统,Windows NT系统,OS X 最新生产版本 1.22.0 [1] 最新主线版本 1.23.0 [1]

在Linux centos系统下安装nginx

下载nginx

这里我下载的版本是nginx-1.22.0.tar.gz

wget http://nginx.org/download/nginx-1.22.0.tar.gz

[root@localhost opt]# wget http://nginx.org/download/nginx-1.22.0.tar.gz

选择安装黄目录,/opt/ngx/

##创建安装目录
mkdir /opt/ngx
[root@localhost opt]# cd ngx/ 
[root@localhost ngx]# ls
##将安装包解压到当前目录
[root@localhost ngx]# tar zxvf /opt/nginx-1.22.0.tar.gz
##列出当前目录下文件
[root@localhost ngx]# ls
nginx-1.22.0

安装nginx服务所需依赖:

[root@localhost ngx]# yum -y install gcc-c++ pcre pcre-devel zlib zilb-devel openssl openssl-devel

编译nginx安装、开启nginx状态监测功能

[root@localhost ngx]# cd nginx-1.22.0/
[root@localhost ngx]# cd nginx-1.22.0/
##编译
[root@localhost nginx-1.22.0]# ./configure --prefix=/opt/nginx1-22/ --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
##安装命令
make && make install
## --prefix=/opt/nginx1-22/ 为你要编译的路径;

Nginx安装技术文档 http://nginx.org/en/docs/

编译参数详解

使用configure命令配置构建。它定义了系统的各个方面,包括允许 nginx 用于连接处理的方法。最后它创建一个Makefile.

该configure命令支持以下参数:

--help
打印帮助信息。
--prefix=path
定义将保存服务器文件的目录。这个相同的目录也将用于由 configure(库源的路径除外)和nginx.conf配置文件中设置的所有相对路径。它/usr/local/nginx默认设置为目录。
--sbin-path=path
设置 nginx 可执行文件的名称。此名称仅在安装期间使用。默认情况下,文件名为 prefix/sbin/nginx.
--modules-path=path
定义将安装 nginx 动态模块的目录。默认使用prefix/modules目录。
--conf-path=path
设置nginx.conf配置文件的名称。如果需要,可以通过在命令行参数中指定不同的配置文件来启动 nginx 。默认情况下,文件名为 . -c fileprefix/conf/nginx.conf
--error-log-path=path
设置主要错误、警告和诊断文件的名称。安装后,始终可以 使用error_log指令在nginx.conf配置文件 中更改文件名。默认情况下,文件名为 . prefix/logs/error.log
--pid-path=path
设置nginx.pid将存储主进程的进程 ID 的文件的名称。安装后,始终可以 使用pid指令在nginx.conf配置文件 中更改文件名。默认情况下,文件名为 . prefix/logs/nginx.pid
--lock-path=path
为锁定文件的名称设置前缀。安装后,始终可以 使用lock_file指令在nginx.conf配置文件 中更改该值。默认情况下,该值为 。 prefix/logs/nginx.lock
--user=name
设置工作进程将使用其凭据的非特权用户的名称。安装后,始终可以 使用user指令在nginx.conf配置文件 中更改名称。默认用户名是nobody。
--group=name
设置工作进程将使用其凭据的组的名称。安装后,始终可以 使用user指令在nginx.conf配置文件 中更改名称。默认情况下,组名设置为非特权用户的名称。
--build=name
设置一个可选的 nginx 构建名称。
--builddir=path
设置构建目录。
--with-select_module
--without-select_module
启用或禁用构建允许服务器使用该select()方法的模块。如果平台似乎不支持更合适的方法,例如 kqueue、epoll 或 /dev/poll,则此模块会自动构建。
--with-poll_module
--without-poll_module
启用或禁用构建允许服务器使用该poll()方法的模块。如果平台似乎不支持更合适的方法,例如 kqueue、epoll 或 /dev/poll,则此模块会自动构建。
--with-threads
启用 线程池的使用。
--with-file-aio
允许在 FreeBSD 和 Linux 上使用 异步文件 I/O (AIO)。
--with-http_ssl_module
允许构建一个将 HTTPS 协议支持添加 到 HTTP 服务器的模块。默认情况下不构建此模块。构建和运行此模块需要 OpenSSL 库。
--with-http_v2_module
允许构建一个为HTTP/2 提供支持的模块 。默认情况下不构建此模块。
--with-http_realip_module
启用构建 ngx_http_realip_module 模块,该模块将客户端地址更改为指定标头字段中发送的地址。默认情况下不构建此模块。
--with-http_addition_module
启用构建 ngx_http_addition_module 模块,该模块在响应之前和之后添加文本。默认情况下不构建此模块。
--with-http_xslt_module
--with-http_xslt_module=dynamic
允许构建使用一个或多个 XSLT 样式表转换 XML 响应的 ngx_http_xslt_module 模块。默认情况下不构建此模块。构建和运行此模块需要 libxml2和 libxslt库。
--with-http_image_filter_module
--with-http_image_filter_module=dynamic
支持构建 ngx_http_image_filter_module 模块,该模块将图像转换为 JPEG、GIF、PNG 和 WebP 格式。默认情况下不构建此模块。
--with-http_geoip_module
--with-http_geoip_module=dynamic
允许构建 ngx_http_geoip_module 模块,该模块根据客户端 IP 地址和预编译的 MaxMind数据库创建变量。默认情况下不构建此模块。
--with-http_sub_module
启用构建 ngx_http_sub_module 模块,该模块通过将一个指定的字符串替换为另一个来修改响应。默认情况下不构建此模块。
--with-http_dav_module
允许构建 通过 WebDAV 协议提供文件管理自动化的ngx_http_dav_module模块。默认情况下不构建此模块。
--with-http_flv_module
启用构建 ngx_http_flv_module 模块,该模块为 Flash 视频 (FLV) 文件提供伪流服务器端支持。默认情况下不构建此模块。
--with-http_mp4_module
启用构建 ngx_http_mp4_module 模块,该模块为 MP4 文件提供伪流服务器端支持。默认情况下不构建此模块。
--with-http_gunzip_module
启用构建 ngx_http_gunzip_moduleContent-Encoding: gzip模块,该模块使用“ ”为不支持“gzip”编码方法的客户端 解压缩响应。默认情况下不构建此模块。
--with-http_gzip_static_module
启用构建 ngx_http_gzip_static_module 模块,该模块可以发送带有“ .gz”文件扩展名的预压缩文件而不是常规文件。默认情况下不构建此模块。
--with-http_auth_request_module
允许构建 基于子请求结果实现客户端授权的ngx_http_auth_request_module模块。默认情况下不构建此模块。
--with-http_random_index_module
启用构建 ngx_http_random_index_module 模块,该模块处理以斜杠字符(' /')结尾的请求并在目录中选择一个随机文件作为索引文件。默认情况下不构建此模块。
--with-http_secure_link_module
启用构建 ngx_http_secure_link_module 模块。默认情况下不构建此模块。
--with-http_degradation_module
启用构建 ngx_http_degradation_module模块。默认情况下不构建此模块。
--with-http_slice_module
启用构建 ngx_http_slice_module 模块,该模块将请求拆分为子请求,每个子请求都返回一定范围的响应。该模块提供更有效的大响应缓存。默认情况下不构建此模块。
--with-http_stub_status_module
启用构建 ngx_http_stub_status_module 模块,该模块提供对基本状态信息的访问。默认情况下不构建此模块。
--without-http_charset_module
禁止构建 ngx_http_charset_module 模块,该模块将指定的字符集添加到“Content-Type”响应头字段,并且可以另外将数据从一个字符集转换为另一个字符集。
--without-http_gzip_module
禁用构建压缩 HTTP 服务器响应的模块。zlib 库是构建和运行此模块所必需的。
--without-http_ssi_module
禁用构建 ngx_http_ssi_module 模块,该模块在通过它的响应中处理 SSI(服务器端包含)命令。
--without-http_userid_module
禁用构建 ngx_http_userid_module 模块,该模块设置适合客户端识别的 cookie。
--without-http_access_module
禁用构建 允许限制对某些客户端地址的访问 的ngx_http_access_module模块。
--without-http_auth_basic_module
禁用构建 ngx_http_auth_basic_module 模块,该模块允许通过使用“HTTP 基本身份验证”协议验证用户名和密码来限制对资源的访问。
--without-http_mirror_module
禁止构建 ngx_http_mirror_module 模块,该模块通过创建后台镜像子请求来实现原始请求的镜像。
--without-http_autoindex_module
禁用构建 处理以斜杠字符('')结尾的请求的ngx_http_autoindex_module模块,并在ngx_http_index_module模块找不到索引文件 /的情况下生成目录列表 。
--without-http_geo_module
禁用构建 ngx_http_geo_module 模块,该模块根据客户端 IP 地址创建具有值的变量。
--without-http_map_module
禁用构建 ngx_http_map_module 模块,该模块根据其他变量的值创建具有值的变量。
--without-http_split_clients_module
禁止构建 为 A/B 测试创建变量 的ngx_http_split_clients_module模块。
--without-http_referer_module
禁用构建 ngx_http_referer_module 模块,该模块可以阻止对“Referer”标头字段中具有无效值的请求的站点访问。
--without-http_rewrite_module
禁用构建允许 HTTP 服务器 重定向请求和更改请求的 URI 的模块。构建和运行此模块需要 PCRE 库。
--without-http_proxy_module
禁用构建 HTTP 服务器 代理模块。
--without-http_fastcgi_module
禁止构建 将请求传递给 FastCGI 服务器 的ngx_http_fastcgi_module模块。
--without-http_uwsgi_module
禁止构建 将请求传递给 uwsgi 服务器 的ngx_http_uwsgi_module模块。
--without-http_scgi_module
禁止构建将请求传递给 SCGI 服务器的 ngx_http_scgi_module 模块。
--without-http_grpc_module
禁止构建 将请求传递给 gRPC 服务器 的ngx_http_grpc_module模块。
--without-http_memcached_module
禁用构建 从 memcached 服务器获取响应 的ngx_http_memcached_module模块。
--without-http_limit_conn_module
禁用构建限制每个键的连接数的 ngx_http_limit_conn_module 模块,例如,来自单个 IP 地址的连接数。
--without-http_limit_req_module
禁用构建限制每个键的请求处理速率的 ngx_http_limit_req_module 模块,例如,来自单个 IP 地址的请求的处理速率。
--without-http_empty_gif_module
禁止构建 发出单像素透明 GIF的模块。
--without-http_browser_module
禁止构建 ngx_http_browser_module 模块,该模块创建的变量的值取决于“User-Agent”请求标头字段的值。
--without-http_upstream_hash_module
禁用构建实现 散列 负载平衡方法的模块。
--without-http_upstream_ip_hash_module
禁用构建实现 ip_hash 负载平衡方法的模块。
--without-http_upstream_least_conn_module
禁用构建实现了 minimum_conn 负载平衡方法的模块。
--without-http_upstream_random_module
禁用构建实现 随机 负载平衡方法的模块。
--without-http_upstream_keepalive_module
禁用构建提供 与上游服务器 的连接缓存的模块。
--without-http_upstream_zone_module
禁用构建一个可以将上游组的运行时状态存储在共享内存 区域中的模块。
--with-http_perl_module
--with-http_perl_module=dynamic
能够构建 嵌入式 Perl 模块。默认情况下不构建此模块。
--with-perl_modules_path=path
定义了一个保存 Perl 模块的目录。
--with-perl=path
设置 Perl 二进制文件的名称。
--http-log-path=path
设置 HTTP 服务器的主要请求日志文件的名称。安装后,始终可以 使用access_log指令在nginx.conf配置文件 中更改文件名。默认情况下,文件名为 . prefix/logs/access.log
--http-client-body-temp-path=path
定义一个目录,用于存储保存客户端请求正文的临时文件。安装后,始终可以 使用client_body_temp_path 指令在nginx.conf配置文件 中更改目录。默认情况下,该目录名为 . prefix/client_body_temp
--http-proxy-temp-path=path
定义一个用于存储临时文件的目录,其中包含从代理服务器接收到的数据。安装后,始终可以 使用proxy_temp_path 指令在nginx.conf配置文件 中更改目录。默认情况下,该目录名为 . prefix/proxy_temp
--http-fastcgi-temp-path=path
定义了一个目录,用于存储带有从 FastCGI 服务器接收到的数据的临时文件。安装后,始终可以 使用fastcgi_temp_path 指令在nginx.conf配置文件 中更改目录。默认情况下,该目录名为 . prefix/fastcgi_temp
--http-uwsgi-temp-path=path
定义一个目录,用于存储从 uwsgi 服务器接收到的数据的临时文件。安装后,始终可以 使用uwsgi_temp_path 指令在nginx.conf配置文件 中更改目录。默认情况下,该目录名为 . prefix/uwsgi_temp
--http-scgi-temp-path=path
定义一个目录,用于存储从 SCGI 服务器接收到的数据的临时文件。安装后,始终可以 使用scgi_temp_path 指令在nginx.conf配置文件 中更改目录。默认情况下,该目录名为 . prefix/scgi_temp
--without-http
禁用HTTP服务器。
--without-http-cache
禁用 HTTP 缓存。
--with-mail
--with-mail=dynamic
启用 POP3/IMAP4/SMTP 邮件代理服务器。
--with-mail_ssl_module
允许构建一个模块,将 SSL/TLS 协议支持添加 到邮件代理服务器。默认情况下不构建此模块。构建和运行此模块需要 OpenSSL 库。
--without-mail_pop3_module
禁用邮件代理服务器中的POP3协议。
--without-mail_imap_module
禁用邮件代理服务器中的IMAP协议。
--without-mail_smtp_module
禁用邮件代理服务器中的SMTP协议。
--with-stream
--with-stream=dynamic
支持为通用 TCP/UDP 代理和负载平衡 构建 流模块。默认情况下不构建此模块。
--with-stream_ssl_module
允许构建一个将 SSL/TLS 协议支持添加 到流模块的模块。默认情况下不构建此模块。构建和运行此模块需要 OpenSSL 库。
--with-stream_realip_module
启用构建 ngx_stream_realip_module 模块,该模块将客户端地址更改为在 PROXY 协议头中发送的地址。默认情况下不构建此模块。
--with-stream_geoip_module
--with-stream_geoip_module=dynamic
允许构建 ngx_stream_geoip_module 模块,该模块根据客户端 IP 地址和预编译的 MaxMind数据库创建变量。默认情况下不构建此模块。
--with-stream_ssl_preread_module
启用构建 ngx_stream_ssl_preread_module 模块,该模块允许从 ClientHello 消息中提取信息而无需终止 SSL/TLS。默认情况下不构建此模块。
--without-stream_limit_conn_module
禁用构建限制每个键的连接数的 ngx_stream_limit_conn_module 模块,例如,来自单个 IP 地址的连接数。
--without-stream_access_module
禁用构建 允许限制对某些客户端地址 的访问的ngx_stream_access_module模块。
--without-stream_geo_module
禁用构建 ngx_stream_geo_module 模块,该模块根据客户端 IP 地址创建具有值的变量。
--without-stream_map_module
禁用构建 ngx_stream_map_module 模块,该模块根据其他变量的值创建具有值的变量。
--without-stream_split_clients_module
禁止构建 为 A/B 测试创建变量 的ngx_stream_split_clients_module模块。
--without-stream_return_module
禁用构建 ngx_stream_return_module 模块,该模块将一些指定的值发送到客户端,然后关闭连接。
--without-stream_set_module
禁止构建为变量设置值的 ngx_stream_set_module 模块。
--without-stream_upstream_hash_module
禁用构建实现 散列 负载平衡方法的模块。
--without-stream_upstream_least_conn_module
禁用构建实现了 minimum_conn 负载平衡方法的模块。
--without-stream_upstream_random_module
禁用构建实现 随机 负载平衡方法的模块。
--without-stream_upstream_zone_module
禁用构建一个可以将上游组的运行时状态存储在共享内存 区域中的模块。
--with-google_perftools_module
允许构建 ngx_google_perftools_module模块,该模块可以使用Google Performance Tools 对 nginx 工作进程进行分析 。该模块适用于 nginx 开发人员,默认情况下不构建。
--with-cpp_test_module
启用构建 ngx_cpp_test_module模块。
--add-module=path
启用外部模块。
--add-dynamic-module=path
启用外部动态模块。
--with-compat
启用动态模块兼容性。
--with-cc=path
设置 C 编译器的名称。
--with-cpp=path
设置 C 预处理器的名称。
--with-cc-opt=parameters
设置将添加到 CFLAGS 变量的附加参数。在 FreeBSD 下使用系统 PCRE 库时, --with-cc-opt="-I /usr/local/include" 应指定。select()如果需要增加 支持的文件数量,也可以在这里指定,例如: --with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=parameters
设置将在链接期间使用的附加参数。在 FreeBSD 下使用系统 PCRE 库时, --with-ld-opt="-L /usr/local/lib" 应指定。
--with-cpu-opt=cpu
启用每个指定 CPU 的构建: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, sparc32, sparc64, ppc64.
--without-pcre
禁用 PCRE 库的使用。
--with-pcre
强制使用 PCRE 库。
--with-pcre=path
设置 PCRE 库源的路径。库分发需要从 PCRE站点下载并提取。其余的由 nginx./configure和 make. location指令 中的正则表达式支持 和ngx_http_rewrite_module 模块需要该库。
--with-pcre-opt=parameters
为 PCRE 设置额外的构建选项。
--with-pcre-jit
构建具有“即时编译”支持的 PCRE 库(1.1.12, pcre_jit指令)。
--without-pcre2
禁用 PCRE2 库而不是原始 PCRE 库 (1.21.5) 的使用。
--with-zlib=path
设置 zlib 库源的路径。库分发(版本 1.1.3 — 1.2.11)需要从 zlib站点下载并解压缩。其余的由 nginx./configure和 make. ngx_http_gzip_module模块 需要该库 。
--with-zlib-opt=parameters
为 zlib 设置额外的构建选项。
--with-zlib-asm=cpu
启用针对指定 CPU 之一优化的 zlib 汇编源代码: pentium, pentiumpro.
--with-libatomic
强制使用 libatomic_ops 库。
--with-libatomic=path
设置 libatomic_ops 库源的路径。
--with-openssl=path
设置 OpenSSL 库源的路径。
--with-openssl-opt=parameters
为 OpenSSL 设置额外的构建选项。
--with-debug
启用调试日志。

启动nginx、进入sbin目录,找到nginx启动命令;

[root@localhost ~]# cd /opt/ng
nginx1-22/    nginx-1.22.0/ ngx/          
[root@localhost ~]# cd /opt/ng
nginx1-22/    nginx-1.22.0/ ngx/          
[root@localhost ~]# cd /opt/nginx1-22/
[root@localhost nginx1-22]# ls
conf  html  logs  sbin
[root@localhost nginx1-22]# cd sbin/
[root@localhost sbin]# ls
nginx
#nginx的启动命令就放在sbin/目录下

[root@localhost sbin]# netstat -tunlp |grep nginx
[root@localhost sbin]# pwd
/opt/nginx1-22/sbin
[root@localhost sbin]# ./nginx  
nginx: [emerg] getpwnam("nginx") failed (2: No such file or directory)
# 报错是因为没有创建Nginx用户
#解决办法创建用户即可
[root@localhost sbin]# useradd -s /bin/nologin -M nginx
[root@localhost sbin]# ./nginx 
[root@localhost sbin]# netstat -tunlp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      100137/nginx: maste 
#到此Nginx服务就安装完成了

多端口虚拟主机实现

修改NGINX网页根目录;

[root@localhost opt]# ls /opt/nginx1-22/conf/
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost opt]# cd /opt/nginx1-22/conf/
[root@localhost conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost conf]# cvim nginx.conf
bash: cvim: command not found...
Similar command is: 'vim'
[root@localhost conf]# vim nginx.conf

nginx配置文件

#gzip  on;

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    #这里是Nginx网站配置
    location / {
    #通过root指令 确定网页文件位置放在哪里
    #这里的html指的是Nginx服务下面指定的网页文件夹(可以通过更改此文件夹来改变文件,更改过后重启Nginx才能生效)
        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;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #

修改网页配置文件后,得先验证配置文件是否正确:

命令:/opt/nginx1-22/sbin/nginx -t

[root@localhost conf]# /opt/nginx1-22/sbin/nginx -t
nginx: the configuration file /opt/nginx1- 
22//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-2 
2//conf/nginx.conf test is successful
告诉Nginx重新读取配置文件

命令:/opt/nginx1-22/sbin/mginx -s reload