原文链接: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. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8. worker_connections  1024;
  9. }
  10. http {
  11. include       mime.types;
  12. default_type  application/octet-stream;
  13. #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14. #                  '$status $body_bytes_sent "$http_referer" '
  15. #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log  logs/access.log  main;
  17. sendfile        on;
  18. #tcp_nopush     on;
  19. #keepalive_timeout  0;
  20. keepalive_timeout  65;
  21. #gzip  on;
  22. server {
  23. listen       9922;
  24. server_name  firstProxyServer;
  25. #charset koi8-r;
  26. #access_log  logs/host.access.log  main;
  27. #location / {
  28. #root   html;
  29. #index  index.html index.htm;
  30. #}
  31. location / {
  32. proxy_pass http://localhost:8989;
  33. }
  34. #error_page  404              /404.html;
  35. # redirect server error pages to the static page /50x.html
  36. #
  37. error_page   500 502 503 504  /50x.html;
  38. location = /50x.html {
  39. root   html;
  40. }
  41. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  42. #
  43. #location ~ \.php$ {
  44. #    proxy_pass   http://127.0.0.1;
  45. #}
  46. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  47. #
  48. #location ~ \.php$ {
  49. #    root           html;
  50. #    fastcgi_pass   127.0.0.1:9000;
  51. #    fastcgi_index  index.php;
  52. #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  53. #    include        fastcgi_params;
  54. #}
  55. # deny access to .htaccess files, if Apache's document root
  56. # concurs with nginx's one
  57. #
  58. #location ~ /\.ht {
  59. #    deny  all;
  60. #}
  61. }
  62. server {
  63. listen       9977;
  64. server_name  secondProxyServer;
  65. #charset koi8-r;
  66. #access_log  logs/host.access.log  main;
  67. #location / {
  68. #root   html;
  69. #index  index.html index.htm;
  70. #}
  71. location / {
  72. proxy_pass http://localhost:8080;
  73. }
  74. #error_page  404              /404.html;
  75. # redirect server error pages to the static page /50x.html
  76. #
  77. error_page   500 502 503 504  /50x.html;
  78. location = /50x.html {
  79. root   html;
  80. }
  81. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  82. #
  83. #location ~ \.php$ {
  84. #    proxy_pass   http://127.0.0.1;
  85. #}
  86. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  87. #
  88. #location ~ \.php$ {
  89. #    root           html;
  90. #    fastcgi_pass   127.0.0.1:9000;
  91. #    fastcgi_index  index.php;
  92. #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  93. #    include        fastcgi_params;
  94. #}
  95. # deny access to .htaccess files, if Apache's document root
  96. # concurs with nginx's one
  97. #
  98. #location ~ /\.ht {
  99. #    deny  all;
  100. #}
  101. }
  102. # another virtual host using mix of IP-, name-, and port-based configuration
  103. #
  104. #server {
  105. #    listen       8000;
  106. #    listen       somename:8080;
  107. #    server_name  somename  alias  another.alias;
  108. #    location / {
  109. #        root   html;
  110. #        index  index.html index.htm;
  111. #    }
  112. #}
  113. # HTTPS server
  114. #
  115. #server {
  116. #    listen       443 ssl;
  117. #    server_name  localhost;
  118. #    ssl_certificate      cert.pem;
  119. #    ssl_certificate_key  cert.key;
  120. #    ssl_session_cache    shared:SSL:1m;
  121. #    ssl_session_timeout  5m;
  122. #    ssl_ciphers  HIGH:!aNULL:!MD5;
  123. #    ssl_prefer_server_ciphers  on;
  124. #    location / {
  125. #        root   html;
  126. #        index  index.html index.htm;
  127. #    }
  128. #}
  129. }

其中主要的是有两个server,每个server对应的被代理的服务器的不同。从而实现了nginx代理多个服务器的目的。

下面是两个服务server的配置:
[plain] view plaincopy
  1. server {
  2. listen       9922;
  3. server_name  firstProxyServer;
  4. #charset koi8-r;
  5. #access_log  logs/host.access.log  main;
  6. #location / {
  7. #root   html;
  8. #index  index.html index.htm;
  9. #}
  10. location / {
  11. proxy_pass http://localhost:8989;
  12. }
  13. #error_page  404              /404.html;
  14. # redirect server error pages to the static page /50x.html
  15. #
  16. error_page   500 502 503 504  /50x.html;
  17. location = /50x.html {
  18. root   html;
  19. }
  20. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  21. #
  22. #location ~ \.php$ {
  23. #    proxy_pass   http://127.0.0.1;
  24. #}
  25. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  26. #
  27. #location ~ \.php$ {
  28. #    root           html;
  29. #    fastcgi_pass   127.0.0.1:9000;
  30. #    fastcgi_index  index.php;
  31. #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  32. #    include        fastcgi_params;
  33. #}
  34. # deny access to .htaccess files, if Apache's document root
  35. # concurs with nginx's one
  36. #
  37. #location ~ /\.ht {
  38. #    deny  all;
  39. #}
  40. }
  41. server {
  42. listen       9977;
  43. server_name  secondProxyServer;
  44. #charset koi8-r;
  45. #access_log  logs/host.access.log  main;
  46. #location / {
  47. #root   html;
  48. #index  index.html index.htm;
  49. #}
  50. location / {
  51. proxy_pass http://localhost:8080;
  52. }
  53. #error_page  404              /404.html;
  54. # redirect server error pages to the static page /50x.html
  55. #
  56. error_page   500 502 503 504  /50x.html;
  57. location = /50x.html {
  58. root   html;
  59. }
  60. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  61. #
  62. #location ~ \.php$ {
  63. #    proxy_pass   http://127.0.0.1;
  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. # deny access to .htaccess files, if Apache's document root
  75. # concurs with nginx's one
  76. #
  77. #location ~ /\.ht {
  78. #    deny  all;
  79. #}
  80. }

下面是测试的结果:

首先两个tomcat中部署两个服务器:
然后启动nginx。
cmd下:start nginx
分别访问这两个server:
http://localhost:9922/ngtt/
http://localhost:9977/testnnnn/

nginx 代理多个服务器——多个server方式相关推荐

  1. 详解nginx 代理多个服务器(多个server方式)

    其中主要的是有两个server,每个server对应的被代理的服务器的不同.从而实现了nginx代理多个服务器的目的. user root; worker_processes 1;#error_log ...

  2. Nginx代理内网服务器访问外网

    Nginx代理内网服务器访问外网 1.SpringBoot工程配置文件改写 2. SpringBoot静态工具类注入配置文件变量 3. SpringBoot 使用 4. 配置Nginx 问题描述: 有 ...

  3. nginx 代理 portainer 报 Unable to retrieve server settings and status

    一.nginx配置 location /portainer {proxy_pass http://localhost:19000/;proxy_set_header Via "nginx&q ...

  4. Nginx:12---反向代理之(代理模块,代理单个上游服务器)

    一.反向代理介绍 反向代理应该是 Nginx 做的最多的一件事了,什么是反向代理呢,反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网 ...

  5. Nginx代理上传文件大小设置

    一,安装nginx配置 进入nginx配置文件–>conf–>nginx.conf #user nobody; worker_processes 5; events {worker_con ...

  6. Nginx代理sftp配置

    最近有几个项目组的同事都在咨询nginx代理sftp的问题,那今天就写下此博文以供大家参考.献丑了! 0. 环境 主机名 IP 角色 nginx 192.168.10.182 代理服务器 sftp_s ...

  7. Nginx配置实例-反向代理实现浏览器请求Nginx跳转到服务器某页面

    场景 Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detai ...

  8. Nginx——反向代理多个服务器

    反向代理单个服务器 在配置反向代理之前,你需要保证你的系统中已经成功安装了Tomcat 配置过tomcat之后,我们可以通过ip+端口号访问tomcat,tomcat默认端口是8080,访问之后会得到 ...

  9. Nginx快速入门(三)正向代理、HTTP服务器与动静分离

    3.HTTP服务器 Nginx本身也是一个静态资源的服务器,当只有静态资源的时候,就可以使用Nginx来做服务器,同时现在也很流行动静分离,就可以通过Nginx来实现,首先看看Nginx做静态资源服务 ...

最新文章

  1. Handler 实现的一个延时操作
  2. 异部时钟电路的FPGA设计
  3. Python语言学习:利用sorted对字典按照value进行递减排序,输出列表,并给定排名索引,组成新字典输出
  4. 武大上交发布首篇「图像匹配」大领域综述!涵盖 8 个子领域,汇总近 20年经典方法
  5. 贪心只能过样例 loj515
  6. ubuntu gedit出错:Failed to connect to the session manager
  7. React ref的转发
  8. oracle中 start with,Oracle中connect by...start with...的使用
  9. Keep-Alive功能使客户端到服务器端的连接持续有效
  10. Docker下ETCD集群搭建
  11. Ext.util.Format.date与Ext.Date.format区别, 转换时间戳
  12. html宠物医院制作与实现,基于JSP的宠物医院系统设计与实现-毕业设计.doc
  13. HTML设置半透明的背景,CSS设置半透明背景实例详解
  14. java利用反射映射两个不同对象的属性值
  15. 产品经理入职四部曲—带你顺利度过试用期
  16. 计算机进入桌面黑屏怎么办,电脑进入桌面后黑屏如何修复 电脑经常进入桌面之后黑屏的处理方法...
  17. Eclipse Theia:Eclipse的继承者?
  18. PyQt5编程-鼠标事件
  19. git merge 冲突解决
  20. 【系】微信小程序云开发实战坚果商城-扩展篇

热门文章

  1. php 两个数组 交集_两个数组的交集
  2. db2 空值转换函数_Hive常见函数的使用
  3. multisim中轻触开关在哪_现货供应轻触开关|品质确保|厂家直销
  4. java禁止js获取cookie_java中Cookie被禁用后Session追踪问题
  5. xargs 命令教程
  6. kafka和mysql内存机制_一文五分钟让你彻底理解Kafka架构原理
  7. 区块如何防篡改_深入浅出:一条数据是如何完成上链的
  8. html5画电池状态,HTML5的一个显示电池状态的API简介
  9. 配置文件bashrc与profile的区别
  10. MYSQL出错代码列表大全(中文)