开发者社区> 问答> 正文

请教一个关于nginx的问题。

我的本地环境是apache,云服务器的环境是nginx.
为是提高网站性能,减少请求和节省宽带,我本地的apache环境下的程序都会将js和图片以及css等缓存到客户端,apache只需要在..htaccess文件中写入以下代码就可以实现:
<FilesMatch “.(gif|jpg|jpeg|png|ico|swf|js|css)$”>
Header set Cache-Control “max-age=2592000″
</FilesMatch>

但我的服务器环境是nginx,按网上的教程,实现这个功能的配置是:
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires      30d;
        }
        location ~ .*\.(js|css)?$ {
            expires      30d;
        }

但是,我这样配置nginx后,网站出错了,所有js/css都加载不来,图片显示不出来,更不用说缓存到本地了,请问一下要怎么解决这个问题?
我的nginx.conf如下:


user  www www;
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  10240;
}

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.77time.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /mnt/wwwroot/dianying;
            rewrite ^(.*)movie(.*)\.html$ $1movie.php?id=$2 last;
            index index.php index.html index.htm;
        }
        #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   /mnt/wwwroot/dianying;
        }
        # 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$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /mnt/wwwroot/dianying$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires      30d;
        }
        location ~ .*\.(js|css)?$ {
            expires      30d;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server {
        server_name 77time.com;
rewrite ^(.*) http://www.77time.com$1 permanent;
    }
}


请懂的人帮忙看看。

展开
收起
ap5001p1v 2012-07-29 23:14:37 12354 0
5 条回答
写回答
取消 提交回答
  • 吸取积极的价值,增加自己的信任值。

    二十五史资源多多,曹雪芹立书的王熙凤的下场一样凄凉封建颠覆体系的结果!我曾用尽全力,所以不会遗憾。塑造区块链溯源二十五史工程项目资源研究:https://mp.weixin.qq.com/s/7x1MwwpLWUgR6t3V59qeAw

    2021-12-12 13:02:08
    赞同 展开评论 打赏
  • 技术文章,帮顶哈。
    2012-07-30 17:38:53
    赞同 展开评论 打赏
  • Re请教一个关于nginx的问题。
    引用第2楼ap6214f2r于2012-07-30 02:23发表的  :
    你在针对CSS等文件只设置了缓存。连根目录都没有指定

    在location {}段里加入       root   /mnt/wwwroot/dianying;

    高手啊!!
    2012-07-30 17:06:17
    赞同 展开评论 打赏
  • 你在针对CSS等文件只设置了缓存。连根目录都没有指定

    在location {}段里加入       root   /mnt/wwwroot/dianying;
    2012-07-30 02:23:46
    赞同 展开评论 打赏
  • Re请教一个关于nginx的问题。
    急需帮忙

    -------------------------

    回2楼ap6214f2r的帖子
    嗯,是这个问题,刚解决了,没有设置目录。
    2012-07-30 00:15:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载