一,web服務器小論
以前的公司使用的web服務器是tomcat(tomcat+apache作集群),現在的公司是一家互聯網公司,采用的架構是resin+nginx作集群(resin比tomcat快?還有待以后章節比較討論),
集群配置服務器說明:
1),serve1:127.0.0.1:8080
2), serve2:127.0.0.1:8081
3), nginx:127.0.0.1:80
集群配置服務器說明:
1),serve1:127.0.0.1:8080
2), serve2:127.0.0.1:8081
3), nginx:127.0.0.1:80
二,配置resin服務器
(注:resin4之前,可以搭一個服務器,在<cluster></cluster>中配置多個server實例,通過實例啟動,4.0之后就不可以了,要有專業版才能實現)
在安裝resin之前,必須先安裝完jvm環境。
serve1:127.0.0.1:8080--->
1,安裝resin-4.0.*.tar.gz包
- shell $> tar zxvf resin-4.0.23.tar.gz
- shell $> cd resin-4.0.23
- shell $> ./configure --prefix=/usr/resin/resinserver1/resinserver1/
- shell $> make
- shell $> make install
2,配置resin.conf文件
shell $> cd /usr/resin/resinserver1/resinserver1/
- shell $> cd conf
- shell $> vim resin.conf
- ## 查找 <http address="*" port="8080"/>
- ## 注釋掉 <!--http address="*" port="8080"/-->
- ## 查找 <server id="" address="127.0.0.1" port="6800">
- ## 替換成
- <server id="resinserver1" address="127.0.0.1" port="6800">
- <!--<strong>兩臺服務器配置不同的watchdog端口,否則會出現異常</strong>-->
- <watchdog-port>6700</watchdog-port>
- <http id="" port="8080"/>
- </server>
- ###重啟 resin 服務#####
- shell $> cd ../bin
- shell $>./resin.sh -server resinserver1 start
serve1:127.0.0.1:8081--->
---1,
- shell $> tar zxvf resin-4.0.23.tar.gz
- shell $> cd resin-4.0.23
- shell $> ./configure --prefix=/usr/resin/resinserver2/resinserver2/
- shell $> make
- shell $> make install
2,配置resin
shell $> cd conf
- shell $> vim resin.conf
- ## 查找 <http address="*" port="8080"/>
- ## 注釋掉 <!--http address="*" port="8080"/-->
- ## 查找 <server id="" address="127.0.0.1" port="6800">
- ## 替換成
- <server id="resinserver2" address="127.0.0.1" port="6801">
- <!--兩臺服務器配置不同的watchdog端口-->
- <watchdog-port>6701</watchdog-port>
- <http id="" port="8081"/>
- </server>
- ###重啟 resin 服務#####
- shell $> cd ../bin
- shell $>./resin.sh -server resinserver2 start
通過以上配置,開啟兩臺resin服務器,接下來就是配置nginx
三,nginx的配置
nginx安裝需要以下軟件包安裝
1,nginx-1.1.5.tar.gz
2,tbje-nginx-upstream-jvm-route-6016b39.tar.gz
3,openssl-1.0.0e.tar.gz(OPENSSL不需要編譯安裝,只需要解壓出來就行)
分別解壓以上程序...
- shell $> cd nginx-1.1.5
- shell $> patch -p0 < ../tbje-nginx-upstream-jvm-route-6016b39/jvm_route.patch
- shell $> useradd www
- shell $> ./configure --user=www --group=www --prefix=/usr/nginx/nginxserver --with-http_stub_status_module --with-http_ssl_module=/usr/nginx/openssl-1.0.0e --add-module=/usr/nginx/tbje-nginx-upstream-jvm-route-6016b39
- shell $> make
- shell $> make install
到nginxserver/sbin目錄下,,運行./nginx 訪問http://127.0.0.1 出現“webcome to nginx”代表nginx安裝成功
四,配置集群
打開nginxserver/conf/nginx.conf文件。
- user www www;#工作進程的屬主
- worker_processes 2;#工作進程數,一般與 CPU 核數等同
-
- error_log logs/error.log crit;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
-
- pid logs/nginx.pid;
-
-
- events {
- worker_connections 1024;#每個工作進程允許最大的同時連接數
- }
-
-
- http {
- upstream backend {#集群服務器
- server 127.0.0.1:8080 srun_id=resinserver1;
- server 127.0.0.1:8081 srun_id=resinserver2;
- jvm_route $cookie_JSESSIONID|sessionid;#session共享
- }
-
- 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;#打開gzip文件壓縮
-
- server {
- listen 80;
- server_name localhost;
-
- charset UTF-8;
-
- #下面為location的配置,可以根據自己的業務情況進行定制
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- }
-
- #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;
- }
- location ~ .*\.jsp$
- {
- proxy_pass http://backend;
- proxy_redirect off;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header Host $http_host;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 1h;
- }
-
- # 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;
- # }
- #}
-
- }
nginx命令:
- start nginx 開啟nginx
-
- Nginx -s stop 快速關閉Nginx,可能不保存相關信息,并迅速終止web服務。(quick exit)
- Nginx -s quit 平穩關閉Nginx,保存相關信息,有安排的結束web服務。(graceful exit)
- Nginx -s reload 因改變了Nginx相關配置,需要重新加載配置而重載。(changing configuration,start a new worker,quitting an old worker gracefully.)
- Nginx -s reopen 重新打開日志文件。(reopenging log files)
上面兩resin的服務器已經打開,更改兩服務器下的如(resinserver1):/usr/resin/resinserver1/resinserver1/webapps/ROOT 下的index.jsp
區別服務器訪問路徑:
- <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- %>
- <html>
- <head>
- </head>
- <body>
- nginxserver1<!--在另一臺服務器上寫nginxserver2-->
- <br />
- <%out.print(request.getSession()) ;%>
- <br />
- <%out.println(request.getHeader("Cookie")); %>
- </body>
- </html>
這時重啟nginx
這時訪問:http://127.0.0.1/index.jsp會出現以下情況:
自我總結:
1,下面這個端口號也要變,不然只能啟動一個resin實例(未解)
<http address="*" port="8444">
<jsse-ssl self-signed-certificate-name="resin@localhost"/>
</http>
server 127.0.0.1:8080 ;
server 127.0.0.1:8081;
}
session replication 策略是復制會話,即一個用戶訪問了一次就把session復制到所有的服務器或這一部分服務器。
這樣的好處是如果正訪問的服務器down了 用戶可以自動被轉到別的服務器session不丟失。缺點當然是效率低。
session sticky策略則是不復制,一個用戶訪問了一次后,同一個session周期內,所有的請求都定向到這個服務器,
down了session就丟了。
3,啟動不同服務名稱的resin實例時,要到命令行去,不然不知道啟動哪個,報異常。
到G:\javaWeb\resin-pro-2\bin\目錄:start.bat -server resinserver2 start