nginx+tomcat實現負載均衡以及session共享(linux centos7環境)


一、nginx的安裝

1.准備三份tomcat

tomcat1 設置端口 8080

tomcat2 設置端口 8081

tomcat3 設置端口 8082

2.

下載nginx

3.

解壓到/home目錄下 並重命名為nginx

4.

cd /home/nginx  進入nginx目錄

5.

./configure --with-http_stub_status_module 

進行初始化配置。

如提示PCRE錯誤,需要手動安裝PCRE,見 http://www.linuxidc.com/Linux/2015-03/114986.htm

安裝完PCRE后,再次運行

./configure --with-http_stub_status_module

進行初始化即可

6.

make 編譯

make install 編譯安裝。

7.

完成第六步 會出現如以下提示

  ......
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
  ......

8.

./usr/local/nginx/sbin/nginx 啟動nginx

如出現下面所述的錯誤,在已安裝PCRE庫的情況下,需要配置PCRE共享庫。具體操作看這里 http://www.linuxidc.com/Linux/2015-03/114985.htm 。

./usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

9.

在瀏覽器中輸入IP:80,出現如下圖所示,說明安裝成功。

10.

nginx操作:

啟動
./usr/local/nginx/sbin/nginx

重啟
./usr/local/nginx/sbin/nginx -s reload

關閉nginx
./usr/local/nginx/sbin/nginx -s stop

11.

vi /usr/local/nginx/conf/nginx.conf  修改nginx配置

如下:

 

監控訪問路徑 ip:80/status

配置好以后的nginx.conf文件內容:

#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 {
client_max_body_size 100M; 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 tomcat { server 127.0.0.1:8080 weight=1; server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://tomcat; proxy_redirect default; #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /status { stub_status on; access_log off; } #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; # } #} }

 

 

 

 

二、設置tomcat集群session共享

0.如果是服務端集群,首先在項目目錄下WEB-INF目錄中的web.xml中配置:

 

<distributable/>
如圖:

 
        

 

1.

修改三個tomcat目錄下conf/server.xml 端口

以及如圖:tomcat 1、2、3 

2.

分別啟動tomcat 123 以及nginx,訪問ip:80進入項目則成功

session是否共享成功測試jsp頁面代碼為:刷新session不變說明成功。(測試時要在jsp平行目錄新建WEB-INF/web.xml)

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ page import="java.util.*" %>

<html><head><title>Cluster App Test</title></head>

<body>

Server Info:

<%
  out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");
%>
<%

  out.println("<br> ID " + session.getId()+"<br>");

  // 如果有新的 Session 屬性設置

  String dataName = request.getParameter("dataName");

  if (dataName != null && dataName.length() > 0) {

    String dataValue = request.getParameter("dataValue");

    session.setAttribute(dataName, dataValue);

  }

  out.println("<b>Session 列表</b><br>");

  System.out.println("============================");

  Enumeration e = session.getAttributeNames();

  while (e.hasMoreElements()) {

    String name = (String)e.nextElement();

    String value = session.getAttribute(name).toString();

    out.println( name + " = " + value+"<br>");

    System.out.println( name + " = " + value);

  }

%>

<form action="index.jsp" method="POST">

  名稱:<input type=text size=20 name="dataName">

  <br>

  值:<input type=text size=20 name="dataValue">

  <br>

  <input type=submit>

</form>

</body>

</html>

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <distributable/>
</web-app>

 

 

 

三、參考擴展

http://blog.csdn.net/e421083458/article/details/30086413


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM