当前位置:   首页安装配置

多个主机:详解nginx 代理多个主机(多个server方式)

发布日期:2022-01-21 17:37 | 文章来源:源码中国

上一篇文章介绍了nginx的基本配置和使用方法,并且简单的介绍了一下如何利用nginx结合tomcat进行使用,达到反向代理的作用。现在我们要使用nginx达到这样的一个目的,能够代理多个主机。

首先修改配置文件:

#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_age

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

下面是两个服务server的配置:

server { 
  listen  9922; 
  server_name firstProxyServer; 
 
  #charset koi8-r; 
 
  #access_log logs/host.access.log main; 
 
  #location / { 
   #root html; 
   #index index.html index.htm; 
  #} 
  location / { 
   proxy_pass http://localhost:8989; 
  } 
 
  #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; 
  #} 
 } 
 
  server { 
  listen  9977; 
  server_name secondProxyServer; 
 
  #charset koi8-r; 
 
  #access_log logs/host.access.log main; 
 
  #location / { 
   #root html; 
   #index index.html index.htm; 
  #} 
  location / { 
   proxy_pass http://localhost:8080; 
  } 
 
  #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; 
  #} 
 } 

下面是测试的结果:

首先两个tomcat中部署两个主机:


然后启动nginx。

cmd下:start nginx

分别访问这两个server:

http://localhost:9922/ngtt/

http://localhost:9977/testnnnn/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部