下面示例,本人親測有效
為什麼要反向代理和叢集?
因為并發問題,很多請求如果全部分發給一個tomcat,一個tomcat優化最好的話,據說可達到800負載,但是面對成千上萬的請求,單單一個tomcat是不行的,不管是apr還是nio,面對高并發高負載時,采取Nginx反向代理政策是最好的。同時,也可以解決tomcat因為代碼問題或者記憶體洩露而導緻的當機問題。
關于tomcat的安裝和優化問題,本人部落格中已經有,大家可以參考
Ubuntu16.04之開發環境建構和
centos7之Java開發環境建構這兩篇文章
一、nginx初步使用
1.安裝前必先安裝執行這些指令
apt-get install build-essential
apt-get install libtool
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
apt-get install openssl
2.解壓配置啟動
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -xzvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/home/ubuntu1/nginx
make
make install
上述無報錯的情況進入下一步
進入相關目錄
cd /home/ubuntu1/nginx
啟動nginx
./sbin/nginx
//啟動成功的标志是沒有任何提示,這時你可以通過ps -ef|grep nginx 檢視是否有這個程序存在,如果存在表示成功,否則失敗
通常問題:
由于伺服器安裝的apache或者tomcat端口為80,通常會報錯,錯誤資訊就是說端口被占用,不能使用。
nginx預設端口為80.
這時你通過lsof -i 80 将對應端口程序殺死或者通過進入nginx/conf/修改nginx.conf配置檔案中的端口解決該問題
3.可以更換nginx歡迎頁
可以将nginx的歡迎頁改成自己的個性化頁面,比如自己網站的介紹等
進入 home/ubuntu1/nginx/html目錄下,将index.html換成自己的頁面
二、nginx做反向代理,tomcat叢集
前提安裝兩個tomcat
修改nginx.conf配置檔案:
#user nobody;
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 2048;
}
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;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream tomcat_server{
server 192.168.126.128:8080;
server 192.168.126.128:8090;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat_server;
root home/ubuntu1/tomcat_001/webapps/index.html;
# index index.html index.htm;