溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器

發(fā)布時(shí)間:2021-08-21 11:12:54 來(lái)源:億速云 閱讀:142 作者:小新 欄目:服務(wù)器

這篇文章給大家分享的是有關(guān)nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

首先修改配置文件:

#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:1m; 
 # ssl_session_timeout 5m; 
 
 # ssl_ciphers HIGH:!aNULL:!MD5; 
 # ssl_prefer_server_ciphers on; 
 
 # location / { 
 #  root html; 
 #  index index.html index.htm; 
 # } 
 #} 
 
}

其中主要的是有兩個(gè)server,每個(gè)server對(duì)應(yīng)的被代理的服務(wù)器的不同。從而實(shí)現(xiàn)了nginx代理多個(gè)服務(wù)器的目的。

下面是兩個(gè)服務(wù)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; 
  #} 
 }

下面是測(cè)試的結(jié)果:

首先兩個(gè)tomcat中部署兩個(gè)服務(wù)器:

nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器

nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器

然后啟動(dòng)nginx。

cmd下:start nginx

分別訪問(wèn)這兩個(gè)server:

http://localhost:9922/ngtt/

nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器

http://localhost:9977/testnnnn/

nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器

感謝各位的閱讀!關(guān)于“nginx如何實(shí)現(xiàn)代理多個(gè)服務(wù)器”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI