nginx與tomcat8整合

服務器: tomcat8

nginx版本:1.6.0

1、tomcat的安裝

2.1、安裝JAVA

cd /opt

tar -xzf jdk-8u151-linux-x64.tar.gz

ln -s jdk1.8.0_151/ java

2.2、配置環境變量

# jdk

JAVA_HOME=/opt/java

PATH=$JAVA_HOME/bin:$PATH

export JAVA_HOME PATH


source /etc/profile

2.3、查看JAVA是否安裝成功

java -version

[root@localhost local]# java -version

java version "1.8.0_151"

Java(TM) SE Runtime Environment (build 1.8.0_151-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

2.4、安裝tomcat

下載地址:

版本:apache-tomcat-8.5.24.tar.gz

2.5解壓

cd /opt

tar -xzf apache-tomcat-8.5.24.tar.gz

ln -s apache-tomcat-8.5.24 tomcat

2.6修改tomcat的環境變量

#tomcat

CATALINA_BASE=/opt/tomcat

PATH=$CATALINA_BASE/bin:$PATH

export PATH CATALINA_BASE


source /etc/profile

2.7查看tomcat版本狀態

cd /opt/tomcat/bin

./catalina.sh version

nginx與tomcat8整合

2.8 啟動tomcat服務


./catalina.sh start

ps -ef |grep tomcat

nginx與tomcat8整合

nginx與tomcat8整合

2.9添加啟動腳本

vi /etc/rc.d/init.d/tomcat

# Tomcat init>

#

# chkconfig: 2345 96 14

# description: The Apache Tomcat servlet/JSP container.

# JAVA_OPTS='-Xms64m -Xmx128m'

JAVA_HOME=/usr/java/latest

CATALINA_HOME=/opt/tomcat

export JAVA_HOME CATALINA_HOME

case $1 in

start)

exec $CATALINA_HOME/bin/catalina.sh start ;;

stop)

exec $CATALINA_HOME/bin/catalina.sh stop;;

restart)

$CATALINA_HOME/bin/catalina.sh stop

sleep 2

exec $CATALINA_HOME/bin/catalina.sh start ;;

*)

echo "Usage: `basename $0` {start|stop|restart}"

exit 1

;;

esac


chmod +x /etc/rc.d/init.d/tomcat

chkconfig --add tomcat

service tomcat start

2.10為tomcat配置個虛擬主機

vi /opt/tomcat/conf/server.xml

#改這裡支持中文

<connector>

connectionTimeout="20000"

redirectPort="8443" URLEncoding="UTF-8"/>


<host>

unpackWAR="true" autoDeploy="true">

<context>

nginx與tomcat8整合


創建站點目錄與文件:

mkdir -pv /opt/tomcat/webapps/test/WEB-INF/{classes,lib}

vim /opt/tomcat/webapps/test/index.jsp

<title>TomcatA/<title>/<connector>

TomcatA /<font>

<table>

Session ID

Created on

/<table>


重啟tomcat測試:

service tomcat stop

service tomcat start

瀏覽器登陸:http://192.168.0.13:8080/test/

nginx與tomcat8整合

2、nginx的編譯安裝

1.1、下載相關的包

http://nginx.org/download/nginx-1.6.0.tar.gz

1.2 準備編譯環境,解決依賴關係

yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel -y

1.3 編譯安裝nginx

tar -xzf nginx-1.6.0.tar.gz

cd nginx-1.6.0

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make && make install


1.4 啟動nginx

/usr/local/nginx/sbin/nginx #啟動服務

/usr/local/nginx/sbin/nginx -s stop #停止服務


1.5訪問nginx(默認為80端口)


nginx與tomcat8整合

3、nginx與tomcat整合

3.1 在/usr/local/nginx/conf下面添加文件proxy.conf

cat /usr/local/nginx/conf/proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;

client_body_buffer_size 128k;

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

3.2配置nginx.conf

user root;

worker_processes 1;


#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;


#pid logs/nginx.pid;

pid /usr/local/nginx/logs/nginx.pid;


events {

worker_connections 1024;

}


http {

include mime.types;

default_type application/octet-stream;

include /usr/local/nginx/conf/proxy.conf;


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


sendfile on;

tcp_nopush on;


#keepalive_timeout 0;

keepalive_timeout 65;


limit_conn_zone $binary_remote_addr zone=perip:10m;


server {

listen 80;

server_name localhost;


limit_rate 500k;


autoindex on;

autoindex_exact_size off;

autoindex_localtime off;

# charset gb2312;

charset utf-8;


#access_log logs/host.access.log main;

location / {

root /opt/tomcat/webapps/;

index download.html index.htm;

#limit_rate 500k;

}


location /nginx_status

{

stub_status on;

access_log off;

}


location ~ .*\\.(css|js|jpg|png|html)$ {

root /opt/tomcat/webapps/;

}


location ~* ^/(.*)\\.jsp|html|txt$ {

index download.html index.jsp;

proxy_pass http://192.168.0.11:8080;

}


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

#

#location ~ \\.php$ {

# proxy_pass http://127.0.0.1;

#}


# pass the PHP>

#

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

# }

#}


}

3.3 重啟nginx

chmod 777 /opt/tomcat/webapps/apk

service tomcat start

/usr/local/nginx/sbin/nginx -s reload


檢測配置/usr/local/nginx/sbin/nginx -thttp://192.168.0.13:8080http://192.168.0.13/test/index.jsp

4、nginx限制下載速度

limit_conn_zone $binary_remote_addr zone=addr:10m;(http裡面配置)

location /apk/dw/ {

limit_rate_after 500k;

limit_rate 50k;

}


分享到:


相關文章: