Nginx 服务器类型

1. Web服务器

  Web服务器用于提供HTTP(包括HTTPS)的访问,例如Nginx、Apache、IIS等。

2. 应用程序服务器

  应用程序服务器能够用于应用程序的运行,包括的工作有:客户会话管理、业务逻辑管理、数据操作等。

3. 代理服务器

  代理服务器通常是客户端访问的一种行为。它虽然不属于网站部署中的环境,但在整体的客户端访问中,它却是一个重要环节的服务器。

4. 反向代理

  反向代理服务器上缓存的页面,不是由于某些用户访问某个页面后留下的缓存,却是根据网站运维的策略定期、定时地生成一些后台服务器页面缓存。

  代理服务器是工作在客户端的,而反向代理服务器是工作在服务器端的。

5. 后台服务器

  后台服务器只是一种说法而已,这是根据它的工作特点来说的,换句话说就是没有直接提供给客户访问。例如众多的FastCGI 服务器,它们都是工作在后台,HTTP协议却无法访问到它们,另一种情况,如果我们从前是通过使用Apache 作为 Web 服  务器提供 HTTP访问的,现在被 Nginx 反向代理了,就是说由 Nginx 直接面对客户访问,而将请求再转到 Apache 服务器, 那么这里的 Apache 服务器就已经成为后台服务器了。

6. CDN 缓存服务器

  就是缓存服务器的角色,而且是反向代理的应用。

Nginx 服务器可以胜任其中的每一种服务器。

Nginx 服务器的基础安装及优化

在安装部署 Nginx 的时候,一定要遵循:需要某一个模块则安装,不需要则不要安装,每一个被安装的模块都会影响 Nginx 的性能和占用系统资源。

在本次的安装过程中,使用到了 3 个第三方模块:

  1. TCMalloc 模块。使用TCMalloc,可以使得内存的开销较少,而且从理论上证明,最优的分配不会比 TCMalloc 的分配好很多。
  2. ngx_cache_purge 模块。用于清除Nginx指定url的缓存
  3. nginx-upstream-fair 模块。 fair采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求。

Nginx 及模块下载链接:
链接:https://pan.baidu.com/s/1l48Pb6xIR_uNK19CzYMZbA
提取码:4eq9

操作系统:Centos 7.2 x64

前提

在安装任何软件之前,都应该查看下单用户的 最大文件描述符 和 最大使用进程数

系统默认文件描述符为 1024 对于高并发的主机远远不够的。

[root@192.168.118.15 ~]#vim /etc/security/limits.conf

在文件末尾添加,保存退出

修改成功,这里建议有条件的情况下最好重启主机,再查看是否配置生效。

上面百度云分享里有,如有更新请去github上自行下载。TCMalloc 下载链接:https://github.com/gperftools/gperftools

1. 编译安装 TCMalloc 模块

[root@192.168.118.15 /usr/local/src]#yum install unzip net-tools -y
[root@192.168.118.15 /usr/local/src]#unzip Nginx.zip
[root@192.168.118.15 /usr/local/src]#cd Nginx
[root@192.168.118.15 /usr/local/src/Nginx]#yum install gcc* -y 首先安装 libunwind
[root@192.168.118.15 /usr/local/src/Nginx]#tar xf libunwind-1.3..tar.gz
[root@192.168.118.15 /usr/local/src/Nginx]#cd libunwind-1.3.
[root@192.168.118.15 /usr/local/src/Nginx/libunwind-1.3.]#CFLAGS=-fPIC ./configure
[root@192.168.118.15 /usr/local/src/Nginx/libunwind-1.3.]#make CFLAGS=-fPIC
[root@192.168.118.15 /usr/local/src/Nginx/libunwind-1.3.]#make CFLAGS=-fPIC install 安装 gperftools [root@192.168.118.15 /usr/local/src/Nginx]#yum install autoconf automake libtool libsysfs -y
[root@192.168.118.15 /usr/local/src/Nginx]#unzip gperftools-master.zip
[root@192.168.118.15 /usr/local/src/Nginx]#cd gperftools-master
[root@192.168.118.15 /usr/local/src/Nginx/gperftools-master]#./autogen.sh
[root@192.168.118.15 /usr/local/src/Nginx/gperftools-master]#./configure
[root@192.168.118.15 /usr/local/src/Nginx/gperftools-master]#make -j
[root@192.168.118.15 /usr/local/src/Nginx/gperftools-master]#make install

说明: make -j 4  
    -j [N], --jobs[=N]          Allow N jobs at once; infinite jobs with no arg.
    这里参数建议小于等于CPU线程数

2. 修改Nginx版本号

修改Nginx版本号需要对源码进行修改

[root@192.168.118.15 /usr/local/src/Nginx]#tar xf nginx-1.14..tar.gz
[root@192.168.118.15 /usr/local/src/Nginx]#cd nginx-1.14.
[root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#vim src/core/nginx.h

3. 对第三方模块进行解压

fair 模块
[root@192.168.118.15 /usr/local/src/Nginx]#unzip nginx-upstream-fair-master.zip
ngx_cache_purge 模块
[root@192.168.118.15 /usr/local/src/Nginx]#tar xf ngx_cache_purge-2.3.tar.gz

4. 编译安装 Nginx

创建nginx运行用户:
[root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#groupadd -g www
[root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#useradd -u -g -M -s /sbin/nologin www
安装依赖包:
[root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#yum install openssl-devel pcre-devel -y
[root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-google_perftools_module --with-pcre --add-module=../nginx-upstream-fair-master/ --add-module=../ngx_cache_purge-2.3/ --user=www nginx用户
--group=www nginx组
--with-http_ssl_module 支持https
--with-http_realip_module 向后端转发客户端真实地址
--with-http_gzip_static_module 支持静态gzip,节约带宽资源
--with-http_stub_status_module 可配置ngx_status 查看当前连接数量和状态
--with-pcre 支持正则表达式
--with-google_perftools_module 第三方tcmalloc模块,减少内存开销
--with-pcre --add-module=../nginx-upstream-fair-master/ 第三方模块fair,后端分配算法
--add-module=../ngx_cache_purge-2.3/ 第三方模块,通过url清除缓存 [root@192.168.118.15 /usr/local/src/Nginx/nginx-1.14.]#make -j && make install
[root@192.168.118.15 ~]#echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@192.168.118.15 ~]#source /etc/profile.d/nginx.sh 检查 nginx 配置是否存在异常
[root@192.168.118.15 ~]#nginx -t
nginx: error while loading shared libraries: libprofiler.so.: cannot open shared object file: No such file or directory
[root@192.168.118.15 ~]#find / -name "libprofiler.so.0" 搜索模块并添加到 /lib64下
[root@192.168.118.15 ~]#find / -name "libprofiler.so.0"
/usr/local/lib/libprofiler.so.
/usr/local/src/Nginx/gperftools-master/.libs/libprofiler.so.
[root@192.168.118.15 ~]#ln -vs /usr/local/lib/libprofiler.so. /lib64/
‘/lib64/libprofiler.so.’ -> ‘/usr/local/lib/libprofiler.so.’ [root@192.168.118.15 ~]#nginx -t
nginx: error while loading shared libraries: libunwind.so.: cannot open shared object file: No such file or directory
[root@192.168.118.15 ~]#find / -name "libunwind.so.8"
/usr/local/lib/libunwind.so.
/usr/local/src/Nginx/libunwind-1.3./src/.libs/libunwind.so.
[root@192.168.118.15 ~]#ln -vs /usr/local/lib/libunwind.so. /lib64/
‘/lib64/libunwind.so.’ -> ‘/usr/local/lib/libunwind.so.’ [root@192.168.118.15 ~]#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

nginx安装成功。

5. 配置 nginx 高亮工具

配置高亮工具是为了在编写nginx.conf 的时候不会写错参数名

[root@192.168.118.15 ~]#mkdir -pv /root/.vim/syntax
mkdir: created directory ‘/root/.vim’
mkdir: created directory ‘/root/.vim/syntax’
[root@192.168.118.15 ~]#cp -a /usr/local/src/nginx.vim /root/.vim/syntax/
[root@192.168.118.15 ~]#echo "au BufRead,BufNewFile /usr/local/nginx/conf/* set ft=nginx" >> /usr/share/vim/vim74/filetype.vim 然后打开配置文件查看:
[root@192.168.118.15 ~]#vim /usr/local/nginx/conf/nginx.conf

6. 检查三方模块是否成功

tcmalloc 模块

[root@192.168.118.15 ~]#yum install lsof -y
编写 nginx.conf 添加 tcmalloc 配置
[root@192.168.118.15 ~]#vim /usr/local/nginx/conf/nginx.conf

启动 nginx
[root@192.168.118.15 ~]#nginx

检查 tcmalloc 模块

tcmalloc 配置启动成功。

fair 模块
[root@192.168.118.15 ~]#vim /usr/local/nginx/conf/nginx.conf

检查 nginx 语法是否正确

[root@192.168.118.15 ~]#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

这样就已经说明 fair配置成功并启动使用。

ngx_cache_purge 模块

安装 httpd 并配置为 8080 端口启动

httpd 配置这里省略

[root@192.168.118.15 ~]#netstat -ntplu | egrep httpd
tcp6 ::: :::* LISTEN /httpd

启动成功。

[root@192.168.118.15 /usr/local/nginx]#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@192.168.118.15 /usr/local/nginx]#nginx -s reload

浏览器访问:

已经生成缓存文件

使用 ngx_cache_purge 模块 清除缓存

文件已被删除,ngx_cache_purge 模块使用成功。

常见问题:

1. Nginx 在 windows 下的性能如何?

答: 在同等硬件配置下,Nginx 在 windows 下的性能远低于 Linux 系统下的性能。

2. Nginx 与 Apache 比较有那些优点?

答:Apache 重在功能,而 Nginx 重在性能。Apache 有几百个模块,模块即功能,但一个 Apache 服务器最多只有 2000 + 的并发量;Nginx 提供的模块也就几十个,但它却提供了 20 000 的并发量。Apache提供的功能多,而Nginx提供的功能少。

有这样一句话:Apache 就像 Microsoft Word 它有一百万个选项,但你只需要 6 个。Nginx 只做了这 6 件事,但是它做的这六件事中有五件事是Apache做的 50 倍

3. 安装完成 Nginx 后,如何查看 Nginx 的版本

可以看到,我们这里已经将 Nginx 版本做修改了。

4. 安装完成 Nginx 后,如何查看 configure 时的配置

建议:在添加第三方模块的时候建议使用绝对路径,这样对后期查找更加方便

nginx 服务器篇的更多相关文章

  1. nginx入门篇----nginx服务器基础配置

    1.nginx.conf文件结构...                         #全局块  events{  ...  }  http                      #http块{ ...

  2. Linux服务器架设篇,Nginx服务器的架设

    1.安装 nginx依赖包 (1)安装pcre yum install pcre-devel (2)安装openssl yum -y install openssl-devel (3)安装zlib y ...

  3. LNMP平台搭建---Nginx安装篇

    在上一篇博文<LNMP平台搭建---Linux系统安装篇>中,我们安装了CentOS版本的Linux操作系统,现在,我们来安装一个Web服务器,大标题写着LNMP,其中的N就是Nginx, ...

  4. 【T电商 1】Nginx服务器搭建

    在项目中,首先是需要Nginx服务器作为一个图片服务器来使用.那么,久涉及到服务器的搭建.这次服务器的搭建,主要是在三个环境上进行了学习:CentOS6.2,CentOS7,和Ubuntu16.那么本 ...

  5. FreeBSD上构架Nginx服务器

    这篇文章主要记录作者如何在FreeBSD上构架Nginx服务器.作者采用下载该程序的一个源代码包手动编译的方法,而不是使用包管理工具.这样做有两个原因:首先包质量不能保证,或无效或版本旧:其次需要在编 ...

  6. Nginx服务器中配置非80端口的端口转发方法详解

    这篇文章主要介绍了Nginx服务器中配置非80端口的端口转发方法详解,文中使用到了Nginx中的proxy_pass配置项,需要的朋友可以参考下 nginx可以很方便的配置成反向代理服务器: 1 2 ...

  7. nginx服务器搭建以及配置

    2019年第一篇博客,在新的一年祝大家新年快乐,技术更上一层楼. 今天在公司搞了好长时间的nginx服务器搭建,以及遇到的问题,总结一下,方便查询 这里使用的是百度云的服务器,CentOS7系统的 N ...

  8. Linux下安装PHP并在nginx服务器中进行配置的详细方法

    先介绍一下使用的环境:centos 7.4, PHP 7.0 , nginx 1.12 Linux系统版本可以通过命令:lsb_release -a 查看. 现在开始步入正题了! 1.  首先查看一下 ...

  9. 初探Nginx服务器的整体架构

    高度模块化的设计是 Nginx 的架构基础.Nginx 服务器被分解为多个模块,每个模块就是一个功能模块,只负责自身的功能,模块之间严格遵循“高内聚,低耦合”的原则. 核心模块 核心模块是 Nginx ...

随机推荐

  1. mysql数据库的存储过程

    一. 什么是存储过程: 存储过程是一组可编程的函数,是为了完成特定功能的SQL语句集,经过第一次编译后再次调用不需要再次编译,创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调 ...

  2. Unity3D for VR 学习(4): 自绘摄像机的视口区域锥体

    在Unity Editor下,当选择Camera组件后,可呈现出Camera视口区域锥体,非常方便.但是当选择其他物体,如Cube后,就无法得知是否在Camera市口区内了,这里我找到了雨松MOMO的 ...

  3. 【原创】【1】rich editor系列教程。前期准备,兼容

    [1]前期准备,兼容 索引目录:http://www.cnblogs.com/henryli/p/3439642.html rich editor的原理无非是启用iframe的编辑模式或者div的co ...

  4. @RequestBody 和@ResponseBody 注解详解

    简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...

  5. Linux常用网络工具:批量主机服务扫描之nmap

    Linux下有很多强大网络扫描工具,网络扫描工具可以分为:主机扫描.主机服务扫描.路由扫描等. 之前已经写过常用的主机扫描和路由扫描工具,nmap支持批量主机扫描和主机服务扫描. nmap的安装直接使 ...

  6. [DeeplearningAI笔记]序列模型2.6Word2Vec/Skip-grams/hierarchical softmax classifier 分级softmax 分类器

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.6 Word2Vec Word2Vec相对于原先介绍的词嵌入的方法来说更加的简单快速. Mikolov T, Chen ...

  7. OpenCV---边缘保留滤波EPF

    OpenCV经典的两种实现EPF方法:高斯双边和均值迁移 一:双边模糊 差异越大,越会完整保留 def bi_demo(image): dst = cv.bilateralFilter(image,0 ...

  8. OpenCV---Numpy数组的使用以及创建图片

    一:对头像的所有像素进行访问,并UI图像进行像素取反 (一)for循环取反 import cv2 as cv import numpy as np def access_pixels(image): ...

  9. SpringCloud学习(6)——Hystrix熔断器

    分布式系统面临的问题 复杂的分布式体系结构中的应用程序有数十个依赖关系, 每个依赖关系在某些时刻不可避免的失败. 服务雪崩效应 多个微服务调用的时候, 假设微服务A调用微服务B和微服务C, 微服务B和 ...

  10. CF839 C 树形DP 期望

    给一颗树,求从根出发路径长度的期望是多少. 树形DP 要想清楚期望的计算 /** @Date : 2017-08-12 23:09:41 * @FileName: C.cpp * @Platform: ...