nginx 转发 Springboot nginx转发netty_nginx


看上图,鼠标右键-在新标签中打开图片食用

内容分为三部分:

nginx转发

OpenResty(nginx升级版)_web服务器+lua

测试

1.nginx转发

1.1.搭建nginx

略; 翻阅其他文章;window直接安装exe, linux翻翻博客, docker 拉个nginx镜像再翻翻博客。

1.2. nginx转发配置

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

    upstream nginx-cluster{
        server 132.232.xxx.xxx:8081;
    }
    server {
        listen       80;
        server_name  localhost;

        location /api {
            proxy_pass http://nginx-cluster;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
这个配置是nginx conf文件的配置
1.upstream 是做负载均衡的,也可以写多个server
upstream nginx-cluster{
        server 132.232.xxx.xxx:8081;
        server 132.232.xxx.xxx:8082;
}

这样写即可。

其中nginx-cluster就是一个变量,叫什么都可以,不过配上了之后,nginx在做反向代理时可以代理到指定ip:端口,也可以代理到这个变量。这个变量会被负载均衡器自动分配到对应的服务上去。

2. http
  • nginx监听着http的请求
3. listen 80;
  • nginx转发服务器监听着80端口
4. location /api { proxy_pass http://nginx-cluster; }
  • http://nginx转发服务器地址:80/api就会发送到nginx负载均衡器那里,从而转发给指定的服务器上的进程(服务)。

1.3. 启动nginx

  • window执行nginx.exe
  • linux 执行启动命令
  • 进入nginx/sbin目录

cd /usr/local/nginx/sbin/

  • 启动nginx

./nginx

  • docker run容器即可;

1.4. 启动,关闭,重启,命令

  • ./nginx //启动
  • ./nginx -s stop //关闭
  • ./nginx -s reload
  • ./代表是当前目录,前提是进入了nginx命令那个目录xxx/xxx/nginx/sbin
  • 配了环境变量就不需要./了。 --------有些啰嗦了。

2. OpenResty(nginx升级版)_web服务器+lua

2.1. OpenResty web服务器

1.简介

OpenResty ® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty ® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty ® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

2.安装
2.1首先你的Linux虚拟机必须联网
2.2安装开发库
yum install -y pcre-devel openssl-devel gcc --skip-broken
2.3安装OpenResty仓库
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
如果提示说命令不存在,则运行:
yum install -y yum-utils
然后再重复上面的命令
2.4安装OpenResty
yum install -y openresty
2.5安装opm工具
opm是OpenResty的一个管理工具,可以帮助我们安装一个第三方的Lua模块。
yum install -y openresty-opm
2.6目录结构
默认情况下,OpenResty安装的目录是:/usr/local/openresty
2.7配置nginx的环境变量(它本质还是nginx只是多了些插件)
vi /etc/profile
在最下面加入两行:
export NGINX_HOME=/usr/local/openresty/nginx
export PATH=${NGINX_HOME}/sbin:$PATH

# NGINX_HOME:后面是OpenResty安装目录下的nginx的目录

然后让配置生效:
source /etc/profile
3.启动和运行
3.1 OpenResty底层是基于Nginx的,查看OpenResty目录的nginx目录,结构与windows中安装的nginx基本一致:
# 启动nginx
nginx
# 重新加载配置
nginx -s reload
# 停止
nginx -s stop
3.2 nginx的默认配置文件注释太多,影响后续我们的编辑,这里将nginx.conf中的注释部分删除,保留有效部分。修改`/usr/local/openresty/nginx/conf/nginx.conf`文件,内容如下:
#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

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

    server {
        listen       8081;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
3.3 nginx的默认配置文件注释太多,影响后续我们的编辑,这里将nginx.conf中的注释部分删除,保留有效部分。修改`/usr/local/openresty/nginx/conf/nginx.conf`文件,内容如下:
#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

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

    server {
        listen       8081;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
3.4在Linux的控制台输入命令以启动nginx
3.5然后访问页面:http://132.232.xxx.xxx:8081,注意ip地址替换为你自己的虚拟机IP:
4.后续步骤(在nginx的conf文件中进行配置lua拦截请求)
4.1.加载OpenResty的lua模块:
#lua 模块
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
#c模块  
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
4.2拦截指定请求(这里拦截带/api/item的请求)交给lua进行处理
location /api/item {
            # 默认的响应类型
            default_type application/json;
            # 响应结果由lua/item.lua文件来决定
            content_by_lua_file lua/item.lua;
        }
4.3完成配置后,配置文件长这样
#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #lua 模块
    lua_package_path "/usr/local/openresty/lualib/?.lua;;";
    #c模块  
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";

    server {
        listen       8081;
        server_name  localhost;
        location /api/item {
            # 默认的响应类型
            default_type application/json;
            # 响应结果由lua/item.lua文件来决定
            content_by_lua_file lua/item.lua;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.2. Lua

1.根据OpenResty的nginx conf创建lua文件
  • nginx conf中写到 content_by_lua_file lua/item.lua;
  • 意味着需要在nginx目录下创建一个lua目录,在lua目录下创建一个item.lua的文件
  • cd /usr/local/openresty/nginx
  • mkdir lua
  • touch lua/item.lua
2.编写item.lua 文件
  • vi item.lua(或者用其他文本编辑工具都可)
  • 键入代码
ngx.say('{"id":10001,"name":"SALSA AIR","title":"RIMOWA 27寸托运箱拉杆箱 SALSA AIR系列果绿色 820.70.36.4","price":19900,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t6934/364/1195375010/84676/e9f2c55f/597ece38N0ddcbc77.jpg!q70.jpg.webp","category":"拉杆箱","brand":"RIMOWA","spec":"","status":1,"createTime":"2019-04-30T16:00:00.000+00:00","updateTime":"2019-04-30T16:00:00.000+00:00","stock":2999,"sold":31290}')
  • 这段代码的意思是,以lua文件,向外发送json数据,ngx.say('')里面是一个json数据
  • 重新加载nginx配置
  • nginx -s reload

3.测试

  • 从nginx转发服务器上,访问http://localhost/api/item/10086
  • 返回的是openResty以lua的方式返回的json数据
  • 即从nginx转发到openRestyWeb服务器以lua做controller支撑返回数据就成功了