1. Install Nginx in Linux
0.上传nginx的tar包
1.安装nginx的依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2.创建nginx安装路径:
mkdir /usr/local/nginx
3.解压nginx
tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/nginx
4.编译nginx
4.1.进入nginx的文件目录
cd /usr/local/nginx/nginx-1.8.0
4.2.编译nginx
./configure
make
make install
5.开放nginx的80端口
5.1.开放80端口
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
5.2.开放的端口永久保存到防火墙
/etc/rc.d/init.d/iptables save
5.运行nginx
5.1.进入nginx的运行目录
cd /usr/local/nginx/sbin
5.2.运行nginx
./nginx
5.3.关闭
./nginx -s stop
5.4.重启
./nginx -s reload
2. Overview and configuration of Nginx:
Nginx是一款高性能的http服务器/反向代理服务器及电子邮件代理服务器。
官方测试nginx能够支撑"5万并发链接",并且cpu、内存等资源消耗却非常低,运行非常稳定。
作用:
2.1.发布静态网站
//在conf配置文件中,进行相关文件夹配置即可。
location / {
root 静态网站所在文件夹;
index index.html index.htm;
}
2.2.设置虚拟主机(其实就是可以在一台服务器上部署多个项目)
1.用虚拟目录区分
location / {
root hmly/;
index index.html index.htm;
}
location /b {
alias hmsc/; //可以使用绝对路径,在使用alias的时候,路径最后一定要有/
index index.html index.htm;
}
2.通过端口号区分
//复制server,修改listen端口号
server {
listen 80;
server_name localhost;
location / {
root hmly;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
location / {
root hmsc;
index index.html index.htm;
}
}
3.通过域名绑定
//复制server,修改server_name
#配置旅游网
server {
listen 80;
server_name www.aaa.com;
location / {
root aaa; // 静态网页包
index index.html index.htm;
}
}
#配置商城网
server {
listen 80;
server_name www.bbb.com;
location / {
root bbb; // 静态网页包
index index.html index.htm;
}
}
2.3.反向代理负载均衡
// tomcat集群
upstream server_lb{
server localhost:8080; // tomcat服务器1
server localhost:8081; // tomcat服务器2
server localhost:8082; // tomcat服务器3
}
server {
listen 80;
server_name localhost;
location / {
//代理
proxy_pass http://server_lb;
index index.html index.htm;
}
}