nginx +tomcat
nginx 是web应用服务器,作为高性能服务器,拥有许多功能,反向代理,静态化,负载均衡等
今天实现对静态文件的配置
用nginx对请求进行处理,tomcat负责后台业务逻辑
首先,在linux安装nginx
yum install nginx
service nginx start
然后对nginx进行配置
vim /etc/nginx/conf.d/default.conf
配置如下
upstream tomcat_server{#配置虚拟主机
server localhost:8080;
}
server {
listen 80 default_server;#监听的端口
#listen [::]:80 default_server;
server_name localhost;#server名字
root /usr/file;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html;
}
location ~ .*$ {#配置tomcat
proxy_pass http://tomcat_server;#虚拟主机
proxy_set_header Host $host;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {#配置静态资源
root /usr/file;
expires 30d;
}
location ^~ /static {#配置静态文件夹路径
alias /usr/file/static;
}
location ^~ /var {
alias /var;
}
service nginx restart
访问路径就是host:80/tomcat的访问路径
在浏览器中可以看到nginx,代表配置成功!