前段时间公司根据要求需要将聚石塔上服务器从杭州整体迁移到张家口,刚好趁这次机会将这些乱七八糟的服务器做一次梳理和整合,断断续续一个月迁移完

成大概优化掉了1/3的机器,完成之后遇到了一些问题,比如曾今零零散散部署在生产上一些可视化UI:apollo,kibana,grafana,jenkins 等等要么采用80端口,要

么对公开放了其他端口,为了安全,现在不再开放非80之外的公网端口,由于机器少了,80端口不够,这些可视化UI不再能直接访问到了。所以需另寻其他出路。

一:用nginx做反向代理

为了解决这两个问题,自然第一反应想到的就是使用反向代理,我的理想构思下应该是下图这样的。

既用户所有的请求都经过nginx,让nginx来判断当前url需要跳转到哪一个后端代理上,比较好的策略应该是让nginx来判断当前的host是什么来决定跳转到后端

的哪一个webserver上,比如a.mip.com 就跳转到apollo,j.mip.com 就跳转到jenkins. 以此类推,这样就可以完美解决了,是吧? 在nginx中你完全可以使用rewrite

模块下if指令来进行判断。

二:使用if指令

这里要提一下,nginx比较原始化,如果需使用第三方module,你还需要重新编译nginx,用起来很麻烦,所以这里干脆使用OpenResty,它扩展了nginx,并且

集成了很多成熟的lua模块,自行下载最新的1.15.8,安装方式和nginx一模一样。

默认是安装到/usr/local/目录下,当你看到有一个openresty目录表示你安装成功。

[root@localhost local]# ls
bin etc games include lib lib64 libexec openresty sbin share src
[root@localhost local]# pwd
/usr/local

接下来你可以使用 nginx  -v 来看一下openresty版本号啥的。

[root@localhost sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@localhost sbin]#
[root@localhost sbin]# ./nginx -v
nginx version: openresty/1.15.8.1

为了方便,我就直接使用nginx开启三个server:

<1>  192.168.23.129:80       nginx上开启的第一个网站,就是proxy了。

<2>  192.168.23.129:8001      nginx上开启的第二个网站,模拟apollo。

<3>  192.168.23.129:8002      nginx上开启的第三个网站,模拟jenkins。

1.  apollo的模拟:

    server {
listen ;
server_name somename alias another.alias;
location / {
root html;
index apollo.html;
}
}

8001端口网站的默认页是apollo.html,这个apollo.html所在路径就是在nginx下的html目录,如下所示。

[root@localhost html]# pwd
/usr/local/openresty/nginx/html
[root@localhost html]# ls
50x.html apollo.html index.html jenkins.html

2. jenkins的模拟

    server {
listen ;
server_name somename alias another.alias;
location / {
root html;
index jenkins.html;
}
}

jenkins.html的文件所在路径如上所示哈。不再赘述。

3. proxy的模拟

    server {
listen ;
server_name localhost; location / { if ($host = "a.mip.com") {
proxy_pass http://localhost:8001;
} if ($host = "j.mip.com") {
proxy_pass http://localhost:8002;
}
}

可以看到,只需要使用rewrite模块下的if条件语句,通过$host系统变量判断当前的url中的host的值跳转到相应的网站。

4. host映射

好了,接下来只需要将a.mip.com 和 j.mip.com 映射到nginx的ip地址192.168.23.129即可。因为这些域名方便记忆而不是真实存在的。

192.168.23.129 a.mip.com
192.168.23.129 j.mip.com

5. 启动nginx

[root@localhost sbin]# ./nginx
[root@localhost sbin]#
[root@localhost sbin]#
[root@localhost sbin]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd
tcp 127.0.0.1: 0.0.0.0:* LISTEN /master
tcp6 ::: :::* LISTEN /sshd
tcp6 ::: :::* LISTEN /master

通过上图可以看到,80,8001,8002 端口都已经开启了,接下来大家可以到浏览器去验证一下了。

可以看到这个问题已经很完美的解决了,好了,这就是本篇和大家聊到的实际场景中遇到的一个问题,希望本篇对你有帮助,以下是全部的nginx.conf。

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$host ----> $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 ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # location = /get {
# set_unescape_uri $key $arg_key; # this requires ngx_set_misc
# redis2_query get $key;
# redis2_pass 10.105.13.174:;
# } location / { if ($host = "a.mip.com") {
proxy_pass http://localhost:8001;
} if ($host = "j.mip.com") {
proxy_pass http://localhost:8002;
} root html;
index index.html index.htm; } #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
server_name somename alias another.alias;
location / {
root html;
index apollo.html;
}
} server {
listen ;
server_name somename alias another.alias;
location / {
root html;
index jenkins.html;
}
} # HTTPS server
#
#server {
# listen 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;
# }
#} }

---------------------
作者:一线码农
来源:CNBLOGS
原文:https://www.cnblogs.com/huangxincheng/p/11790007.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

[转]如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析的更多相关文章

  1. 如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析

    前段时间公司根据要求需要将聚石塔上服务器从杭州整体迁移到张家口,刚好趁这次机会将这些乱七八糟的服务器做一次梳理和整合,断断续续一个月迁移完成 大概优化掉了1/3的机器,完成之后遇到了一些问题,比如曾今 ...

  2. 一个电器工厂可以生产多种类型的电器,如海尔工厂可以生产海尔电视机、海尔空调等,TCL工厂可以生产TCL电视机,TCL空调等,相同品牌的电器构成一个产品族,而相同类型的电器构成了一个产品等级结构,现使用

    一个电器工厂可以生产多种类型的电器,如海尔工厂可以生产海尔电视机.海尔空调等,TCL工厂可以生产TCL电视机,TCL空调等,相同品牌的电器构成一个产品族,而相同类型的电器构成了一个产品等级结构,现使用 ...

  3. spring集合类型的setter注入的一个简单例子

    在项目中我们有时候会为集合类型设定一些默认的值,使用spring后,我们可以通过配置文件的配置,用setter方式为对象的集合属性提供一些默认值,下面就是一个简单的例子. 首先我们创建了一个名为Col ...

  4. jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?

    1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...

  5. 在ax中怎么对enum类型循环取其中每一个值

    static void test(Args _args) { DictEnum dictEnum; int i,nextPos; EnumId enumId; ; enumId = EnumNum(S ...

  6. 使用django建博客时遇到的URLcon相关错误以及解决方法。错误提示:类型错误:include0获得一个意外的关键参数app_name

    root@nanlyvm:/home/mydj/mysite# python manage.py runserver Performing system checks... Unhandled exc ...

  7. 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法

    在我编程中,我遇到了一个这样的报错, 可是我引用的product类中又确实定义了这么一个方法, protected void BindPageData(int categoryID) { Produc ...

  8. 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。

    package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  9. 【紫书】BigInteger 高精度类型 原书上有一个bug:A+B!=B+A

    存个代码 struct BigInterger { static const int BASE = 1e8; ; vector<int> s; BigInterger() { *this ...

随机推荐

  1. 使MySQL支持emoji

    1. 修改数据库的字符集 和 排序规则为: ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode ...

  2. request.getcontextPath() 详解 和 <link标签>

    classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 绝对路径: D:\磊弟资料\最代码\智父子考试 ...

  3. idea导入gradle项目后,找不到右边gradle窗口

    解决方案:关闭当前项目idea,随便打开个其他的项目 选择你刚刚的gradle项目 一定要选择你的gradle文件,然后OK就行了..剩下的按照指示打开就会显示gradle右边窗了 原文弟子:http ...

  4. 阿里小二的日常工作要被TA们“接管”了!

    昨天有人偷偷告诉我说 阿里巴巴其实是一家科技公司! 我想了整整一夜 究竟是谁走漏了风声 那么重点来了,阿里到底是如何在内部的办公.生活中,玩转“黑科技”的呢? AI取名:给你专属的“武侠”花名 花名是 ...

  5. MacOS配置双网

    目的 日常工作中,我们可能会同时需要用到公司的内网以及互联网,为了避免来回的切换,我们可以通过配置电脑的两个网卡来实现同时访问内网和互联网. 环境说明 互联网 无线网卡 网关 子网掩码 内网 有线网卡 ...

  6. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

  7. 尖峰7月线上技术分享--Hadoop、MySQL

      7月2号晚20:30-22:30 东大博士Dasight分享主题<大数据与Hadoop漫谈> 7月5号晚20:30-22:30  原支付宝MySQL首席DBA分享主题<MySQL ...

  8. shell学习(19)- find查找命令

    Linux find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将查找到的子目录 ...

  9. php-textarea 换行

    PHP接收表单提交的信息之后 存入数据库 再次从数据库获取数据再前端显示时  空格还有回车都消失了: 解决办法: 1,存入数据库时候进行替换 2,或者在取出数据之后进行替换 然后再在html中显示 s ...

  10. [asp.net]登录协同工作平台安全解决方式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/david_520042/article/details/25372207 [摘要]公司领导说登录验证 ...