Skip to content

Instantly share code, notes, and snippets.

@zhangskills
Created September 4, 2014 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangskills/907084648e4d721f5195 to your computer and use it in GitHub Desktop.
Save zhangskills/907084648e4d721f5195 to your computer and use it in GitHub Desktop.
nginx备忘
@zhangskills
Copy link
Author

Gzip 压缩配置

gzip(GNU-ZIP)是一种压缩技术。经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多。gzip的压缩页面需要浏览器和服务器双方都支持,实际上就是服务器端压缩,传到浏览器后浏览器解压并解析。浏览器那里不需要我们担心,因为目前的巨大多数浏览器都支持解析gzip过的页面。
Nginx的压缩输出有一组gzip压缩指令来实现。相关指令位于http{….}两个大括号之间。

gzip on;
//该指令用于开启或关闭gzip模块(on/off)
gzip_min_length 1k;
//设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 4 16k;
//设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
gzip_http_version 1.1;
//识别http的协议版本(1.0/1.1)
gzip_comp_level 2;
//gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types text/plain application/x-javascript text/css application/xml
//匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
gzip_vary on;
//和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

nginx 配置gzip段如下:

gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

@zhangskills
Copy link
Author

采用apache的rotatelogs进行按天分割

  1. 从一台已经安装好apache的机器上的apache bin目录下把rotatelogs拷贝到本机的/opt/nginx_logs下

    mkdir -p /opt/nginx_logs
    cp rotatelogs /opt/nginx_logs/
    chmod +x /opt/nginx_logs/rotatelogs
  2. 创建有名管道

    mkdir /opt/nginx_logs/abc_www/
    mkfifo /opt/nginx_logs/abc_www/access_log
  3. 在nginx配置文件server中加上

    access_log /opt/nginx_logs/abc_www/access_log;

  4. 创建日志目录

    mkdir /opt/nginx_logs/abc_www/log/

  5. 创建日志分割脚本

    [root@localhost nginx_logs]# cat /opt/nginx_logs/abc_www/rotate.sh
    baselogdir=/opt/nginx_logs/abc_www
    rotatelogs=/opt/nginx_logs/rotatelogs
    while [ 1 ]
    do
    echo `date +"%F %T"`" rotatelogs access start"
    $rotatelogs $baselogdir/log/access_%Y%m%d.log 86400 480 < $baselogdir/access_log
    echo `date +"%F %T"`" rotatelogs access stop"
    sleep 1;
    done
  6. 创建启动日志分割脚本

    [root@localhost nginx_logs]# cat /opt/nginx_logs/abc_www/run.sh
    sh /opt/nginx_logs/abc_www/rotate.sh >> /opt/nginx_logs/abc_www/log/access-rotate.log 2>&1 &

http://blog.chinaunix.net/uid-11121450-id-3177198.html

@zhangskills
Copy link
Author

Nginx反向代理映射成子路径

多个web应用共享一个域名和端口时,可以考虑把不同的web应用映射成不同的子路径,这个子路径在Java EE里称作ContextPath。下面的配置片段解决nginx作为前端,反向代理多个tomcat主机,通过不同子路径共享一个域名的情况。

server {
    listen       80;
    server_name  _;
    index index.html index.htm index.php;
    root /home/dashboard;

    location /dashboard {
        rewrite /dashboard/(.*) /$1 break;
        rewrite ^/dashboard$ /dashboard/ permanent;
        proxy_pass http://127.0.0.1:9082;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

http://codelife.me/blog/2013/09/12/reverse-proxy-for-a-subdirectory-in-nginx/

@zhangskills
Copy link
Author

子目录跳转:

upstream abc{
        server 127.0.0.1:8000;
}

server {
  listen  80;

  server_name abc.com;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;

 location ~ .*\.(?:ico|css|js|gif|jpe?g|png|html)$ {
        proxy_pass http://abc;
        expires 30d;
 }

  location / {
        rewrite /(.*)$ /player/$1 break;
        proxy_pass http://abc;
  }

  error_page   500 502 503 504  /50x.html;
  error_page   404  /404.html;
  location = /50x.html {
    root   html;
  }
  location = /404.html {
    root   /mnt/website/abc;
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment