下面開始咱們的步驟: php
第一步: 安裝咱們的Nginx, Mac上是利用brew安裝的; html
[chenyuan@Mac:~]$ brew install nginx ==> Installing nginx dependency: pcre ==> Downloading http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.3 ######################################################################## 100.0% ==> ./configure --prefix=/usr/local/Cellar/pcre/8.34 --enable-utf8 --enable-unic ==> make ==> make test ==> make install /usr/local/Cellar/pcre/8.34: 140 files, 4.1M, built in 58 second> ==> Installing nginx
差很少就這一步, 咱們看看是夠安裝成功: java
[chenyuan@chenyuan-MBP:~]$ nginx -V nginx version: nginx/1.4.4 TLS SNI support enabled configure arguments: --prefix=/usr/local/Cellar/nginx/1.4.4 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.4.4/bin/nginx --with-cc-opt=-I/usr/local/include --with-ld-opt=-L/usr/local/lib --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module
能看見, 這裏安裝的是 1.4.4版本的. python
順便告訴你們, brew安裝的文件路徑通常默認在: nginx
[chenyuan@chenyuan-MBP:Cellar]$ pwd /usr/local/Cellar
然而, nginx的真正路徑在: web
[chenyuan@chenyuan-MBP:nginx]$ pwd /usr/local/etc/nginx
咱們啓動nginx服務器: apache
[chenyuan@chenyuan-MBP:~]$ sudo nginx Password: nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) nginx: [emerg] still could not bind()
輸入你的我的密碼, 就算是啓動了: tomcat
這就算啓動了. 很簡單吧~ 服務器
若是是查看nginx啓動進程 session
[chenyuan@chenyuan-MBP:~]$ ps -ef|grep nginx 0 8220 1 0 9:07PM ?? 0:00.00 nginx: master process nginx 502 8322 8220 0 9:34PM ?? 0:00.87 nginx: worker process 502 8323 8220 0 9:34PM ?? 0:00.15 nginx: worker process 502 8704 8598 0 8:27PM ttys000 0:00.00 grep nginx
關閉nginx服務器:
[chenyuan@chenyuan-MBP:~]$ sudo nginx -s stop
============= 如今就差很少把nginx的安裝說明白了. OK嗎?
第二步: 咱們在本機部署2臺Tomcat容器, 而且要正常跑起來. 具體看看:
你須要下載一個Tomcat, 這個我以爲不須要在說怎麼下載Tomcat了吧..我通常會去官方下載tar包.
apache-tomcat-7.0.37
下載以後, 咱們將名字修改成:
drwxr-xr-x 16 chenyuan staff 544 Jun 21 18:39 tomcat-7-2 drwxr-xr-x 16 chenyuan staff 544 Jun 21 18:41 tomcat-7-3
我這裏暫且從2開始取名吧~ 由於我本地還有一個tomcat-7-1, 本身開發用的. 反正這只是一個文件夾的名字而已, 應該不會形成混淆. 咱們就以 tomcat-7-2, tomcat-7-3, 開舉例子:
若是須要讓2臺Tomcat同時啓動, 咱們是須要修改配置文件的, 由於Tomcat綁定的端口號是惟一的, 其實咱們就是讓他們的端口號不衝突而已. 這樣子就能跑起2臺Tomcat了.
進入tomcat-7-2的配置
[chenyuan@chenyuan-MBP:conf]$ pwd /Users/chenyuan/Library/tomcat-7-2/conf [chenyuan@chenyuan-MBP:conf]$ ls -l total 408 drwxr-xr-x 3 chenyuan staff 102 Jun 21 18:39 Catalina -rw-r--r-- 1 chenyuan staff 12377 Jun 21 18:39 catalina.policy -rw-r--r-- 1 chenyuan staff 6528 Jun 21 18:39 catalina.properties -rw-r--r-- 1 chenyuan staff 1392 Jun 21 18:39 context.xml -rw-r--r-- 1 chenyuan staff 3352 Jun 21 18:39 logging.properties -rw-r--r-- 1 chenyuan staff 6455 Jun 21 18:52 server.xml -rw-r--r-- 1 chenyuan staff 1528 Jun 21 18:39 tomcat-users.xml -rw-r--r-- 1 chenyuan staff 162892 Jun 21 18:39 web.xml [chenyuan@chenyuan-MBP:conf]$
咱們打開server.xml文件, 這裏就是咱們須要配置的地方, 利用open命令打開, 很爽的一個命令, 我這裏設置默認打開爲 Sublime Text 2, 通常默認爲 VIM.
[chenyuan@chenyuan-MBP:conf]$ open server.xml [chenyuan@chenyuan-MBP:conf]$
<Server port="8105" shutdown="SHUTDOWN"> <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8180" protocol="HTTP/1.1" redirectPort="8443"/> <Connector port="8109" protocol="AJP/1.3" redirectPort="8443"/>
這3個關鍵子地方的port, 很好記, 我如今修改後都是以 81開頭的, 而以後的tomcat-7-3, 我就會以82開頭:
<Server port="8205" shutdown="SHUTDOWN"> <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8280" protocol="HTTP/1.1" redirectPort="8443"/> <Connector port="8209" protocol="AJP/1.3" redirectPort="8443"/>
這裏就算配置完了, 咱們能夠創建明目直接發佈到每一個不一樣的tomcat中, 也能夠直接跑. 我這類是直接發佈的我本身的一個war包. ROOT.war 到 webapp:
[chenyuan@chenyuan-MBP:tomcat-7-2]$ cd webapps/ [chenyuan@chenyuan-MBP:webapps]$ ls -l total 23352 drwxr-xr-x 10 chenyuan staff 340 Jun 21 19:03 ROOT -rw-r--r-- 1 chenyuan staff 11953340 Jun 21 19:00 ROOT.war drwxr-xr-x 53 chenyuan staff 1802 Jun 21 18:39 docs drwxr-xr-x 7 chenyuan staff 238 Jun 21 18:39 examples drwxr-xr-x 7 chenyuan staff 238 Jun 21 18:39 host-manager drwxr-xr-x 8 chenyuan staff 272 Jun 21 18:39 manager [chenyuan@chenyuan-MBP:webapps]$
咱們分開啓動2個Tomcat: 這個在tomcat的bin目錄下.
[chenyuan@chenyuan-MBP:bin]$ sh startup.sh
OK, 咱們訪問: 我直接上圖:
這就說明已經成功啓動了2臺:
第三步: 修改nginx配置, 讓其作好tomcat的跳轉:
進入:
[chenyuan@chenyuan-MBP:nginx]$ pwd /usr/local/etc/nginx
打開其中的 nginx.conf 文件: 我將貼上最基本的全文件供你們參考:
user chenyuan 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 http://localhost; 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 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; # 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 http://localhost; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect HOST default; }
而後重啓nginx服務器: 或者關閉後開啓動都同樣:
最後咱們輸入 http://localhost
第四步: 測試是否可以跳轉到不一樣的服務器:
咱們修改tomcat-7-2中的一個JSP頁面, 而tomcat-7-3不要修改. 咱們不停的刷新, 咱們能夠看見一下子有變化, 一下子不會變化, 這樣說明OK了.
咱們停掉tomcat-7-2, tomcat-7-3依然開啓無論, 依然可以訪問;
咱們停掉tomcat-7-3, tomcat-7-2依然開啓無論, 依然可以訪問;
這樣子就算搭建好了 Nginx + Tomcat7.0 集羣. 不算很難吧~ 我也是新手, 忘你們拍磚...