>  기사  >  운영 및 유지보수  >  nginx를 사용하여 여러 서버를 프록시하는 방법

nginx를 사용하여 여러 서버를 프록시하는 방법

PHPz
PHPz앞으로
2023-05-17 12:19:154046검색

먼저 구성 파일을 수정하세요.

#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; 
 # } 
 #} 
 
}

가장 중요한 점은 두 개의 서버가 있고 각 서버는 서로 다른 프록시 서버에 해당한다는 것입니다. 이는 nginx가 여러 서버를 프록시하는 목적을 달성합니다.

다음은 두 서비스 서버의 구성입니다.

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를 사용하여 여러 서버를 프록시하는 방법

nginx를 사용하여 여러 서버를 프록시하는 방법

그런 다음 nginx를 시작합니다.

cmd: nginx 시작

다음 두 서버를 각각 방문하세요.

http://localhost:9922/ngtt/

nginx를 사용하여 여러 서버를 프록시하는 방법

http://localhost:9977/testnnnn/

nginx를 사용하여 여러 서버를 프록시하는 방법

위 내용은 nginx를 사용하여 여러 서버를 프록시하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제