nginx 服务器篇
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 个第三方模块:
- TCMalloc 模块。使用TCMalloc,可以使得内存的开销较少,而且从理论上证明,最优的分配不会比 TCMalloc 的分配好很多。
- ngx_cache_purge 模块。用于清除Nginx指定url的缓存
- 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 服务器篇的更多相关文章
- nginx入门篇----nginx服务器基础配置
1.nginx.conf文件结构... #全局块 events{ ... } http #http块{ ...
- Linux服务器架设篇,Nginx服务器的架设
1.安装 nginx依赖包 (1)安装pcre yum install pcre-devel (2)安装openssl yum -y install openssl-devel (3)安装zlib y ...
- LNMP平台搭建---Nginx安装篇
在上一篇博文<LNMP平台搭建---Linux系统安装篇>中,我们安装了CentOS版本的Linux操作系统,现在,我们来安装一个Web服务器,大标题写着LNMP,其中的N就是Nginx, ...
- 【T电商 1】Nginx服务器搭建
在项目中,首先是需要Nginx服务器作为一个图片服务器来使用.那么,久涉及到服务器的搭建.这次服务器的搭建,主要是在三个环境上进行了学习:CentOS6.2,CentOS7,和Ubuntu16.那么本 ...
- FreeBSD上构架Nginx服务器
这篇文章主要记录作者如何在FreeBSD上构架Nginx服务器.作者采用下载该程序的一个源代码包手动编译的方法,而不是使用包管理工具.这样做有两个原因:首先包质量不能保证,或无效或版本旧:其次需要在编 ...
- Nginx服务器中配置非80端口的端口转发方法详解
这篇文章主要介绍了Nginx服务器中配置非80端口的端口转发方法详解,文中使用到了Nginx中的proxy_pass配置项,需要的朋友可以参考下 nginx可以很方便的配置成反向代理服务器: 1 2 ...
- nginx服务器搭建以及配置
2019年第一篇博客,在新的一年祝大家新年快乐,技术更上一层楼. 今天在公司搞了好长时间的nginx服务器搭建,以及遇到的问题,总结一下,方便查询 这里使用的是百度云的服务器,CentOS7系统的 N ...
- Linux下安装PHP并在nginx服务器中进行配置的详细方法
先介绍一下使用的环境:centos 7.4, PHP 7.0 , nginx 1.12 Linux系统版本可以通过命令:lsb_release -a 查看. 现在开始步入正题了! 1. 首先查看一下 ...
- 初探Nginx服务器的整体架构
高度模块化的设计是 Nginx 的架构基础.Nginx 服务器被分解为多个模块,每个模块就是一个功能模块,只负责自身的功能,模块之间严格遵循“高内聚,低耦合”的原则. 核心模块 核心模块是 Nginx ...
随机推荐
- P1407 [国家集训队]稳定婚姻
题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就结婚,结婚不到 ...
- 扶苏的bitset浅谈
bitset作为C++一个非常好用的STL,在一些题目中巧妙地使用会产生非常不错的效果.今天扶苏来分享一点bitset的基础语法和应用 本文同步发布于个人其他博客,同时作为P3674题解发布. 本文感 ...
- 关于JBoss基本说明文档及基本使用安装
关于JBoss JBoss是全世界开发者共同努力的成果,一个基于J2EE的开放源代码的应用服务器.在不 到12个月的时间里有一百万以上的拷贝被下载.JBoss是第一位的J2EE应用服务器. J ...
- java8系列
参见地址:https://segmentfault.com/a/1190000012211339
- bzoj2962 序列操作
2962: 序列操作 Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1145 Solved: 378[Submit][Status][Discuss ...
- Netfilter之连接跟踪实现机制初步分析
Netfilter之连接跟踪实现机制初步分析 原文: http://blog.chinaunix.net/uid-22227409-id-2656910.html 什么是连接跟踪 连接跟踪(CONNT ...
- 2.UiSelector API 详细介绍
一.UiSelector类介绍 //通过各种属性与节点关系定位组件 简单实例: public void testDemo2() throws UiObjectNotFoundException{ Ui ...
- vue 拦截器
拦截器:请求发送之前和请求返回之后的处理 使用:1.config---dev.env.js 开发环境配置 2. prod.env.js 生产 API:'http://www.wpdic.com' 3. ...
- Tensorflow模型变量保存
Tensorflow:模型变量保存 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tensorflow1.4.0 pyt ...
- eclipse常用快捷键大全 (转)
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...