程序笔记   发布时间:2022-05-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了详解nginx 代理多个服务器(多个server方式)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇文章介绍了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_agent" "$http_x_forWARDed_for"'; 
 
 #access_log logs/access.log main; 
 
 sendfile  on; 
 #tcp_nopush  on; 
 
 #keepalive_timeout 0; 
 keepalive_timeout 65; 
 
 #gzip on; 
 
 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; 
  #} 
 } 
 
 # another virtual host using mix of IP-,name-,and port-based configuration 
 # 
 #server { 
 # Listen  8000; 
 # Listen  somename:8080; 
 # server_name somename alias another.alias; 
 
 # LOCATIOn / { 
 #  root HTML; 
 #  index index.HTML index.htm; 
 # } 
 #} 
 
 
 # httpS server 
 # 
 #server { 
 # Listen  443 ssl; 
 # server_name localhost; 
 
 # ssl_certificate  cert.pem; 
 # ssl_certificate_key cert.key; 
 
 # ssl_session_cache shared:SSL:1R_415_11845@; 
 # ssl_session_timeout 5m; 
 
 # ssl_ciphers HIGH:!aNulL:!MD5; 
 # ssl_prefer_server_ciphers on; 
 
 # LOCATIOn / { 
 #  root HTML; 
 #  index index.HTML index.htm; 
 # } 
 #} 
 
} 

其中主要的是有两个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; 
  #} 
 } 

下面是测试的结果:

首先两个tomcat中部署两个服务器:

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


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

然后启动Nginx。

cmd下:start Nginx

分别访问这两个server:

http://localhost:9922/ngtt/

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

http://localhost:9977/testnnnn/

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

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

大佬总结

以上是大佬教程为你收集整理的详解nginx 代理多个服务器(多个server方式)全部内容,希望文章能够帮你解决详解nginx 代理多个服务器(多个server方式)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。