【2022】Nginx专题:部署Nginx服务与配置文件说明

1. Nginx介绍:

1.1 Nginx是什么?

  • Nginx(“engine x”)是一个开源的,支持高性能高并发的www服务和代理服务软件。
  • 由俄罗斯人Igor Sysoev开发,最初应用于俄罗斯大型网站www.rambler.ru上。
  • Nginx具有高并发、占用系统资源少等特性。
  • Nginx可以运行在UNIX、Linux、DSB、Mac OS X、Solaris及Windows等操作系统上。

1.2 Nginx主要特性

  • 支持高并发:能支持几万并发连接
  • 资源消耗少:三万并发连接下,开始10个线程消耗内存不到200MB。
  • 可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能
  • 具备Squid等专业缓存软件的缓存功能
  • 支持异步网络I/O事件模型

1.3 Nginx软件的主要功能应用

  • 作为Web服务软件
  • 反向代理及负载均衡服务
  • 前端业务数据缓存服务

2. Nginx Web服务

2.1 Nginx作为Web服务器应用场景

  • 使用Nginx运行HTML、JS、CSS、小图片等静态数据
  • Nginx结合FastCGI运行PHP等动态程序
  • Nginx结合Tomcat/Resin等支持Java动态程序

2.2 如何选择Web服务器

工作中,根据需求来选择合适的业务服务软件:

  • 静态业务:高并发场景,首选采用Nginx
  • 动态业务:Nginx与Apache都可,建议Nginx
  • 静态+动态业务:推荐Nginx

3 编译安装Nginx

安装方法多种,本文使用编译安装方式。如果需要大规模部署,可将业务需求定制好rpm包,然后通过Ansible安装。

3.1 安装pcre库

查看当前系统版本:

cat /etc/redhat-release
uname -r

结果:

CentOS release 6.10 (Final)
2.6.32-754.el6.x86_64

采用yum方式安装pcre:

yum -y install pcre pcre-devel
rpm -qa pcre pcre-devel

结果:

pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64

3.2 安装Nginx

检查是否装有openssl、openssl-devel:

rpm -qa openssl openssl-devel

结果:如果没有,使用yum安装

openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64

创建nginx包存放目录:

mkdir -p /app/nginx-1.8.1
mkdir -p /server/tools
cd /server/tools/

下载nginx软件包:
官方地址:www.nginx.rog

wget -q http://nginx.org/download/nginx-1.8.1.tar.gz

创建nginx用户:

useradd nginx -s /sbin/nologin -M

解压软件包并进入解压后的目录:

tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1

进行编译:
编译模块可以通过./configure --help查看

./configure --user=nginx --group=nginx --prefix=/app/nginx-1.8.1/ --with-http_stub_status_module --with-http_ssl_module

安装:

make
make install

创建软链接:方便使用以及版本升级

ln -s /app/nginx-1.8.1/ /app/nginx

启动前测试:

/app/nginx/sbin/nginx -t

结果:

nginx: the configuration file /app/nginx-1.8.1//conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.8.1//conf/nginx.conf test is successful

启动Nginx服务并检查端口:

/app/nginx/sbin/nginx
netstat -utpln | grep 80

结果:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      13689/nginx

检查Nginx启动结果:以下内容代表启动成功

curl 192.168.1.31

结果:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

4.【新】【1.22.0】使用官方源进行yum安装(推荐)

4.1.安装依赖包

 # 安装C环境
 yum -y install gcc gcc-c++ autoconf make automake httpd-tools
 # 安装依赖库
 yum -y install openssl-devel pcre-devel zlib-devel gd-devel

上列安装的几个依赖库分别为:

  • openssl:用于通信安全与访问控制
  • pcre:HTTP核心模块与rewrite模块会用到pcre
  • zlib:提供压缩和解压的算法
  • gd:用于图片处理

4.2.配置nginx官方源

  • 安装必要组件
yum install yum-utils -y
  • 配置nginx源
vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

4.3.安装nginx

yum makecache
yum -y install nginx

nginx -v
nginx version: nginx/1.22.0

nginx -V
nginx version: nginx/1.22.0
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=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

查看版本为1.22.0,可以看到基本上我们常用的模块都被预安装了!

4.4.启动服务

systemctl start nginx
systemctl enable nginx

4.5.访问测试

在这里插入图片描述
看到这个页面就说明我们的nginx已经安装完成了

【新】5. Nginx目录结构与配置文件

5.1 Nginx目录结构说明

tree /app/nginx
/app/nginx
├── client_body_temp
├── conf							#nginx配置文件目录
│   ├── fastcgi.conf				#fastcgi相关参数配置文件
│   ├── fastcgi.conf.default
│   ├── fastcgi_params				#fastcgi参数文件
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types					#媒体类型
│   ├── mime.types.default
│   ├── nginx.conf					#Nginx主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params					#scgi配置文件
│   ├── scgi_params.default
│   ├── uwsgi_params				#uwsgi配置文件
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp					#fastcgi临时数据文件
├── html							#默认站点目录
│   ├── 50x.html					#错误页面显示文件
│   └── index.html					#默认的站点首页文件
├── logs							#默认日志路径
│   ├── access.log					#默认访问日志文件
│   ├── error.log					#默认错误日志文件
│   └── nginx.pid					#Nginx的pid文件
├── proxy_temp						#临时目录
├── sbin							#Nginx命令目录
│   ├── nginx						#启动命令
│   └── nginx.old
├── scgi_temp						#临时目录
└── uwsgi_temp						#临时目录

9 directories, 22 files

5.2 Nginx主配置文件

去注释显示配置文件:

egrep -v "#|^$" /app/nginx/conf/nginx.conf.default

结果:

worker_processes  1;							#worker进程数量
events {										#事件区块开始
    worker_connections  1024;					#单worker进程支持的最大连接
}												#事件区块结束
http {											#HTTP区块开始
    include       mime.types;					#支持的媒体类型库
    default_type  application/octet-stream;		#默认媒体类型
    sendfile        on;							#开启高效传输模式
    keepalive_timeout  65;						#连接超时
    server {									#server区块开始
        listen       80;						#服务端口,默认80
        server_name  localhost;					#域名主机名
        location / {							#location区块开始
            root   html;						#站点根目录
            index  index.html index.htm;		#默认首页文件
        }										#location区块结束
        error_page   500 502 503 504  /50x.html;#对应状态码及回应
        location = /50x.html {					#location开始回应50x.html
            root   html;						#站点目录为html
        }										
    }
}												#HTTP区块结束

注:server区块和location区块可以是多个。

  • 全局配置(events之上):用于配置用户组、进程pid、日志路径、文件索引、worker进程数等
  • events块:可以定义每个进程的最大连接数、驱动模型等
  • http块:定义HTTP的处理行为,内部可镶嵌多个server块
  • server块:定义web服务器的相关参数,如端口、域名、location等
  • location块:配置请求资源的路径和资源处理行为
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. Nginx简介 1.1. 什么是nginx 1.2. Nginx的优点 1.3. 哪里使用到nginx 1.4. Nginx和Apache的区别 2. 安装Nginx服务器 2.1. 在windows上安装 2.2. 在Linux上安装 2.2.1. 写在前面 2.2.2. 准备使用yum安装nginx的运行环境 2.2.3. 安装pcre 2.2.4. 安装zlib库 2.2.5. 安装nginx 2.2.6. 控制nginx 2.2.7. nginx安装服务 3. Nginx配置文件详解 3.1. Nginx的主配置文件概述 3.1.1. 认识配置文件 3.1.2. nginx配置文件结构 3.1.3. nginx的全局配置 3.2. events配置 3.3. http的配置 3.4. nginx重要指令之location 4. nginx中的rewrite 4.1. 什么是rewrite 4.2. rewrite的命令的作用域和优先级 4.3. if指令 4.3.1. if指令的语法 4.3.2. if指令中使用的逻辑运算符 4.3.3. If指令中可以使用的变量 4.3.4. if指令实例 4.4. rewrite指令 4.4.1. rewrite指令语法 4.4.2. flag标记 4.4.3. set指令 4.4.4. return指令 4.4.5. rewrite实例 5. nginx的虚拟主机 5.1. 什么是nginx的虚拟主机 5.2. 标准的虚拟主机配置 5.3. 规划虚拟主机的配置文件 6. 动静分离 7. nginx的反向代理 7.1. 什么是反向代理 7.2. 明确两个概念 7.3. 特点 7.4. 反向代理的配置 7.5. 可以将代理配置单独放在一个配置文件中 8. nginx的负载均衡(自学) 8.1. 什么是负载均衡 8.2. 负载均衡的优点 8.3. 负载均衡的分配策略 8.4. 负载均衡配置 9. 安装PHP 10. PHP-FPM 10.1. 什么是PHP-FPM 10.2. 为什么要是使用PHP-FPM 10.3. 安装并且启动PHP-FPM 10.3.1. 安装 10.3.2. fpm的配置 10.3.3. 启动和停止 10.3.4. 自启动php-fpm 10.3.5. 检查php-fpm是否启动 10.4. nginx使用php-fpm处理php

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丶重明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值