nginx+tomcat动静态资源分离

#user nobody;
worker_processes 2;

#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;
charset utf-8;
#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;
# 启用内核复制模式,应该保持开启达到最快IO效率
sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
# HTTP1.1支持持久连接alive
# 降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
keepalive_timeout 65;

# 启动内容压缩,有效降低网络流量
gzip on; 
# 过短的内容压缩效果不佳,压缩过程还会浪费系统资源
gzip_min_length 1000;
# 可选值1~9,压缩级别越高压缩率越高,但对系统性能要求越高
gzip_comp_level 4;
# 压缩的内容类别
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

# 静态文件缓存
# 最大缓存数量,文件未使用存活期
open_file_cache max=655350 inactive=20s;
# 验证缓存有效期时间间隔
open_file_cache_valid 30s;
# 有效期内文件最少使用次数
open_file_cache_min_uses 2;
 

upstream web_app {

server 192.168.0.4:8080 weight=1;
server 192.168.0.4:8081 weight=2;
}

upstream web_test {

server 192.168.0.4:8081 ;
}

server {
listen 8888;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root html;
# index index.html index.htm index.jsp index.do;;
# proxy_pass http://web_app;
#}

#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
{ 
root html; 
#expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
expires 7d; 
} 
location /test {
deny all;
}
location /web {
proxy_pass http://web_test;
}
#所有jsp、do的动态请求都交给后面的tomcat处理 location ~ (\.jsp)|(\.do)$ 
location /
{

#tomcat地址
proxy_pass http://web_app; 

# 请求头中Host信息
proxy_set_header HOST $host; 
# 真实的客户端IP
proxy_set_header X-Real-IP $remote_addr; 
# 代理路由信息,此处取IP有安全隐患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
# 真实的用户访问协议
proxy_set_header X-Forwarded-Proto $scheme;
# 默认值default,
# 后端response 302时 tomcat header中location的host是http://192.168.1.62:8080
# 因为tomcat收到的请求是nginx发过去的, nginx发起的请求url host是http://192.168.1.62:8080
# 设置为default后,nginx自动把响应头中location host部分替换成当前用户请求的host部分
# 网上很多教程将此值设置成 off,禁用了替换,
# 这样用户浏览器收到302后跳到http://192.168.1.62:8080,直接将后端服务器暴露给浏览器
# 所以除非特殊需要,不要设置这种画蛇添足的配置
proxy_redirect default;
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; 
}

#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 Apache's document root
# concurs with nginx's 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;
# }
#}

}

 

你可能感兴趣的:(nginx+tomcat动静态资源分离)