这篇文章主要介绍了使用 nginx+tomcat 实现静态和动态页面的分离,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。其将源代码以类 BSD 许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
博主最近在优化一个 javaweb 项目,该项目之前一直都是使用 tomcat 处理用户请求的,无论静态还是动态的东西,一律交给 tomcat 处理。tomcat 主要是负责处理 servlet 的,静态的文件还是交给 nginx 处理,nginx 对静态文件的处理比 tomcat 不是只快了一点,并且 Nginx 的使用对项目并发能力有很大的提升。下面主要记录下主要的配置过程:
实验环境:windows
实验工具:Nginx、tomcat
windows 下安装 Nginx 非常简单,去官网下载压缩包解压后并且双击解压目录下的 nginx.exe 程序即可。然后在浏览器输入 localhost 可出现下图,即表示 nginx 已经在工作。
nginx 的工作流程是:对外,nginx 是一个服务器,所有的请求都先请求到 nginx,然后再由 nginx 对内网进行请求的分发到 tomcat,然后 tomcat 处理完请求后将数据发送给 nginx,然后由 nginx 发送给用户,整个过程对用户的感觉就是 nginx 在处理用户请求。既然这样子,nginx 肯定需要进行配置,主要的配置文件是 conf 文件夹下的 nginx.conf,因为我主要是进行了静态与动态分离,所以没有进行静态文件缓存,也没有进行负载均衡的配置。
- #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 {
- #nginx默认最大并发数是1024个用户线程
- 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;
- #http1.1在请求完之后还会保留一段时间的连接,所以这里的timeout时长不能太大,也不能太小,
- #太小每次都要建立连接,太大会浪费系统资源(用户不再请求服务器)
- keepalive_timeout 65;
- #gzip on;
- server {
- #nginx监听80端口
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- #这里的/表示所有的请求
- #location / {
- #将80端口的所有请求都转发到8080端口去处理,proxy_pass代表的是代理路径
- # proxy_pass http://localhost:8080;
- # root html;
- # index index.html index.htm;
- #}
- #对项目名进行访问就去访问tomcat服务
- location /Student_Vote {
- proxy_pass http://localhost:8080;
- }
- #对jsp和do结尾的url也去访问tomcat服务
- location ~ \.(jsp|do)$ {
- proxy_pass http://localhost:8080;
- }
- #对js、CSS、png、gif结尾的都去访问根目录下查找
- location ~ \.(js|css|png|gif)$ {
- root F:/javaweb;
- }
- #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;
- # }
- #}
- }
上面的配置中我把默认的 location / 给注释掉了,因为它会拦截所有的请求,无论是动态还是静态,还有一个就是对静态文件的配置我配置成了 javaweb 的工作区间,接下来会说明为什么。
因为之前写的项目一直以来都是使用 jsp 内置对象来进行目录的文件访问,但是使用了 nginx 一切都需要改变,当我使用了 nginx,并且项目没有进行路径的修改的时候,总是无法加载静态文件,查看日志发现这样的错误:2016/05/20 18:27:30 [error] 6748#6936: *225 CreateFile()"F:/javaweb/Student_Vote/lib/images/username.png"failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /Student_Vote/lib/images/username.png HTTP/1.1", host: "localhost", referrer: "http://localhost/Student_Vote/index.jsp",大致信息是根据 jsp 中文件的配置,nginx 将会从 / Stdent_Vote(这是我的项目名)/lib/images 包中查找静态文件,而我又不想对项目文件做太大变化,其实还有一种方法是不使用 jsp 的内置对象,直接使用 http://localhost/username.png 来代替内置对象访问静态文件,但是这样改要改很多的地方,所以我就直接将 web-inf 文件夹下的 lib 文件夹拷到上一个文件夹,也就是该文件夹和 web-inf 文件夹是兄弟文件夹的关系。
通过上述操作,就实现了动态与静态的分离了,无图无真相,下面展示效果图。
上图可以看到 server 是 "Apache-Coyote/1.1"。tomcat 的连接器就是这个。
而上面的 server 可以看到是 nginx, 说明对外而言接收请求的服务器是 nginx。
来源: http://www.phperz.com/article/17/0316/320212.html