Nginx服务部署

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网站的月户有百度、京东、新浪、网易、腾讯、淘宝等。

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万~3万并发连接数(这得益于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 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@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]# 

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值