Nginx

1. 什么是Nginx

Nginx

  • 俄罗斯人开发的一个Web服务器软件。
  • Nginx 具有极好的并发性能!资源占用低。
  • 用于解决高并发问题
    • 利用Nginx与应用服务器组建反向代理负载均衡服务器集群。
    • Nginx 充当前置代理机
  • 据测算Nginx本身能够支持500000并发!

2. Web Server

  • 开源Web Server(Windows Linux Unix)
    • Nginx Apache
  • 商业服务器
    • MS IIS Server (Windows)
  • Java
    • 开源 Tomcat Jetty JBoss
    • 商业 IBM WebSpare, Oracle WebLogic

3. 安装Nginx

3.1 使用yum 安装nginx

  1. 安装

    yum install nginx

  2. 启动

    systemctl start nginx.service

  3. 配置文件

    /etc/nginx/nginx.conf

3.2 使用源码安装

  1. 下载源码:

    http://nginx.org/en/download.html 找到下载地址
    wget http://nginx.org/download/nginx-1.12.2.tar.gz

  2. 编译, 需要安装 gcc openssl 包

    tar -zxvf nginx-1.12.0.tar.gz

    1
    2
    3
    4
    5
    useradd nginx
    cd nginx-1.12.0
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
    make
    make install
  3. 启动nginx(使用root用户)

    cd /usr/local/nginx/sbin
    ./nginx -c /usr/local/nginx/conf/nginx.conf

  4. 检查Nginx是否正在运行

    ps -A | grep nginx
    netstat -utnalp | grep 80

  5. 配置防火墙,开启 80 端口

    firewall-cmd –permanent –add-port=80/tcp
    firewall-cmd –reload

  6. 再另外一个电脑上用浏览器访问服务器

4. Nginx 的配置文件

编译安装的配置文件:

/usr/local/nginx/conf/nginx.conf

yum安装的配置文件:

/etc/nginx/conf/nginx.conf

Nginx 配置文件结构:

通用参数
http{
    http 参数

    server{
        虚拟机参数
        location{
            文件位置
        }
    }

    server{
        ...
    }
}

5. 通用参数

user  nginx;  //Nginx的启动用户
worker_processes  1; //Nginx 进程数量,建议按照处理器个数设置

error_log  logs/error.log; //错误日志文件
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid; //保存nginx进程号的文件

events {
    worker_connections  2048; //每个进程可以开启的线程数
}

nginx配置文件中的#表示注释

6. nginx HTTP通用参数

include       mime.types;  //配置文件任何位置都可以使用include
default_type  application/octet-stream;
access_log  logs/access.log  main; //保存访问日志
sendfile        on;  //发生文件
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65; //设置服务器保持连接等待时间
#gzip  on;	//支持响应压缩技术

7. nginx 虚拟主机设置

虚拟主机: 复用同一台服务器,为多个网站提供服务。 对于客户端干就是两个主机一样。

虚拟主机方式有3种

  1. 基于IP的虚拟主机
  2. 基于端口的虚拟主机
  3. 基于域名的虚拟主机,这个方式最常用的方式。

案例测试步骤:

  1. 客户端利用hosts文件模拟解析域名,实际工作中可以利用DNS服务解析域名:

    修改客户端/etc/hosts文件,添加:

    192.168.22.21   laowang.com
    192.168.22.21   laoli.com
    
  2. 修改服务器nginx配置文件

    修改服务器端 /usr/local/nginx/conf/nginx.conf

    添加:

    server {
        listen       80;
        server_name  laowang.com;
        charset      utf-8;
        location / {
            root    web1;
            index   index.html  index.htm;
        }
    }
    
    server {
        listen       80;
        server_name  laoli.com;
        charset      utf-8;
        location / {
            root    web2;
            index   index.html  index.htm;
        }
    }
    
  3. 利用nginx 测试配置文件:

    nginx -t /usr/local/nginx/conf/nginx.conf

  4. 重写加载配置文件:

    nginx -s reload

    或者关闭再重写启动
    
    nginx -s stop
    nginx -c /usr/local/nginx/conf/nginx.conf
    
  5. 客户端测试,利用浏览器访问域名或者:

    wget http://laowang.com/index.html
    wget http://laoli.com/index.html

8. 在阿里云配置Nginx虚拟主机

  1. 配置域名解析:

  解析两个域名
  t1.canglaoshi.org  -->  ip
  t2.canglaoshi.org  -->  ip
  1. 安装 nginx

    1
    yum install nginx
  2. 修改nginx配置文件 /etc/nginx/nginx.conf:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    server {
    listen 80;
    server_name t1.canglaoshi.org;
    location / {
    root web1;
    index index.html index.htm;
    }
    }

    server {
    listen 80;
    server_name t2.canglaoshi.org;
    location / {
    root web2;
    index index.html index.htm;
    }
    }

  3. 添加文件夹和文件

    1
    2
    3
    cd /usr/share/nginx
    mkdir web1
    mkdir web2

    添加文件 web1/index.html

    1
    Hello web1!

    添加文件 web2/index.html

    1
    Hello web2!
  4. 测试配置

    1
    nginx -t -c /etc/nginx/nginx.conf
  5. 启动nginx

    1
    systemctl start nginx.service
  6. 停止

    1
    systemctl stop nginx.service
  7. 重启动

    systemctl restart nginx.service

  8. 浏览器测试:

    1
    2
    http://t1.canglaoshi.org
    http://t2.canglaoshi.org

9. HTTPS 虚拟主机

支持HTTPS加密的虚拟主机,应用十分广泛:

其原理是在SSL加密的TCP通信基础上进行 HTTP 通信:

服务器配置步骤:

  1. 重新编译nginx增加ssl模块

    1
    2
    3
    4
    5
    useradd nginx
    cd /usr/local/nginx/nginx-1.12.0
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
    make
    make install
  2. 更新nginx.conf增加ssl主机配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server {
    listen 443;
    server_name tts.canglaoshi.org;
    ssl on;
    ssl_certificate cert/214438499540580.pem;
    ssl_certificate_key cert/214438499540580.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
    root web3;
    index index.html index.htm;
    }
    }
  3. 下载 证书文件 cert.zip 释放 到 /usr/local/nginx/conf/cert

    1
    2
    3
    /usr/local/nginx/conf/cert
    |-- 214438499540580.pem
    |-- 214438499540580.key
  4. 添加web文件

    1
    /usr/local/nginx/web3/index.html
  5. 在防火墙上开启端口 443

    1
    2
    firewall-cmd --permanent --add-port=443/tcp
    firewall-cmd --reload
  6. 测试nginx配置文件

    1
    nginx -t -c /usr/local/nginx/conf/nginx.conf
  7. 关闭nginx重新启动

    1
    2
    nginx -s stop
    nginx -c /usr/local/nginx/conf/nginx.conf
  8. 修改客户端 hosts 文件解析域名

    1
    192.168.22.21  tts.canglaoshi.org
  9. 用浏览器测试

    https://tts.canglaoshi.org
    或者
    wget https://tts.canglaoshi.org/index.html

9.1 添加用户功能

命令:

useradd 用户名

案例:

useradd tom
cat /etc/passwd|grep tom

9.2 为用户设置密码:

命令

passwd 用户名

案例:

passwd tom

9.3 删除用户

命令

userdel 用户名

案例:

userdel tom

10. 反向代理

利用服务器代理用户上网称为正向代理,相反利用服务器代理应用服务器提供网络服务称为反向代理。

搭建反向代理服务器群的目的就是为了解决“高并发”问题。

11. Nginx反向代理集群搭建

搭建环境设计:

nginx

搭建步骤:

  1. 配置并且启动Tomcat服务器

    1. 更新Tomcat的首页文件 tomcat/webapps/ROOT/index.jsp
    2. 设置Tomcat启动脚本执行权限: chmod +x tomcat/bin/*.sh
    3. 在防火墙上开启 8080 端口
    4. 启动Tomcat tomcat/bin/startup.sh
    5. 检查tomcat是否成功提供服务
  2. 配置第二台tomcat

  3. 更新添加集群配置文件 toms.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
upstream toms {
server 192.168.27.113:8080;
server 192.168.27.140:8080;
}

server {
listen 80;
server_name toms.canglaoshi.org;

access_log logs/toms.log;
error_log logs/toms.error.log;

index index.html index.htm index.jsp ;

## send request back to apache ##
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_pass http://toms;

}
}
  1. 更新 nginx 配置文件nginx.conf,引用toms.conf

    1
    include toms.conf;	
  2. 测试 nginx 配置文件

    1
    nginx -t -c /usr/local/nginx/conf/nginx.conf

    如果测试出现问题,请根据提示更正

  3. 重新启动Nginx

    1
    nginx -s reload
  4. 更新客户端hosts文件,解析域名

    1
    192.168.27.63 toms.canglaoshi.org

    windows的hosts文件位置: C:/Windows/system32/driver/etc

  5. 利用浏览器访问,测试

    1
    http://toms.canglaoshi.org

12. Nginx 的负载均衡分配方案

Nginx 包含4种分配策略

  1. 轮询(默认)逐个转发请求
    • 可以指定权重,权重大的服务器分担的任务重
    • weight 指定权重
  2. ip_hash 根据客户端的IP地址分配固定的服务器
    • 可以将用户与服务对应有利于保持Session
  3. fair:需要使用 fair模块
  4. url_hash: 第三方模块,根据URL映射到固定服务器

ip_hash 原理:

ip-hash

12.1 服务器配置

配置轮询权重

1
2
3
4
upstream toms {
server 192.168.27.113:8080 weight=10;
server 192.168.27.140:8080 weight=20;
}

撤掉服务器

1
2
3
4
upstream toms {
server 192.168.27.113:8080 weight=10;
server 192.168.27.140:8080 down;
}

备份服务器

1
2
3
4
upstream toms {
server 192.168.27.113:8080 weight=10;
server 192.168.27.140:8080 backup;
}

失败时间

1
2
3
4
5
upstream toms {
server 192.168.27.113:8080 weight=10;
server 192.168.27.140:8080 ;
fail_timeout 30;
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 高行行
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信