Mac 环境下搭建Nginx + Tomcat集群(3)

第三步: 修改nginx配置, 让其做好tomcat的跳转:

进入:

[linuxidc@:nginx]$ pwd
/usr/local/etc/nginx

打开其中的 nginx.conf 文件: 我将贴上最基本的全文件供大家参考:

user linuxidc staff ;
worker_processes 2;

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

upstream localhost {
#ip_hash;
server localhost:8180;
server localhost:8280;
}

server {
listen 80;
server_name localhost;

charset utf-8;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
proxy_pass ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect HOST default;
}

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

# 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;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

#include /usr/local/etc/nginx/conf.d/*.conf;

}

其中需要注意的地方:

upstream localhost {
#ip_hash;
server localhost:8180;
server localhost:8280;
}

location / {
root html;
index index.html index.htm;
proxy_pass ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect HOST default;
}

然后重启nginx服务器: 或者关闭后开启动都一样:

最后我们输入

Mac 环境下搭建Nginx + Tomcat集群

第四步: 测试是够真的跳转了不同的服务器:

我们修改tomcat-7-2中的一个JSP页面, 而tomcat-7-3不要修改. 我们不停的刷新, 我们可以看见一会儿有变化, 一会儿不会变化, 这样说明OK了.

我们停掉tomcat-7-2, 依然能够访问;

我们停掉tomcat-7-3, 依然能够访问;

这样子就算搭建好了 Nginx + Tomcat7.0 集群. 不算很难吧~ 我也是新手, 忘大家拍砖...

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/588d2d661dcb25d38f85039aea66a4be.html