Nginx代理支持GRPC的反向代理和负载均衡配置

Nginx代理gRPC反向代理和负载均衡配置

linux版本安装

一、准备和安装
#指定目录安装( /home/yyt/nginxgrpc)
mkdir  /home/yyt/nginxgrpc
cd /home/yyt/nginxgrpc

# 1从nginx官网上获取版本号不低于1.13.10的源码包(nginx-1.17.9.tar.gz),本文以1.17.9为例。
wget https://nginx.org/download/nginx-1.17.9.tar.gz
# 解压并进入指定的安装目录
tar -zxvf nginx-1.17.9.tar.gz
# 注意:这只是源码(进入nginx源码目录中,执行如下命令)
cd nginx-1.17.9

# 首先需要先安装nginx 编译所需的基本依赖:
yum -y install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel

# 设置常量
# 编译和安装nginx(grpc的一定要有--with-http_ssl_module和--with-http_v2_module)
./configure \
--prefix=/home/yyt/nginxgrpc \
--sbin-path=/home/yyt/nginxgrpc/nginx \
--conf-path=/home/yyt/nginxgrpc/nginx.conf \
--pid-path=/home/yyt/nginxgrpc/nginx.pid \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream \
--with-http_v2_module

make && make install
二、查看和启动
# 完成后查看 检验是否安装成功
/home/yyt/nginxgrpc/nginx -v
#进入到nginx的安装目录下,检查nginx配置文件是否正确:
/home/yyt/nginxgrpc/nginx -t
#如果配置文件检查正常,运行nginx程序:
/home/yyt/nginxgrpc/nginx
#检查nginx运行状态及端口占用情况:(kill -9 进程)
ps -ef|grep nginx
#查看端口占用情况
netstat -anpo|grep 80
#重新载入配置
./nginx -s reload
# 停止
./nginx -s stop
# 浏览器访问地址
10.60.0.2:80
三、修改配置

nginx.conf(grpc反向代理和负载均衡配置实例)配置文件修改如下:

	# google grpc框架的图片存储应用(fastdfs)
	upstream grpcservers {
	    server 10.60.0.22:6002;
	    server 10.60.0.22:6003;
	}
	server {
	    listen 6001  http2;
	 
	    location / {
		grpc_pass grpc://grpcservers;
		error_page 502 = /error502grpc;
	    }
	 
	    location = /error502grpc {
		internal;
		default_type application/grpc;
		add_header grpc-status 14;
		add_header grpc-message "unavailable";
		return 204;
	    }
	}
	
	# 视频云检索应用
	upstream select{
	    server 10.60.0.22:10003;
	}

	server {
	    listen 10003;
	 
	    location / {
		proxy_pass http://select;
	    }
	}

全部:


user root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	upstream grpcservers {
	    server 10.60.0.22:6002;
	    server 10.60.0.22:6003;
	}
	server {
	    listen 6001  http2;
	 
	    location / {
		grpc_pass grpc://grpcservers;
		error_page 502 = /error502grpc;
	    }
	 
	    location = /error502grpc {
		internal;
		default_type application/grpc;
		add_header grpc-status 14;
		add_header grpc-message "unavailable";
		return 204;
	    }
	}

	upstream select{
	    server 10.60.0.22:10003;

	}

	server {
	    listen 10004;
	
	    location / {
		proxy_pass http://select;
	    }

	}

    server {
        listen       8085;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

四、启动应用测试
nohup java -jar /home/yyt/java-project/video_structurization_api/test/grpc-pic-server.jar --server.port=10013 --grpc.service.port=6002 --spring.profiles.active=dev >/dev/null 2>&1  &

测试:http://10.60.0.22:10004/vehicle/export/5fb5294a46e0fb000141d280

Nginx安装完成,没有生成sbin目录

一定要区分个人用户和root用户,通过root用户执行Nginx安装命令,最终生成的nginx文件夹应该在root根目录,而不是在个人用户下

//root根目录
/usr/local/nginx
//个人用户目录
/home/wanjk/usr/local/nginx/

docker版本安装

使用dockerfile方式构建镜像

一、准备
mkdir /home/yyt/nginx-grpc -p

#进入nginx目录,创建nginx测试页,创建dockerfile文件
cd /home/yyt/nginx-grpc/

# 创建测试页面
vim index.html
<h1> nginx test mypage </h1>

# 编写dockerfile
vim dockerfile
二、编写dockerfile

路径不要轻易在此修改了,很容器错,容器内的使用这个路径即可

vim dockerfile

FROM centos:7
MAINTAINER "showlin"
WORKDIR /usr/local/src
#定义环境变量(不是必须的)
ENV NG_VERSION nginx-1.17.9

# RUN yum -y install epel-release
#下载nginx文件并解压
RUN yum -y install wget && wget http://nginx.org/download/$NG_VERSION.tar.gz && tar -xzvf $NG_VERSION.tar.gz
# 下载好了就直接解压
#ADD $NG_VERSION.tar.gz /usr/local/src

#安装编译依赖包
RUN yum -y install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
#清理仓库
RUN yum clean all 
#创建nginx用户
RUN useradd -M -s /sbin/nologin nginx 

#切换工作目录进行编译
WORKDIR /usr/local/src/$NG_VERSION 

#编译安装nginx
RUN ./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream \
--with-http_v2_module && make && make install

# /usr/local/nginx/conf/nginx.conf 也是容器内映射 到宿主机的地址(随便定)
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf

#复制测试页面到容器中
#ADD index.html /usr/local/src/$NG_VERSION/html
ADD index.html /usr/local/nginx/html
#设置容器中要挂在到宿主机的目录
VOLUME /usr/local/src/$NG_VERSION/html
#设置sbin环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#暴露80端口 若端口这里没有配置,后面nginx的conf文件中也支持新添加的
EXPOSE 80
EXPOSE 6001
EXPOSE 10003
# ENTRYPOINT和CMD连用时相当于ENTRYPOINT的参数
ENTRYPOINT ["nginx"] 

注释不要写在同一行,否则会认为是参数

一定注意安装目录:使用默认安装的目录

三、dockerfile文件制作nginx的镜像:
# 移除容器
docker rm -f nginx-grpc
# 移除镜像
docker rmi jgh/nginx-grpc:v1.17.9
#构建镜像
docker build ./ -t jgh/nginx-grpc:v1.17.9
# 运行容器
docker run -d -p 8085:81 --name nginx1 nginx-grpc:v1.17.9

#创建文件夹
mkdir /home/yyt/nginx-grpc/conf
#拷贝配置文件
docker cp nginx-grpc:/usr/local/nginx/conf/nginx.conf /home/yyt/nginx-grpc/conf/nginx.conf

# 创建并启动容器 10003已经有端口了,会报端口占用,所以改为其10004
docker run -d -p 6001:6001 -p 10004:10003 -p 80:80 \
--name nginx-grpc \
--restart=always \
-v /home/yyt/nginx-grpc/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf jgh/nginx-grpc:v1.17.9

# 查看
docker ps -a |grep nginx-grpc
# 进入
docker exec -it nginx-grpc /bin/bash

#将容器做出新的镜像(但是修改的nginx.conf不会一起保存容器镜像时保存,还是需要重新配置)
#docker commit jgh/nginx-grpc:v1
docker save -o nginx-grpc.tar jgh/nginx-grpc:v1 

测试:http://10.60.0.22:10004/vehicle/export/5fb5294a46e0fb000141d280

参考:
  • 反向代理和负载均衡配置

  • nginx安装到指定目录

  • linux安装nginx到指定用户目录下

  • Docker镜像管理2之nginx镜像制作

  • Docker 制作Nginx镜像
    nginx HTML网页乱码

你可能感兴趣的:(linux,nginx,docker,nginx,docker,linux)