logo头像

分享技术,品味人生

瑞吉实战13-nginx集成部署

瑞吉实战13–nginx集成部署

实验内容

  • nginx安装

  • nginx常用命令(日常运维)

  • nginx配置文件多种实战!

  • nginx性能优化

一、nginx安装

补充:环境准备

# 克隆并开机后修改ip
cat /etc/sysconfig/network-scripts/ifcfg-ens192 
/etc/init.d/network restart

# 修改主机名,便于识别
hostnamectl set-hostname nginx1
bash

nginx官网下载最新的稳定版,安装到/usr/local/nginx下

安装过程:
1、安装依赖包 yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
2、下载Nginx安装包wget https://nginx.org/download/nginx-1.22.0.tar.gz
3、解压 tar -zxvf nginx-1.16.1.tar.gz
4、cd nginx-1.16.1
5、./configure --prefix=/usr/local/nginx
6、make && make install
7、环境变量配置
echo 'NGINX_HOME=/usr/local/nginx'>>~/.bash_profile 
echo 'PATH=$PATH:$NGINX_HOME/sbin'>>~/.bash_profile 
echo 'export NGINX_HOME PATH'>>~/.bash_profile 
source ~/.bash_profile   

web访问OK

image-20220920165749391

二、nginx常用命令使用

nginx 日常主要使用启停、刷新配置、查看进程的操作。

#查看Nginx版本可以使用命令:
./nginx -v

# 在启动Nginx服务之前,可以先检查一下conf/nginx.conf文件配置的是否有错误,命令如下:
./nginx -t

# 启动Nginx服务使用如下命令:
./nginx

# 停止Nginx服务使用如下命令:
./nginx -s stop

# 启动完成后可以查看Nginx进程:
ps -ef | grep nginx

# 当修改Nginx配置文件后,需要重新加载才能生效,可以使用下面命令重新加载配置文件:
./nginx -s reload

三、nginx配置文件多种实战!

nginx的conf文件主要分为三个区块:

  • 全局块,nginx运行所需的全局配置
  • events,网络连接相关的配置
  • http,代理、缓存、日志记录、虚拟主机配置

部署静态资源

相对比较简单

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; 
    keepalive_timeout  65;

    server {
        listen       80;  # 监听的端口
        server_name  localhost; #服务器名称
        location / { #匹配客户端请求url
            root   html; #指定静态资源根目录
            index  index.html index.htm; #指定默认首页
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

测试1:上传静态资源hello.html到html文件夹 并进行访问测试

image-20220920171828370

测试2:修改index默认配置,做配置文件检查,检查OK后重载配置,测试OK!

image-20220920171929786

image-20220920171936576

image-20220920171956414

反向代理

正向代理比如用户IE流浪器的代理服务器设置

反向代理就是透明的,配置在服务端,用户无感知!只需要知道入口即可

# nginx.conf 中http块增加配置
    server {
        listen       81;
        server_name  localhost;

        location / {
            proxy_pass http://192.168.20.142:8080; #反向代理
        }
    }
    
# 验证配置文件OK,加载配置,并测试
nginx -t
nginx -s reload

非常的奈斯!

image-20220921083813560

负载均衡

采用【SpringBoot】章节中的负载均衡代码,idea中进行maven打包,上传到两台tomcat服务器中启动

java -jar SpringBootHelloWorld-0.0.1-SNAPSHOT.jar 

独立访问也是OK的

image-20220921094426779

nginx配置调整

# nginx.conf http模块
    upstream testserver{
        server 192.168.20.142:8080;
        server 192.168.20.143:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://testserver;         
        }
    }
    
[root@nginx1 conf]# 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@nginx1 conf]# nginx -s reload

负载均衡生效,但session中的count不会自增

image-20220921095020750

Nginx会话保持技术1:采用ip_hash

    upstream testserver{
        ip_hash;
        server 192.168.20.142:8080;
        server 192.168.20.143:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://testserver;
        }
    }

效果达标,但可能负载均衡的效果不够,业界一般常用sticky模块!

Nginx会话保持技术2:采用sticky模块

# 停止nginx
nginx -s stop
ps -ef|grep nginx

# 下载sticky模块,对nginx进行重新编译
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
tar zxvf master.tar.gz 
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ /usr/local/nginx-sticky-module
cd nginx-1.22.0
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-module --with-http_stub_status_module --with-http_ssl_module

# 安装并测试
make && make install
nginx -V

# nginx.conf 开启sticky配置,并测试

效果很棒!同一电脑,两个浏览器访问,完全实现负载均衡!

image-20220921100929969

参考资料: (30条消息) Nginx核心要领六:Nginx添加第三方模块(nginx-sticky-module)_闪耀的瞬间的博客-CSDN博客_nginx-sticky-module

虚拟主机

# 两种配置都是OK的!
    server {
        listen  80;
        server_name     www.test1.com;
        location / {
                proxy_pass http://192.168.20.142:8080;
        }
    }

    upstream testserver2{
        sticky;
        server 192.168.20.143:8080;
    }

    server {
        listen  80;
        server_name     www.test2.com;
        location / {
                proxy_pass http://testserver2;
        }
    }
    
# 服务器本地hosts配置,方便测试
echo "127.0.0.1 www.test1.com www.test2.com" >> /etc/hosts
curl http://www.test1.com/hello
# hello world 192.168.20.142 1
curl http://www.test2.com/hello
# hello world 192.168.20.143 1

注:curl刷两台操作session的count不会自增,用浏览器是ok的,应该是需要cookie等浏览器组件支持才是完整的!

浏览器测试配置可能需要本地hosts文件支持,打开C:\Windows\System32\drivers\etc\hosts配置即可!

规范配置

# conf下创建统一目录,文件名以站点起名、conf结尾
mkdir /usr/local/nginx/conf/vhosts

# nginx.conf 配置
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    include vhosts/*.conf;
}




# 独立配置
[root@nginx1 nginx]# cat conf/vhosts/testserver.conf 
    server {
        listen  80;
        server_name     www.test1.com;
        location / {
                proxy_pass http://192.168.20.142:8080;
        }
    }

    upstream testserver2{
        sticky;
        server 192.168.20.143:8080;
    }

    server {
        listen  80;
        server_name     www.test2.com;
        location / {
                proxy_pass http://testserver2;
        }
    }

四、nginx性能优化

压缩和缓存

# nginx.conf http块中增加下列配置
gzip  on;#开启gzip压缩
gzip_min_length 1k; #小于 1024 字节的文件不要压缩了,意义不大,还要消耗cpu资源
gzip_buffers 4 16k;
gzip_comp_level 9;  #压缩级别1-10,数字越大压缩的越好,时间也越长,看心情随便改吧
gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;  

# 创建测试文件
sudo truncate -s 1k /usr/local/nginx/html/ruiji/test.html
sudo truncate -s 1k /usr/local/nginx/html/ruiji/test.jpg
sudo truncate -s 1k /usr/local/nginx/html/ruiji/test.css
sudo truncate -s 1k /usr/local/nginx/html/ruiji/test.js

# 开启配置的前后测试,查看最后返回是否有 Content-Encoding: gzip
curl -H "Accept-Encoding: gzip" -I http://localhost/test.html
curl -H "Accept-Encoding: gzip" -I http://localhost/test.jpg
curl -H "Accept-Encoding: gzip" -I http://localhost/test.css
curl -H "Accept-Encoding: gzip" -I http://localhost/test.js

这里是用Edge的F12开发工具,查看开启前后变化

image-20220921142602519

ab测试情况

测试文件10K,500并发,10w总请求,局域网无带宽瓶颈,测试机、服务器配置都是2C/4G。

ab -c 500 -n 100000 http://192.168.20.142:8080/test.html

注:ab工具可参看相关章节

c=500,n=100000 qps 用户平均 总数平均
nginx,无优化 23534 21.245 0.042
tomcat,无优化 24540 20.375 0.041
nginx,开启gzip 25973 19.25 0.039
nginx,开gizp,调高进程2 35474 14.095 0.028
nginx,开gizp,调高进程4 37872 13.202 0.026

image-20220921172101489

注:通过netdata可以可以看出CPU是nginx性能主要瓶颈!虽然时间短,整体效果OK,基本够用

image-20220921171840822

tomcat 也做了3次压测,指标跟nginx有些差距,差异估计是在网络处理这块,明显吞吐量、cpu负荷还是少了些。

进程和连接数优化

限制并发数、每秒连接数

防盗链

评论系统未开启,无法评论!