nginx+tomcat动静分离

Posted

tags:

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

nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat处理动态页面。




nginx主配置文件


[[email protected] conf]# vi /usr/local/nginx/conf/nginx.conf


#user  nobody;
worker_processes  2;
worker_cpu_affinity 00000001 00000010;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#error_log /www/log/nginx_error.log crit;
pid        logs/nginx.pid;
worker_rlimit_nofile 1024;

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

    #gzip  on;

  server_names_hash_bucket_size 512;
  client_header_buffer_size 20k;
  large_client_header_buffers 4 40k;
  client_max_body_size 80m;
  tcp_nopush     on;


  proxy_connect_timeout    5;
  proxy_read_timeout       60;
  proxy_send_timeout       5;
  proxy_buffer_size        16k;
  proxy_buffers            4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;



fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 160k;
fastcgi_buffers 16 160k;
fastcgi_busy_buffers_size 320k;
fastcgi_temp_file_write_size 320k;
tcp_nodelay on;

gzip on;
gzip_min_length  1k;
gzip_buffers     4 160k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;




upstream oneself{
    server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 172.23.145.12:8080 weight=1 max_fails=2 fail_timeout=30s;
}


include vhosts.conf;




vhosts.conf内容


[[email protected] conf]# vi /usr/local/nginx/conf/vhosts.conf

server
  {
    listen       80;
    server_name www.struggle.com struggle.com;
server
  {
    listen       80;
    server_name www.struggle.com struggle.com;
    index  index.jsp index.html index.htm;
#    root  /usr/local/nginx/html/jsz;

    location /
    {
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         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_pass http://oneself;
    }


    location ~ .*\.(jsp|cgi|shtml)?$
    {
         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_pass http://oneself;
    }

    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
                root  /usr/local/nginx/html/jsz;
                expires      30d;
    }
                access_log  /usr/local/nginx/logs/access.log;
                error_log   /usr/local/nginx/logs/error.log;

   }

一定要把图片全放在/usr/local/nginx/html/jsz路径下,不然网页不显示图片



配置后端Tomcat服务器,这里省略哦,启动2个tomcat

最后检查Nginx配置是否正确即可,然后测试动静分离是否成功,删除后端tomcat服务器上的某个静态文件,查看是否能访问,如果可以访问说明静态资源nginx直接返回了,不走后端tomcat服务器。







本文出自 “jiazheming” 博客,请务必保留此出处http://8888866666.blog.51cto.com/6988153/1784505

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

Nginx+Tomcat动静分离架构

Nginx---动静分离

Nginx+Tomcat负载均衡动静分离

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

Nginx+Tomcat负载均衡及动静分离

Nginx + Tomcat 实现 负载均衡 和 动静分离群集