詳解nginx 代理多個服務(wù)器(多個server方式)
上一篇文章介紹了nginx的基本配置和使用方法,并且簡單的介紹了一下如何利用nginx結(jié)合tomcat進(jìn)行使用,達(dá)到反向代理的作用。現(xiàn)在我們要使用nginx達(dá)到這樣的一個目的,能夠代理多個服務(wù)器。
首先修改配置文件:
#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;
# }
#}
}
其中主要的是有兩個server,每個server對應(yīng)的被代理的服務(wù)器的不同。從而實(shí)現(xiàn)了nginx代理多個服務(wù)器的目的。
下面是兩個服務(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;
#}
}
下面是測試的結(jié)果:
首先兩個tomcat中部署兩個服務(wù)器:


然后啟動nginx。
cmd下:start nginx
分別訪問這兩個server:
http://localhost:9922/ngtt/

http://localhost:9977/testnnnn/

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx服務(wù)器配置HTTPS nginx.config 配置文件(教程)
下面小編就為大家分享一篇Nginx服務(wù)器配置HTTPS nginx.config 配置文件(教程),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
centos7下基于nginx+uwsgi部署Django項目的實(shí)現(xiàn)
Django是一個開源的Web應(yīng)用框架,使用Python語言編寫,主要用于搭建Web項目,本教程介紹如何在centos7下基于nginx+uwsgi部署Django項目的實(shí)現(xiàn),感興趣的可以了解一下2024-04-04
配置Nginx實(shí)現(xiàn)訪問本地靜態(tài)資源的完整指南
Nginx 是一個高性能的 HTTP 服務(wù)器和反向代理服務(wù)器,廣泛用于靜態(tài)資源的托管和負(fù)載均衡,在開發(fā)和生產(chǎn)環(huán)境中,我們常常需要使用 Nginx 來提供本地靜態(tài)資源的訪問,本文將詳細(xì)介紹如何配置 Nginx 以便訪問本地靜態(tài)資源,需要的朋友可以參考下2024-08-08
prometheus監(jiān)控nginx的實(shí)現(xiàn)
這篇文章主要介紹了prometheus監(jiān)控nginx的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Nginx下實(shí)現(xiàn)pathinfo及ThinkPHP的URL模式
本篇文章主要介紹了Nginx下實(shí)現(xiàn)pathinfo及ThinkPHP的URL模式。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Nginx if語句加正則表達(dá)式實(shí)現(xiàn)字符串截斷
這篇文章主要介紹了Nginx if語句加正則表達(dá)式實(shí)現(xiàn)字符串截斷功能,特殊場合下可能會需要這個功能,NGINX的奇淫技巧之一,需要的朋友可以參考下2015-02-02
Nginx 過濾靜態(tài)資源文件的訪問日志的實(shí)現(xiàn)
這篇文章主要介紹了Nginx 過濾靜態(tài)資源文件的訪問日志的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

