nginx+tomcat动静分离

Posted feral

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat动静分离相关的知识,希望对你有一定的参考价值。

一、安装nginx

yum -y install wget pcre pcre-devel openssl-devel zlib-devel lrzsz gcc gcc-c++
wget http://nginx.org/download/nginx-1.12.0.tar.gz
wget http://nginx.org/download/nginx-1.12.0.tar.gz
groupadd www
useradd -g www -s /sbin/nologin -M -r www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module

make && make install

添加配置文件:

user  www;
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 {
    use epoll;
    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;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascripttext/css application/xml;
    gzip_vary on;

    server {
        listen       80;
        server_name  www.test.com;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/tomcat/webapps/ROOT;
            index  index.html index.jsp  index.htm;
        }

    location ~ .*.jsp$ {
            index index.jsp;
            proxy_pass http://127.0.0.1:8080;   
            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_read_timeout 90;      
            proxy_buffer_size 4k;       
            proxy_buffers 6 32k;        
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k; 
        }
        location ~ .*\.(gif|jpg|png|bmp|swf)$   
        {
            expires 30d;   
        }
        location ~ .*\.(jsp|js|css)?$
        {
            expires 1d;
        }    
    
       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 Apaches document root
        # concurs with nginxs 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;
    #    }
    #}

}

 

添加启动脚本:

#!/bin/bash
# chkconfig: 345 99 20
# description: Nginx servicecontrol script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "Nginx service start success."
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo "reload Nginx config success."
;;
*)
echo "Usage: $0{start|stop|restart|reload}"
exit 1
esac
chmod +x /etc/init.d/nginx
service nginx restart
chkconfig nginx on

添加nginx环境:

echo -e PATH=/usr/local/nginx/sbin:$PATH >> /etc/profile
source /etc/profile

 

二、安装tomcat

 






以上是关于nginx+tomcat动静分离的主要内容,如果未能解决你的问题,请参考以下文章

Nginx---动静分离

15.Nginx动静分离Rewrite

Nginx+Tomcat动静分离架构

Nginx+Tomcat负载均衡动静分离

图文详解 配置Nginx+Tomcat负载均衡动静分离集群

什么是动静分离?