使用nginx处理静态资源请求,其余交给node

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

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    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;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        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  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;
    #    }
    #}

}

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

时间: 2024-08-10 06:40:29

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

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

nginx缓存静态资源,只需几个配置提升10倍页面加载速度 首先我们看图说话 这是在没有缓存的情况下,这个页面发送了很多静态资源的请求: 1.png 可以看到,静态资源占用了整个页面加载用时的90%以上,而且这个静态资源还是已经在我使用了nginx配置压缩以后的大小,如果没有对这些静态资源压缩的话,那么静态资源加载应该会占用这个页面展示99%以上的时间.听起来是不是已经被吓到了,但是数据已经摆在这里了,这可不是危言耸听. 然后再看看使用了nginx缓存之后的效果图: 2.png 看到没有,朋友们

nginx发布静态资源

nginx发布静态资源 参考 ngx_http_index_module index指令 ngx_http_core_module http指令 location指令 listen指令 root指令 server指令 server_name指令 步骤 创建静态资源 为 conf/nginx.conf http模块中新增server模块 静态资源结构 E:\mozq\00store\frxx ├─frxx │ bug.png │ weixin.png server模块配置 server{ list

linux使用Nginx搭建静态资源服务器

最近公司需要做一个宣传片播放  视频有点大 好几百M 就想到使用Nginx来代理静态资源,在过程中出现了一些问题,比如端口没开.访问是403等,没有成功,后面慢慢查找问题,才发现大部分博客资料的都不全,所以在这里记录一下. 安装过程本文就不提了 网上都有很多 本文主要说明 nginx.conf 的配置 如下: 进入编辑nginx.conf 文件 输入密码 配置nginx.conf 上传文件到配置的路径 在网页地址栏输入对应的地址 如果出现 nginx静态资源文件无法访问,403 forbidde

静态资源请求缓存

静态资源请求是有缓存机制的 , 这是浏览器自带的特性 例如我请求服务器上的某文件 , 但是该文件在我请求后被操作过 , 我再次请求拿到的还是原来的文件 , 你抓包的时候会发现你第二次请求的没走 , 抓不到, 解决方法 就是在get请求后面携带一段不一样的字符串 , 这段字符串没有实际意义 , 你可以是一段哈希字符串 , 可以是时间戳 , 只要保证你请求的不重复就好 原文地址:https://www.cnblogs.com/sunjinggege/p/9962713.html

百度UEditor图片上传、SpringMVC、Freemarker、Tomcat、Nginx、静态资源

个人官网.公司项目都需要 可视化编辑器,百度UEditor做得很不错,就用的这个.项目后台用到了SpringMVC.Freemarker,开发过程中部署在Jetty,线上部署用Tomcat,最后可能配置Nginx代理.     在实际使用过程中,遇到了太多的问题,因此有必要梳理和总结下. 1. 先说百度UEditor在Java环境中的使用:1.1   Html页面或者Freemarker模版里,引入百度UEditor的相关JS和CSS,如下 <script type="text/javas

HappyAA服务器部署笔记2(nginx的静态资源缓存配置)

我近期对服务器进行了少量改进,虽然之前使用了nginx反向代理之后性能有所提高,但仍然不够,需要使用缓存来大幅度提高静态资源的访问速度. 服务器上的静态资源主要有这些:png, jpg, svg, js, css等.下面,我通过新的nginx配置来实现缓存.对红色的字我会额外进行说明. worker_processes 1; events { worker_connections 1024; multi_accept on; use epoll; } http { include mime.ty

利用Proxy Cache使Nginx对静态资源进行缓存

前言 Nginx是高性能的HTTP服务器,通过Proxy Cache可以使其对静态资源进行缓存.其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应. 配置Proxy Cache 以下为nginx配置片段: proxy_temp_path   /usr/local/nginx/proxy_temp_dir 1 2; #keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB #/usr/local/

Nginx配置静态资源缓存时间及实现防盗链

环境源主机:192.168.10.158系统:centos 7.4域名:www.wuxier.cn盗链主机:192.168.10.191(使用Nginx+Tomcat实现负载均衡.动静分离的实验主机,点我进行复盘)系统:centos 7.4域名:www.ajie.com 和 www.taobao.com 创建软件包存放目录 [[email protected] ~]# mkdir /root/software [[email protected] ~]# cd /root/software/ [

nginx 代理静态资源报 403

用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R nobody /usr/local/nginx/html 或者在nginx的配置文件nginx.conf中添加 use root; 之后刷新nginx的配置 /usr/local/nginx/bin/nginx -s reload 原文地址:https://www.cnblogs.com/flyi