Loading

nginx相关优化

1.配置监控nginx状态信息

vim /usr/locale/nginx/conf/nginx.conf

server {
          listen 8080;
          server_name   192.168.1.30;
          location / {
                stub_status on;
                access_log off;
                allow 192.168.1.150;   ##只允许该IP访问
                deny all;
        }
    }

访问浏览器显示

2.我们都知道在apache下可以配置访问web服务器的某个路径时,自动显示其目录下面的文件列表的,其实Nginx一点也不比apache弱,它当然也可以实现这个功能,而且还非常容易和简单;主要用到autoindex 这个参数来开启,其配置如下

server {
    listen 80;
    server_name  www.nginx.com nginx.com;
    location / {
        root /data/www/file;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
        index index.html index.php;
    }
    access_log /tmp/test.log;

}

3.配置extra

vim conf/nginx.conf
增加该行即可:
include extra/vhost.conf;

添加extra

[root@lnmp nginx]# ls conf/extra/
vhost.conf

 

缓存优化

location / {
            root   html;
            index  index.html index.htm index.php;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {  
           expires      10y;
           root   html;
        }
        location ~ .*\.(js|css)?$
        {
           expires      360d;
            root   html;
        }

可以利用YSlow工具进行查看

缓存时间发生了改变

也可以用curl -I进行查看

 

压缩优化

#error_page  404              /404.html;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;   #4个单位为16K的内存作为压缩结果的缓存流,默认是原始数据相同大小的内存
        gzip_http_version 1.1;
        gzip_comp_level 2;        #9为最高级别
        gzip_types  text/plain application/x-javascript text/css application/xml;
        gzip_vary on;             #是前端的缓存服务器可以通过Nginx压缩的数据

 

错误页面跳转

server {
    listen 80;
    server_name  www.nginx.com nginx.com;
    location / {
        root /data/www/file;
        index index.html index.php;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       {
          root /data/www/file;
          expires 10y;
          }
    error_page  403   /403.html;             #相对于/data/www/file
    access_log /tmp/test.log;
}

IE 浏览器有时候没有优雅显示,加大404或者403的html页面就显示了。

 

posted @ 2016-09-01 00:10  头痛不头痛  阅读(210)  评论(0编辑  收藏  举报