大发体育娱乐在线-大发体育娱乐官方网站-大发体育娱乐登录网址
做最好的网站

安装反代理访谈后端api,初创型企业

来源:http://www.dfwstonefabricators.com 作者:编程应用 人气:83 发布时间:2019-09-19
摘要:就算如此能够写静态的app了,可是最后app依然要走访后台接口才得以算的上的确的应用程序。 nginx的效用是让后端多个服务器来还要分担客商的流量,进而收缩单个服务器的压力。在自

就算如此能够写静态的app了,可是最后app依然要走访后台接口才得以算的上的确的应用程序。

nginx的效用是让后端多个服务器来还要分担客商的流量,进而收缩单个服务器的压力。在自动化持续交付的时候,我们不能够选取手工得办法来开展灰度宣布,即在nginx服务器上关闭发布服务器,平滑重启nginx。那时引用了consul,通过consul来动态的管制nginx后端服务器,无需平滑重启nginx也能在nginx上关闭后端服务器。

ionic有停放的服务器,我们不用把它放在tomcat、nginx等服务器才可运转,独有进行ionic serve,就可以在localhost:8100上访谈app了。

安装注重

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

自家的后端同事写了二个后端服务:通过id获取用户新闻,地址是http://10.83.11.43:8888/MobileInterface/service。首先假诺在app中一贯访谈这一个地点,像那样:

下载文件

cd /opt/software
wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip
wget http://nginx.org/download/nginx-1.11.5.tar.gz
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

unzip consul_0.7.1_linux_amd64.zip
unzip master.zip 
tar zxf nginx-1.11.5.tar.gz

图片 1Paste_Image.png

配置nginx

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx

但调整台马上给出了反响:

编写翻译安装nginx

./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre --add-module=../nginx-upsync-module-master
make && make install

图片 2Paste_Image.png

nginx配置文件

# /usr/local/nginx/conf/nginx.conf
#user  nobody;
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"'
              '$upstream_addr $upstream_status $upstream_response_time $request_time';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #
    vhost_traffic_status_zone;
    #proxy_cache_path /tmp/cache_backend keys_zone=cache_backend:10m;

    upstream test {
        server 127.0.0.1:11111;
        upsync 192.168.77.129:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
        server 192.168.77.140:8080 backup;
    }

    upstream bar {
        server 192.168.77.139:80 weight=1 fail_timeout=10 max_fails=3; 
    } 


    server {
        listen 80;

        location = / {
        proxy_pass http://test;
          proxy_set_header  Host  $host;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
          add_header    real $upstream_addr;
        }

        location ~ /Content|Scripts/ {
          proxy_pass http://test;
          proxy_set_header  Host  $host;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        location = /bar {
            proxy_pass http://bar;
        }

        location = /upstream_show {
            upstream_show;
        }

        location = /upstream_status {
            stub_status on;
            access_log off;
        }
    }
}

创建upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

能够看到localhost:8100拜候10.83.11.43:8888引人瞩目是跨域难题,所以大家需求经过配备反代理来化解难点。

安装consul

mkdir /usr/local/consul
mv consul /usr/local/consul/

很简单:

启动consul

/usr/local/consul/consul agent -server -bootstrap-expect=1 -data-dir=/consul_data -node=one1 -bind=192.168.77.129 -config-dir=/etc/consul.d -client 0.0.0.0 -ui

加上后端服务

curl -X PUT http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.140:80
curl -X PUT http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.142:80

闭馆后端服务

curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}' http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.140:80

张开后端服务

curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}' http://192.168.77.129:8500/v1/kv/upstreams/test/192.168.77.142:80

图片 3Paste_Image.png

启动nginx

nginx

只要在ionic.config.json中配置proxies品质就足以了,但要注意个中pathproxyUrl的值。大家的地点是ip:port/MobileInterface/service,但pathproxyUrl只需到/MobileInterface

查看consul web信息

图片 4

Paste_Image.png

图片 5

Paste_Image.png

接下来修改大家的乞请命名:

翻开通过consul增添的后端服务

cat /usr/local/nginx/conf/servers/servers_test.conf 
server 192.168.77.142:80 weight=2 max_fails=2 fail_timeout=10s;
server 192.168.77.140:80 weight=2 max_fails=2 fail_timeout=10s;

做客nginx,测量检验下是不是代理到后端服务

图片 6

Paste_Image.png

real地址正是后端服务器的地方。

图片 7Paste_Image.png

这下大家就能够正确访问后端接口了~

本文由大发体育娱乐在线发布于编程应用,转载请注明出处:安装反代理访谈后端api,初创型企业

关键词:

上一篇:没有了

下一篇:没有了

最火资讯