博客
关于我
Nginx+Tomcat实现动静分离
阅读量:792 次
发布时间:2023-02-15

本文共 3894 字,大约阅读时间需要 12 分钟。

Nginx + Tomcat 实现动静分离

随着项目的逐渐完成,工作节奏逐渐平稳,我整理了之前的学习笔记,现将其中关于 Nginx + Tomcat 实现动静分离的内容分享给大家。

一、动静分离的概念

本文的动静分离主要通过 Nginx 和 Tomcat 来实现。Nginx 负责处理图片、HTML 等静态文件,而 Tomcat 则负责处理 JSP、DO 等动态文件。这种配置方式能够有效提升网站性能,特别是在处理大量静态资源时。

二、整体架构

整体架构如下:

  • Nginx 作为反向代理,负责处理静态资源和部分动态资源的路由。
  • Tomcat 作为动态资源的处理服务器,负责处理 JSP、DO 等动态请求。

三、安装与配置

1. 安装 Nginx

下载必要软件包

  • Nginx 可以通过以下命令下载:
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
  • 如果需要正则表达式支持,下载 pcre:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz

安装 pcre

tar zxvf pcre-8.10.tar.gzcd pcre-8.10./configuremakemake installcd ..

安装 Nginx

tar zxvf nginx-0.8.46.tar.gzcd nginx-0.8.46./configure --user www --group www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_modulemakemake install

2. 配置 Nginx

Nginx 配置文件位于 /etc/nginx/nginx.conf,以下是主要配置内容:

user www www;worker_processes 8;error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/nginx.pid;worker_rlimit_nofile 65535;events {    use epoll;    worker_connections 65535;}http {    include mime.types;    default_type application/octet-stream;    charset gb2312;    server_names_hash_bucket_size 128;    client_header_buffer_size 32k;    large_client_header_buffers 4 32k;    client_max_body_size 8m;    sendfile on;    tcp_nopush on;    keepalive_timeout 60;    tcp_nodelay on;    fastcgi_connect_timeout 300;    fastcgi_send_timeout 300;    fastcgi_read_timeout 300;    fastcgi_buffer_size 64k;    fastcgi_buffers 4 64k;    fastcgi_busy_buffers_size 128k;    fastcgi_temp_file_write_size 128k;    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-javascript text/css application/xml;    gzip_vary on;    # 限制爬虫访问    limit_zone crawler $binary_remote_addr:10m;    server {        listen 80;        server_name test1.dl.com;  # 10.1.88.176 是 test1.dl.com 的 IP        index index.html index.htm index.php;        root /usr/local/nginx/html;        # 限制爬虫连接数        limit_conn crawler 20;        location ~.*\.php$ {            proxy_pass http://10.1.88.168: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_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;        }        location ~.*\.(gif|jpg|jpeg|png|bmp|swf) {            root /usr/local/nginx/html;            expires 30d;        }        location ~(\.jsp)|(\.do) {            proxy_pass http://10.1.88.168: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;            proxy_set_header X-Forwarded-Proto $scheme;        }        location ~.*\.(js|css) {            expires 1h;        }        log_format access '$remote_addr - $remote_user [$time_local] "$request"'                    '$status $body_bytes_sent "$http_referer"'                    '"$http_user_agent"' "$http_x_forwarded_for"';        access_log /usr/local/nginx/logs/access.log access;    }    server {        listen 80;        server_name status.test1.dl.com;        location / {            stub_status on;            access_log off;        }    }}

3. 配置 Tomcat

Tomcat 的配置相对简单,主要需要注意以下几点:

  • Tomcat运行在 10.1.88.168:8080 地址。
  • 配置 server.xml 中的 Connector 为 HTTP/1.1 以支持 gzip 压缩。
  • 配置 application.xml 以加载必要的 web 应用程序。

四、总结

通过上述配置,Nginx 和 Tomcat 的组合能够有效实现动静资源的分离。Nginx 负责静态资源的高效处理和路由,Tomcat 则专注于动态资源的高性能处理。这种架构能够显著提升网站的性能表现,特别是在处理大量静态资源时。

转载地址:http://wqcfk.baihongyu.com/

你可能感兴趣的文章
Netty客户端断线重连实现及问题思考
查看>>
Netty工作笔记0001---Netty介绍
查看>>
Netty工作笔记0002---Netty的应用场景
查看>>
Netty工作笔记0003---IO模型-BIO-Java原生IO
查看>>
Netty工作笔记0005---NIO介绍说明
查看>>
Netty工作笔记0006---NIO的Buffer说明
查看>>
Netty工作笔记0007---NIO的三大核心组件关系
查看>>
Netty工作笔记0008---NIO的Buffer的机制及子类
查看>>
Netty工作笔记0009---Channel基本介绍
查看>>
Netty工作笔记0010---Channel应用案例1
查看>>
Netty工作笔记0011---Channel应用案例2
查看>>
Netty工作笔记0012---Channel应用案例3
查看>>
Netty工作笔记0013---Channel应用案例4Copy图片
查看>>
Netty工作笔记0014---Buffer类型化和只读
查看>>
Netty工作笔记0015---MappedByteBuffer使用
查看>>
Netty工作笔记0016---Buffer的分散和聚合
查看>>
Netty工作笔记0017---Channel和Buffer梳理
查看>>
Netty工作笔记0018---Selector介绍和原理
查看>>
Netty工作笔记0019---Selector API介绍
查看>>
Netty工作笔记0020---Selectionkey在NIO体系
查看>>