nginx安装目录
修改nginx.conf文件配置负载均衡配置Tomcat集群并设置动静分离
#user nobody; error_log logs/error.log; worker_processes 2; worker_rlimit_nofile 1024; events { worker_connections 1024; } http { log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘"$status" $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for" ‘ ‘"$gzip_ratio" $request_time $bytes_sent $request_length‘; access_log logs/access.log main; 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; #gzip on; upstream tomcat_server { server localhost:9001 weight=1; #server localhost:9002 weight=1; } server { listen 80; server_name 10.67.16.226; charset utf-8; root static; #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。 location ~ .*\.(htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|svg|eot|ttf|woff|woff2)$ { root static; #static前面不加 ‘/‘ 表示从nginx安装目录为根目录所以静态资源就可以放到static文件夹下, 加 ‘/‘ 表示已我们的本地磁盘根目录(如我D://static), 如果错误可以看logs下的error.log #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 7d; } 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://tomcat_server; } location ~ ^/(WEB-INF)/ { deny all; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
我的网站的静态资源都要从resource文件夹下读取(说明: 下面页面以.html结尾的不是完全静态的是局部静态, 就是使用ajax推送数据首页的数据多数是推送), 但是每次读取都是从tomcat下的resource直接读, 比如jquery, css, bootstrap之类的静态文件我现在就要把他们放到nginx;
看看下面的tomcat的root目录下并没有resource文件夹, 静态资源从nginx上直接读取并让客户端缓存; 我的tomcat端口为9001,下面来验证一下静态文件是从nginx上读取的;
访问本地9001端口tomcat
下面是访问nginx服务器(静态文件访问nginx动态由tomcat处理);
登录后
OK, 大功告成;
时间: 2024-10-07 06:32:58