Nginx服务部署
时间:2022-12-19 15:00:00
Nginx的基本概念
Nginx(engine x)高性能 HTTP 和反向代理 Web还提供服务器 MAP、POP3、SMTP 服务。Nginx是由伊戈尔·赛索耶夫是俄罗斯第二次访问 Rambler.ru站点(俄语为Pamблep)第一个公开版01.0于2004年10月4日发布。
Nginx 以源代码为类BSD(数据库管理员)许可证的形式以其稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,Nginx1.0.4发布事实上,Nginx并发能力在同类网页服务器中表现良好,在中国使用 Nginx百度、JD.COM、新浪、网易、腾讯、淘宝等。
Nginx的主要用途
- 服务器
Nginx 作为负载平衡服务,可以直接支持内部 Rails和PHP也可以支持程序对外服务HTTP 对外服务代理服务器。Nginx采用C语言编写,不论是系统资源开销还是 CPU 都比使用效率高 Perlbal 要好很多。
①处理静态文件、索引文件和自动索引;打开文件描述符缓冲。② 反向代理加速无缓存,负载平衡和容错简单。③FastCGI,负载平衡和容错简单。
④包括模块化结构 gzipping、byte ranges、chunked responses 以及SSI-filter filter。如果由FastCG或者其他代理服务器处理单页中存在的多个代理服务器SSI,这种处理可以在不等待的情况下进行并运行。
⑤ 支持 SSL 和 TLSSNI。 - 代码
Nginx代码完全用C语言写成,已移植到许多系统结构和操作系统,包括 nux、FreeBSD、Solaris、Mac OS X、AIX 以及Microsoft Windows。Nginx除了有自己的函数库,还有自己的函数库zlib、PCRE 和 OpenSSL此外,标准模块只使用系统C库函数。这些第三方库不需要或考虑潜在的授权冲突。 - 代理服务器
Nginx也是一款非常优秀的邮件代理服务器(该产品最早开发的目的之一也是作为邮件代理服务器)。代理服务器介于客户端和Web服务器之间的另一另一个服务器,浏览器不会直接到达 Web 服务器取回网页,但通过向代理服务器发送请求,信号将首先发送到代理服务器,代理服务器将收回浏览器所需的信息并传输给用户浏览器。
Nginx安装非常简单,配置文件非常简单(也可以支持)Perl语法)、bug服务器很少。Nginx启动特别容易,几乎可以达到7x24小时不间断运行,即使运行几个月也不需要重新启动。此外,用户还可以在不间断服务的情况下升级软件版本。
#Nginx的特点 - 跨平台
Nginx 大多数都可以Linux 还有上编译操作,还有Windows移植版。 - 配置简单
Nginx配置风格和程序开发一样简单, - 非阻塞、高并发连接
磁盘数据时,磁盘IO第一阶段是非阻塞。官方测试可支持5万并发连接,在实际生产环境中运行2万~由于30000并发连接由于Nginx使用最新的epoll模型)。 - 事件驱动
采用通信机制epoll模型,支持更大的并发连接。
Nginx服务框架
Nginx务器使用Master/Worker多过程模式。主过程(Master Process)启动后,外部信号将被连接和处理;主过程启动后,通过函数fork()产生一个或多个子过程(Won frocess),每个子过程都将进行过程初始化、模块调用、事件的接收和处理。
- 主进程
主要过程的主要功能是与外部通信和其他内部过程进行管理,具体如下:
①读取Nginx配置文件并验证其有效性和正确性。
② 建立、绑定和关闭 Socket。
③根据配置生成和管理工作流程。
④ 接收重启、关闭、重载服务等外部指令。⑤日志文件管理。 - 子进程
配置文件中可以定义子过程是由主过程生成的。该过程的主要工作是:
①接收客户端请求。
②将请求依次送到各功能模块进行过滤。
③I/O 调用,获取响应数据。
④与后端服务器通信,接收后端服务器处理结果。
数据缓存、访问缓存索引、查询和调用缓存数据。发送请求结果,响应客户端请求。
⑦接收重启、重载、退出等主要过程指令。
部署Nginx服务
- 环境准备
[root@nginx ~]# setenforce 0 setenforce: SELinux is disabled [root@nginx ~]# systemctl stop firewalld [root@nginx ~]# systemctl restart network [root@nginx ~]# yum install -y gcc gcc-c openssl-devel zlib-devel zlib pcre-devel [root@nginx ~]# groupadd -g 1001 nginx [root@nginx ~]# useradd -u 900 -g nginx -s /sbin/nologin nginx [root@nginx ~]# tail -1 /etc/passwd [root@nginx ~]# yum install -y wget [root@nginx ~]# wget https://nginx.org/download/nginx-1.12.2.tar.gz --no-check-certificate
- 安装配置Nginx
[root@nginx ~]# tar zxvf nginx-1.12.2.tar.gz [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx Configuration summary using system PCRE library using system OpenSSL library using system zlib lbrary
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@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@nginx nginx-1.12.2]# 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@nginx nginx-1.12.2]# nginx
[root@nginx nginx-1.12.2]# yum install -y net-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================
Package Arch Version Repository Size
=============================================================================================================
Installing:
net-tools x86_64 2.0-0.22.20131004git.el7 local 305 k
Transaction Summary
=============================================================================================================
Install 1 Package
Total download size: 305 k
Installed size: 917 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.22.20131004git.el7
Complete!
[root@nginx nginx-1.12.2]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14071/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1177/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1702/master
tcp6 0 0 :::22 :::* LISTEN 1177/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1702/master
- 测试
[root@nginx html]# vim index.html
[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# cat index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>网站测试(runoob.com)</title>
</head>
<body>
<h1>测试</h1>
<p>This is a test</p>
</body>
</html>
[root@nginx html]#