Nginx + Tomcat 动静分离

本文介绍了如何通过配置Nginx实现动静分离,将静态资源直接由Nginx处理,动态请求转发到Tomcat服务器。主要内容包括Nginx的http、events及server块的配置,以及针对PHP、静态文件(如gif、jpg)和动态请求(如jsp、do)的处理规则设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;              ####test1.dl.com的ip为10.1.88.176  
        index index.html index.htm index.php;  
        root  /usr/local/nginx/html;  
     
        #limit_conn   crawler  20;      
                                   
        location ~ .*\.(php|php5)?$  
        {        
          #fastcgi_pass  unix:/tmp/php-cgi.sock;  
          fastcgi_pass  127.0.0.1:9000;  
          fastcgi_index index.php;  
          include fastcgi.conf;  
       }  
          
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$              ###所以的静态文件人gif、jpg等都在本地打开,存放的目录为/usr/local/nginx/html,保存时间为30天  
        {  
            root         /usr/local/nginx/html;   
        expires      30d;  
        }  
        location ~ (\.jsp)|(\.do)$                              ###所以jsp、do的动态请求都交给后面的tomcat处理  
        {  
        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 ~ .*\.(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;  
        }  
      }  
    }  
### 一、NginxTomcat 动静分离概述 为了提高系统的性能和扩展性,通常会采用 Nginx 处理静态资源(如 HTML 文件、CSS 样式表、JavaScript 脚本以及图片等),而将动态请求转发给 Tomcat 来处理。这种动静分离的设计可以显著减少服务器的压力,并提升用户的访问体验。 --- ### 二、具体配置流程 #### 1. 安装与初始化 - **安装 Nginx** 确保已成功安装 Nginx 并能够正常运行[^1]。 - **安装 Tomcat** 使用 `startup.sh` 启动 Tomcat,并验证其默认页面是否可以通过浏览器访问[^4]。 #### 2. 配置 Nginx 处理静态资源 编辑 Nginx 的配置文件 `/etc/nginx/nginx.conf` 或者站点对应的虚拟主机配置文件: ```nginx server { listen 80; server_name localhost; # 设置静态资源路径 location /static/ { root /var/www/html/; index index.html; } # 将动态请求转发至 Tomcat location ~ \.jsp$ { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 在此配置中: - 所有以 `/static/` 开头的 URL 请求会被映射到本地磁盘上的指定目录 `/var/www/html/static/`[^1]。 - 对于 `.jsp` 结尾的动态请求,则通过反向代理将其传递给 Tomcat[^3]。 #### 3. 修改 Tomcat 的配置 调整 `$TOMCAT_HOME/conf/server.xml` 文件中的 `<Host>` 节点属性,确保应用根目录指向正确的路径。 如果希望进一步优化性能,还可以考虑启用 AJP 协议代替 HTTP 协议进行通信。此时需更新 Nginx 配置如下: ```nginx location ~ \.jsp$ { include uwsgi_params; uwsgi_pass 127.0.0.1:8009; # 默认AJP端口为8009 } ``` 同时确认 Tomcat 已开启 AJP Connector 组件支持[^5]。 #### 4. 测试部署效果 完成以上步骤后重启服务生效命令分别为: ```bash sudo systemctl restart nginx /usr/local/tomcat/bin/shutdown.sh && /usr/local/tomcat/bin/startup.sh ``` 最后可通过浏览器尝试访问不同类型的网页链接来检验功能是否正常工作。 --- ### 三、最佳实践建议 - **缓存机制引入** 利用 Nginx 提供的强大缓存策略降低频繁读取硬盘带来的开销;对于变动较少的内容设置较长有效期时间戳[TTL](Time To Live)[^2]。 - **SSL 加密传输保障安全性** 当前互联网环境下 HTTPS 成为主要趋势之一,因此务必为网站添加 SSL/TLS 认证证书保护敏感数据交换过程免受中间人攻击威胁[^3]. - **高可用架构设计** 构建基于负载均衡器(Load Balancer)之上由多台物理机组成的分布式计算集群模型从而达到更高的可靠性和吞吐量目标。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值