nginx 代理多个服务器——多个server方式

原文链接:https://blog.csdn.net/wild46cat/article/details/52997005

-------------------------------------------------------------

配置文件下载地址:https://download.csdn.net/download/zengmingen/10462400

nginx 代理多个服务器——多个server方式

上一篇文章(http://blog.csdn.net/wild46cat/article/details/52840125)介绍了nginx的基本配置和使用方法,并且简单的介绍了一下如何利用nginx结合tomcat进行使用,达到反向代理的作用。现在我们要使用nginx达到这样的一个目的,能够代理多个服务器。
首先修改配置文件:
[plain] view plaincopy
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.   
  34.     server {  
  35.         listen       9922;  
  36.         server_name  firstProxyServer;  
  37.   
  38.         #charset koi8-r;  
  39.   
  40.         #access_log  logs/host.access.log  main;  
  41.   
  42.         #location / {  
  43.            #root   html;  
  44.             #index  index.html index.htm;  
  45.         #}  
  46.         location / {  
  47.             proxy_pass http://localhost:8989;  
  48.         }  
  49.   
  50.         #error_page  404              /404.html;  
  51.   
  52.         # redirect server error pages to the static page /50x.html  
  53.         #  
  54.         error_page   500 502 503 504  /50x.html;  
  55.         location = /50x.html {  
  56.             root   html;  
  57.         }  
  58.   
  59.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  60.         #  
  61.         #location ~ \.php$ {  
  62.         #    proxy_pass   http://127.0.0.1;  
  63.         #}  
  64.   
  65.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  66.         #  
  67.         #location ~ \.php$ {  
  68.         #    root           html;  
  69.         #    fastcgi_pass   127.0.0.1:9000;  
  70.         #    fastcgi_index  index.php;  
  71.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  72.         #    include        fastcgi_params;  
  73.         #}  
  74.   
  75.         # deny access to .htaccess files, if Apache's document root  
  76.         # concurs with nginx's one  
  77.         #  
  78.         #location ~ /\.ht {  
  79.         #    deny  all;  
  80.         #}  
  81.     }  
  82.   
  83.      server {  
  84.         listen       9977;  
  85.         server_name  secondProxyServer;  
  86.   
  87.         #charset koi8-r;  
  88.   
  89.         #access_log  logs/host.access.log  main;  
  90.   
  91.         #location / {  
  92.            #root   html;  
  93.             #index  index.html index.htm;  
  94.         #}  
  95.         location / {  
  96.             proxy_pass http://localhost:8080;  
  97.         }  
  98.   
  99.         #error_page  404              /404.html;  
  100.   
  101.         # redirect server error pages to the static page /50x.html  
  102.         #  
  103.         error_page   500 502 503 504  /50x.html;  
  104.         location = /50x.html {  
  105.             root   html;  
  106.         }  
  107.   
  108.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  109.         #  
  110.         #location ~ \.php$ {  
  111.         #    proxy_pass   http://127.0.0.1;  
  112.         #}  
  113.   
  114.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  115.         #  
  116.         #location ~ \.php$ {  
  117.         #    root           html;  
  118.         #    fastcgi_pass   127.0.0.1:9000;  
  119.         #    fastcgi_index  index.php;  
  120.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  121.         #    include        fastcgi_params;  
  122.         #}  
  123.   
  124.         # deny access to .htaccess files, if Apache's document root  
  125.         # concurs with nginx's one  
  126.         #  
  127.         #location ~ /\.ht {  
  128.         #    deny  all;  
  129.         #}  
  130.     }  
  131.   
  132.     # another virtual host using mix of IP-, name-, and port-based configuration  
  133.     #  
  134.     #server {  
  135.     #    listen       8000;  
  136.     #    listen       somename:8080;  
  137.     #    server_name  somename  alias  another.alias;  
  138.   
  139.     #    location / {  
  140.     #        root   html;  
  141.     #        index  index.html index.htm;  
  142.     #    }  
  143.     #}  
  144.   
  145.   
  146.     # HTTPS server  
  147.     #  
  148.     #server {  
  149.     #    listen       443 ssl;  
  150.     #    server_name  localhost;  
  151.   
  152.     #    ssl_certificate      cert.pem;  
  153.     #    ssl_certificate_key  cert.key;  
  154.   
  155.     #    ssl_session_cache    shared:SSL:1m;  
  156.     #    ssl_session_timeout  5m;  
  157.   
  158.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  159.     #    ssl_prefer_server_ciphers  on;  
  160.   
  161.     #    location / {  
  162.     #        root   html;  
  163.     #        index  index.html index.htm;  
  164.     #    }  
  165.     #}  
  166.   
  167. }  

其中主要的是有两个server,每个server对应的被代理的服务器的不同。从而实现了nginx代理多个服务器的目的。
下面是两个服务server的配置:
[plain] view plaincopy
  1. server {  
  2.         listen       9922;  
  3.         server_name  firstProxyServer;  
  4.   
  5.         #charset koi8-r;  
  6.   
  7.         #access_log  logs/host.access.log  main;  
  8.   
  9.         #location / {  
  10.            #root   html;  
  11.             #index  index.html index.htm;  
  12.         #}  
  13.         location / {  
  14.             proxy_pass http://localhost:8989;  
  15.         }  
  16.   
  17.         #error_page  404              /404.html;  
  18.   
  19.         # redirect server error pages to the static page /50x.html  
  20.         #  
  21.         error_page   500 502 503 504  /50x.html;  
  22.         location = /50x.html {  
  23.             root   html;  
  24.         }  
  25.   
  26.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  27.         #  
  28.         #location ~ \.php$ {  
  29.         #    proxy_pass   http://127.0.0.1;  
  30.         #}  
  31.   
  32.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  33.         #  
  34.         #location ~ \.php$ {  
  35.         #    root           html;  
  36.         #    fastcgi_pass   127.0.0.1:9000;  
  37.         #    fastcgi_index  index.php;  
  38.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  39.         #    include        fastcgi_params;  
  40.         #}  
  41.   
  42.         # deny access to .htaccess files, if Apache's document root  
  43.         # concurs with nginx's one  
  44.         #  
  45.         #location ~ /\.ht {  
  46.         #    deny  all;  
  47.         #}  
  48.     }  
  49.   
  50.      server {  
  51.         listen       9977;  
  52.         server_name  secondProxyServer;  
  53.   
  54.         #charset koi8-r;  
  55.   
  56.         #access_log  logs/host.access.log  main;  
  57.   
  58.         #location / {  
  59.            #root   html;  
  60.             #index  index.html index.htm;  
  61.         #}  
  62.         location / {  
  63.             proxy_pass http://localhost:8080;  
  64.         }  
  65.   
  66.         #error_page  404              /404.html;  
  67.   
  68.         # redirect server error pages to the static page /50x.html  
  69.         #  
  70.         error_page   500 502 503 504  /50x.html;  
  71.         location = /50x.html {  
  72.             root   html;  
  73.         }  
  74.   
  75.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  76.         #  
  77.         #location ~ \.php$ {  
  78.         #    proxy_pass   http://127.0.0.1;  
  79.         #}  
  80.   
  81.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  82.         #  
  83.         #location ~ \.php$ {  
  84.         #    root           html;  
  85.         #    fastcgi_pass   127.0.0.1:9000;  
  86.         #    fastcgi_index  index.php;  
  87.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  88.         #    include        fastcgi_params;  
  89.         #}  
  90.   
  91.         # deny access to .htaccess files, if Apache's document root  
  92.         # concurs with nginx's one  
  93.         #  
  94.         #location ~ /\.ht {  
  95.         #    deny  all;  
  96.         #}  
  97.     }  

下面是测试的结果:
首先两个tomcat中部署两个服务器:



然后启动nginx。
cmd下:start nginx

分别访问这两个server:
http://localhost:9922/ngtt/


http://localhost:9977/testnnnn/



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/537959.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sc openscmanager 失败 5 mysql_如何增加windows服务

我以前也出现过你这个问题,用优化大师给删了吧,后来也是重装的,其实说是重装也不是重装,就是修复啦,如果你不想这样,那可以试试这个,我没试过用在mysql上,但别的到是用他加载过。让程…

TemplatePart用法说明

原文:TemplatePart用法说明TemplatePart(Name"PART_Decrease", Typetypeof(RepeatButton)) 一直没明白这是干嘛用的,搜了一下,记载一下。 以Button的定义为例: namespace System.Windows.Controls {// Summary:// Represents a…

nginx配置多个站点共用80端口

原文链接:https://blog.csdn.net/zhezhebie/article/details/73459874 --------------------------------------------- 配置文件下载地址:https://download.csdn.net/download/zengmingen/10462400共用80端口的,要server_name不同。如果用域…

两点间最短路 java_AcWing 850. Dijkstra求最短路 II_Java实现含详细注释

import java.io.*;import java.util.Arrays;import java.util.Comparator;import java.util.PriorityQueue;public class Main {static final int N 150010;static int n, m; //结点数,边数static int[] h, e, ne, w; //邻接表适合表示稀疏图,w用来存每个边权重sta…

SQL Server如何链接到 Oracle并查询其中的数据?并实现做接口

今天用Oracle的驱动教大家如何从SQL Server链接到Oracle. 1. 服务器上需要安装Oracle 64位的客户端或者服务端,安装过程就省略了。不会的同学可以网上搜索一下安装方法,很详细,这里不赘述。 安装完成后SQL Server的访问接口上会新增”OraOLE…

Tomcat 内存调大

第一种方法:Windows下,在文件/bin/catalina.bat,Unix下,在文件/bin/catalina.sh的前面,增加如下设置:JAVA_OPTS-Xms【初始化内存大小】 -Xmx【可以使用的最大内存】需要把这个两个参数值调大。例如&#xf…

java spring bean配置文件_Spring基于xml文件配置Bean过程详解

这篇文章主要介绍了spring基于xml文件配置Bean过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下通过全类名来配置:class:bean的全类名,通过反射的方式在IOC容器中创建B…

win10升级后chrome碰到对话框就卡死

低版本的 chrome 会出现这样的问题 解决方法: 设置-------高级设置-----取消硬件加速

客户端SDK测试思路

本文来自网易云社区作者:万春艳是什么客户端SDK是为第三方开发者提供的软件开发工具包,包括SDK接口、开发文档和Demo示例等。SDK和应用之间是什么关系呢?以云信即时消息服务为例,如下图所示,应用客户端通过调用云信SDK…

nginx could not build the server_names_hash 解决方法

原文地址:http://www.jb51.net/article/26412.htm ------------------------------------------------------- nginx “nginx could not build the server_names_hash”解决方法 给一个服务器下增加了一些站点别名,差不多有20多个。 重启nginx时候&#…

java 使用fusioncharts_fusioncharts同一页面显示2个仪表盘,且以java字符串作为xml数据...

fusioncharts同一页面显示2个仪表盘,且以java字符串作为xml数据String path request.getContextPath();%>String xml "";%>FusionCharts - Multiple Charts on one Pagevar contextpath "";var xml ;body {font-family: Arial, Helve…

排名前16的Java工具类

原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类。以下工具类、方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码…

VS2012(Visual Studio 2012)官方免费中文旗舰版下载(含激活密钥)

原文路径:http://www.nocang.com/visual-studio-ultimate-2012/ vs2012旗舰版安装激活教程 1、下载到的是ISO格式文件,直接解压缩或用虚拟光驱加载运行;2、无所不藏推荐直接解压缩安装即可,双击“vs_ultimate.exe”进行安装&#…

magic square java_测试Magic Square Java的.txt文件

我不想问,但我无法弄清楚这个任务,当我寻求帮助时,助教也不会。我必须从文本文件中获取输入,将文件中的整数输入到数组列表中,然后测试它是否是anxn幻方。n等于数组列表长度的平方根。如果不是理想的正方形&#xff0c…

字符串拼串 能缓解我们的开发难度→!←(ε=(´ο`*)))唉,又是一个不知道该怎么写题目的随笔啊,头疼)...

简单描述:今天看我同事提交的代码,发现一个东西,让我有了一点小想法,是这样的,他利用一个‘’无关紧要‘’的标签属性,(哈哈哈,也不能说人家是无关紧要的属性了,暂时是无关紧要的属性…

SQL中使用DISTINCT显示多个字段的方法(不使用DISTINCT了)

原文连接: https://www.cnblogs.com/alanliu/archive/2008/02/25/1080626.html --------------------------------- 效果是DISTINCT CUS_NO,并且同时显示CUS_NAME.SELECTCUS_NO,MIN(CUS_NAME) ASCUS1 FROMdbo.CUS GROUPBYCUS_NO

java 注释快捷打出时间_Java快捷---自动注释时间作者。。。

在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的。修改作者、日期注释格式:打开Windows->Preferences->Java->Code Style->Code Templates,点击右边窗口中的Comments,可以看到有很多…

016 pickle

英文也是泡菜的意思。 学完了,还是感觉这个模块是蛮不错的,对多数据保存到文件中,然后在使用的时候,再读取出来,让程序闲的更加优雅,简洁。 一:介绍 1.为什么使用 在开篇已经介绍了,…

centos7与centos6区别

原文连接:https://www.cnblogs.com/bethal/p/5945026.html ---------------------------------------------------------------- CentOS 7 vs CentOS 6的不同 (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell)(2)文件系统[…

用java编写日历添加窗口一角_Java 实训4 编写一个窗体程序显示日历

实训要求:1.使用BorderLayout 进行总体布局2.在North 位置放置包含两个按钮( 上月和下月)的Panel3.在South 位置放置一个Label 用于显示当前年份和月份4.在Center 位置放置一个显示日历的Panel5.显示日历的Panel 设置7 行7 列的GridLayout 布局,其中第1行…