1.nginx简介
- nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。
- nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。所以现在国内很多大型网站用户都在使用nginx。
2.nginx的优点
- 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
- 内存消耗少:在3万并发连接下,开启10个nginx进程才消耗150M内存
- 成本低廉:nginx为开源软件,可以免费使用。
- 支持Rewrite重写规则:能够根据域名,URL的不同,将HTTP请求分到不同的后端服务器群组
- 内置的健康检查功能:如果Nginx Proxy后端的某台web服务器宕机了,不会影响前端的访问。
- 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
- 稳定性高:用于反向代理,宕机的概率极小
- 模块化设计:模块可以动态编译
- 支持热部署:可以不停机重载配置文件
3.nginx的安装与配置
//创建系统用户nginx
[root@hxdserver ~]# groupadd -r nginx
[root@hxdserver ~]# useradd -r -M -s /sbin/nologin nginx -g nginx
//安装依赖环境
[root@hxdserver ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@hxdserver ~]# yum -y groups install 'Development Tools'
//创建日志存放目录
[root@hxdserver ~]# mkdir -p /var/log/nginx
[root@hxdserver ~]# chown -R nginx.nginx /var/log/nginx/
//下载nginx
[root@hxdserver ~]# cd /usr/src/
[root@hxdserver src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-08-20 11:19:09-- http://nginx.org/download/nginx-1.14.0.tar.gz
[root@hxdserver src]# ls
debug kernels nginx-1.14.0.tar.gz
//编译安装
[root@hxdserver src]# tar xf nginx-1.14.0.tar.gz
[root@hxdserver nginx-1.14.0]# cd nginx-1.14.0/
[root@hxdserver nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@hxdserver nginx-1.14.0]# make && make install
//nginx安装后配置
//设置环境变量
[root@hxdserver ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@hxdserver ~]# . /etc/profile.d/nginx.sh
//启动nginx
[root@hxdserver ~]# nginx
[root@hxdserver ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//关闭防火墙
[root@hxdserver ~]# systemctl stop firewalld.service
在浏览器中访问服务器IP
写一个简单的html网页做测试
//进去存放网页的位置
[root@hxdserver html]# cd /usr/local/nginx/html/
[root@hxdserver html]# vim 8.8.html
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin: 0;
padding: 0;
}
.a{width: 100%;
height: 100px;
background-color: black;
position: fixed;
}
.b{text-align: center;
position: absolute;
right: 40px;
top: 50px;
color: #fff;
font-size: 20px;
margin-top: -10px;
font-family:微软雅黑;
}
span{margin: 0 30px;
display: inline-block;
}
.c{ width:100%;
height:100px;
position:fixed;
bottom:0;
background-color:#000;
line-height:100px;
text-align:center;
font-family:微软雅黑;
}
ul li{
display:inline-block;
margin:0 30px;
color:#fff;
}
a:link,a:visited,a:hover,a:active{
text-decoration: none;
color:#fff;
}
.a1{
width: 100%;
height:700px;
background-image: url(8.png);
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
</head>
<body>
<div class="a">
<div class="b">
<span><a href="#">嘻嘻</a></span>
<span><a href="#">哈哈</a></span>
<span><a href="#">tom</a></span>
<span><a href="#">jerry</a></span>
<span><a href="#">笔记</a></span>
</div>
</div>
<div class="a1"></div>
<div class="c">
<ul>
<li><a href="">网站首页</a></li>
<li><a href="">企业合作</a></li>
<li><a href="">人才招聘</a></li>
<li><a href="">联系我们</a></li>
<li><a href="">常见问题</a></li>
<li><a href="">友情链接</a></li>
</ul>
</div>
</body>
</html>
//修改配置文件,如果不改就在服务器主机IP后加上网页文件的名字,也可以访问
[root@hxdserver conf]# vim nginx.conf
location / {
root html;
index 8.8.html index.htm; //修改
}
//重新加载配置文件
[root@hxdserver conf]# nginx -s reload
再次验证