由于项目后台使用的是node,然而node不适合对静态资源的处理,因为他的异步处理(事件轮询)机制,所以更擅长的是密集I/O型的应用,所以我就有了一个想法,使用nginx来做反向代理,当请求的是静态资源的时候,直接由nginx(监听80端口)自己处理并返回,其他非静态资源请求转发至node(8080端口),由node来处理。下面是我的nginx配置文档,nginx安装请自行百度,大把资料啦~

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name www.guangzhouyueyang.com; #charset koi8-r; #access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
root html;
expires 30d;
}
location ~ .*\.(css)?$
{
root html;
expires 1h;
}
location ~ .*\.(js)?$
{
root html;
expires 1h;
} location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
#proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://localhost:8080;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
# }
#} }

  配置解释:当请求是以(css, js, gif, jpg, jpeg, png, bmp, swf)结尾的时候则nginx直接拦截请求(上面源码中标红的部分),由nginx静态服务器直接返回静态资源,而如果不是的话则将请求转发到node服务(上面源码中标蓝的部分),由node来完成其余请求。

使用nginx处理静态资源请求,其余交给node的更多相关文章

  1. nginx配置静态资源与动态访问分离【转】

    在前面的博客中<说说 NGINX 的配置及优化>的 2.5 小节里面,提到 location 模块是 nginx 中用的最多的,也是最重要的模块,负载均衡.反向代理.虚拟域名等都与它相关. ...

  2. nginx 配置静态资源路径(url不同于static path)

    目的         用nginx做静态资源代理可以减少请求对后台服务器的压力,使响应更加迅速. 配置        情景一           url : 127.0.0.1:8000/images ...

  3. Nginx之静态资源WEB服务

    本篇主要记录学习Nginx的静态资源WEB服务的几种常见的功能记录学习 Nginx开发常用的命令 nginx -tc /etc/nginx/nginx.conf vim /etc/nginx/conf ...

  4. nginx发布静态资源

    nginx发布静态资源 参考 ngx_http_index_module index指令 ngx_http_core_module http指令 location指令 listen指令 root指令 ...

  5. nginx缓存静态资源,只需几个配置提升10倍页面加载速度

    nginx缓存静态资源,只需几个配置提升10倍页面加载速度 首先我们看图说话 这是在没有缓存的情况下,这个页面发送了很多静态资源的请求:   1.png 可以看到,静态资源占用了整个页面加载用时的90 ...

  6. nginx 作为静态资源web服务

    Nginx作为静态资源web服务 静态资源web服务-CDN场景 Nginx资源存储中心会把静态资源分发给“北京Nginx”,“湖南Nginx”,“山东Nginx”. 然后北京User发送静态资源请求 ...

  7. Nginx作为静态资源web服务之防盗链

    Nginx作为静态资源web服务之防盗链 首先,为什么需要防盗链,因为有些资源存在竞争对手的关系,比如淘宝的商品图片,不会轻易的让工具来爬虫爬走收集.但是如果使用防盗链,需要知道上一个访问的资源,然后 ...

  8. Nginx作为静态资源web服务之文件读取

    Nginx作为静态资源web服务之文件读取 文件读取会使用到以下几个配置 1. sendfile 使用nginx作为静态资源服务时,通过配置sendfile可以有效提高文件读取效率,设置为on表示启动 ...

  9. Nginx作为静态资源web服务之缓存原理

    Nginx作为静态资源web服务之缓存原理 大致理一下http浏览器缓存原理: 浏览器第一次请求服务器,此时浏览器肯定没有缓存,则直接调用服务器端,服务器在返回的信息的信息头中添加 ETag和Last ...

随机推荐

  1. NET Framework 版本和依赖关系

    原文:https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/versions-and-dependencies 每个版本的 ...

  2. 用vue实现简单分页

    在这个demo中,我用vue对一个json文件中的数据进行了简单的分页,没用用到交互,一下是我的实现过程. 基础逻辑 1.将json文件引入app.vue,并作为data返回 data(){ var ...

  3. 第六节 etc/passwd 、etc/shadow 、 useradd 、 groupadd

    调优方法原理:禁用atime的修改来节省cpu和内存资源.命令:mount noatime disk 1.配置文件1. /etc/passwd文档结构:由":" 分隔成7个字段&q ...

  4. Qt中不同类型数据之间的相互转换

    int类型转换为QString类型 ; QString string_data; string_data = QString::number(int_data,);//10进制 qDebug() &l ...

  5. js把通过图片路径生成base64

    主要思想: 使用canvas.toDataURL()方法将图片的绝对路径转换为base64编码. 一.图片在本地服务器: var imgSrc = "img/1.jpg";//本地 ...

  6. 使用图片地图减少HTTP请求数量

    前言 最近在看<高性能网站建设>,记录一下所学. 现在很多网站都是图片形式的导航,点击图片跳转到对应的链接.如果导航项目很多的话,图片的数量就会很多,每需要加载一张图片就会多一个HTTP请 ...

  7. HBase跨地区机房的压测小程序——从开发到打包部署(图文版)

    今天做了一个跨地区机房的压测小程序,主要的思路就是基于事先准备好的rowkey文件,利用多线程模拟并发的rowkey查询,可以实现并发数的自由控制.主要是整个流程下来,遇到了点打包的坑,所以特意记录下 ...

  8. 前端学习_02_vps、web服务器、域名申请

    vps申请 国内比较好用的服务器:阿里云,青云:在国内申请ip比较方便,但是必须要备案域名,否则马上就会被封禁掉. 话说我也有点自己的思路想做个网站,服务器还真的是个问题. 小型的网站只需要ECS服务 ...

  9. Ruby学习之对象模型

    这两周工作内容较多,平时自己也有点不在状态,学的东西有点少了,趁着现在还有点状态,赶紧复习一下之前学习的Ruby吧. Ruby是我真正开始接触动态语言魅力的第一个语言,之前虽然也用过且一直用perl. ...

  10. NestedScrollingParent, NestedScrollingChild 详解

    之前听同事提起过 NestedScrollingView,但是一直没有时间去了解,最近一段时间比较空,才开始去了解.先点开,看 NestedScrollingView 源码: public class ...