高性能web服务器——nginx

一、 简介

1. nginx是什么?

l 是一个使用c语言开发的高性能的http服务器和反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。

l 是俄罗斯的程序设计师Igor Sysoev为俄罗斯访问量第二的 Rambler.ru 站点开发的。

2. nginx的优点?

l 轻量级

l 在应对高并发情况时,能保持低资源低消耗高性能

l 高度模块化的设计,配置简洁

l 官方测试nginx能够支撑5万并发量,并且cpu、内存等资源消耗却非常低,运行非常稳定

3. nginx的应用场景?

l http服务器。Nginx可以独立提供http服务,可以做网页静态服务器

l 虚拟主机。可以实现在一台服务器虚拟出多个网站

l 反向代理,负载均衡

4. nginx版本下载

l http://nginx.org/en/download.html

二、 nginx安装

1. 安装准备

输入

yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

2. nginx安装

  • 解压
[root@node01 local]# tar -zxvf nginx-1.10.3.tar.gz
  • 进入解压目录
[root@node01 local]# cd nginx-1.10.3/
  • 复制下面这段,执行configure,生成Makefile
./configure 

--prefix=/usr/local/nginx 

--pid-path=/var/run/nginx/nginx.pid 

--lock-path=/var/lock/nginx.lock 

--error-log-path=/var/log/nginx/error.log 

--http-log-path=/var/log/nginx/access.log 

--with-http_gzip_static_module 

--http-client-body-temp-path=/var/temp/nginx/client 

--http-proxy-temp-path=/var/temp/nginx/proxy 

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi 

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi 

--http-scgi-temp-path=/var/temp/nginx/scgi
  • 编译
[root@node01 nginx-1.10.3]# make
  • 安装
[root@node01 nginx-1.10.3]# make install
  • 创建链接
[root@node01 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
  • 验证配置有没有问题
[root@node01 conf]# nginx -t

  • 若出现少文件夹的报错,自己创建一下
[root@node01 conf]# nginx


[root@node01 conf]# mkdir /var/temp/nginx

3. nginx启动

  • 检查防火墙,是关闭的状态
[root@node01 conf]# systemctl status firewalld.service

  • 启动,查看进程
[root@node01 conf]# nginx

[root@node01 conf]# ps aux|grep nginx
  • 浏览器访问

4. nginx停止

  • 关闭并查看进程
[root@node01 conf]# nginx -s stop

[root@node01 conf]# ps aux|grep nginx

5. nginx刷新配置

  • 启动后,若修改配置,如下刷新即可,不需要重启nginx
[root@node01 conf]# nginx

[root@node01 conf]# nginx -s reload

三、 nginx详解

1. nginx安装目录

[root@localhost html]# cd /usr/local/nginx

[root@localhost nginx]# ll

conf:nginx的配置文件

html:nginx默认访问的根目录,存放静态资源

sbin:存放nginx运行脚本

2. nginx配置文件

  • worker_processes:工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。

  • worker_connections:每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。

  • include:设置支持的文件类型,具体内容在mime.types中

  • sendfile:sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

  • keepalive_timeout:keepalive超时时间。

  • server:一个server就是一个虚拟机

  • listen:server虚拟机的端口号

  • server_name:server虚拟机的主机名

  • location:默认访问的资源

  • root html:虚拟主机的根目录

四、 nginx基本使用

1. web站点

如下目录,存的东西,外部可直接访问

2. 多虚拟主机

省钱,省事

  • 直接修改配置
[root@node01 conf]# vi nginx.conf
  • 在原server下,再加一个server
server{
	listen 85;
	server_name _;
	location / {
		root	py;
		index	index.html index.htm;
	}
}
  • 复制一份html文件夹,叫py,跟配置对应上
[root@node01 nginx]# cp -r html/ py
  • 修改index.html,让自己能区分

  • 刷新配置
[root@node01 py]# nginx -s reload 

3. 404页面设置

  • 配置里,可以把如下注释打开,自己创建错误页面

  • 创建一个对应的404.html

  • 刷新配置

4. nginx反向代理

  • 如下,此时,访问80端口,实际上会转发到85端口

  • 刷新配置

  • 访问80,跳转到了85

5. nginx负载均衡

  • 修改配置文件
[root@node01 conf]# vi nginx.conf

= 添加负载均衡池,80端口反向代理到负载均衡池

  • 下面多一个95端口的server

  • 复制py,创建个p2,修改里面的index.html,有区分就行

  • 刷新配置

  • 页面访问,此时实现了反向代理到85和90,也是按照权重去访问的

原文地址:https://www.cnblogs.com/fengting0913/p/13467488.html