Nginx笔记
零、换源
sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list apt-get clean
一、vim有conf语法高亮
编译源码包中有contrib文件夹,供我们做语法高亮使用。
cp -r contrib/vim/* /usr/share/vim/vimfiles/
cp -r contrib/vim/* /etc/vim/
二、热更新,无感版本升级
编译安装的时候不要执行make install 。将make好的二进制文件(C语言编译后的文件都会放在src目录)nginx复制到老的sbin文件夹中。
//先备份老的nginx二进制文件 cp nginx nginx.old //将编译好的新版本nginx二进制文件复制进sbin中 cp nginx.new nginx ps -ef | grep nginx root 12716 1 0 2018 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www-data 12717 12716 0 2018 ? 00:00:12 nginx: worker process www-data 12718 12716 0 2018 ? 00:00:20 nginx: worker process www-data 12719 12716 0 2018 ? 00:00:11 nginx: worker process www-data 12720 12716 0 2018 ? 00:00:05 nginx: worker process root 20486 20043 0 00:07 pts/0 00:00:00 grep --color=auto nginx
//找到当前nginx的master进程发送热更新信号,此时会新启一个master进程(新版本nginx),老的worker也在进行,但是新的worker会以新版本在运行,平滑的过渡
kill -USR2 12716 //现在通知老的worker进程发出信号,优雅的关闭worker进程 kill -WINCH 12716 //可查看网络端口的进程 netstat -apn | grep tcp| grep nginx
三、日志文件的切割
生产环境中access.log和error.log往往会非常庞大,我们必须定时(每天)去切割移走,怎么优雅的将文件搬走,又让nginx重新讲日志写入log中呢?
通过mv命令可以将access.log移动走,这里为什么不用cp而用mv呢,因为linux文件系统中,改名并不会影响已经打开文件的写入操作,内核inode不变,这样就不会出现丢日志了,如果使用cp则在cp和重启中间会遗失一部分日志信息。
mv access.log access_bak.log nginx -s reopen //或者使用kill -USR2 nginx.pid 重新拉起一个worker
每日文件备份使用contrab
四、编译安装的常用modules
http_image_filter_module:
可进行nginx层面的图片裁剪缩放,但是处理图片有限(不能加水印),适用于简单的图片。依赖 gd库的支持 。
http_upstream_ip_hash_module:
均衡负载ip哈希策略。
http_v2_module:
http2的使用。http与http2的区别
apt-get install libgd-dev
./configure --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_image_filter_module=dynamic --without-http_upstream_ip_hash_module --without-http_upstream_hash_module --with-http_mp4_module
更多的modules可以在 http://nginx.org/en/docs/ 查看
五、静态资源网站的搭建
gzip压缩静态资源
gzip on; # enable gzip
gzip_http_version 1.1; # turn on gzip for http 1.1 and above
gzip_disable "msie6"; # IE 6 had issues with gzip
gzip_comp_level 3; # inc compresion level, and CPU usage
gzip_min_length 100; # minimal weight to gzip file
gzip_proxied any; # enable gzip for proxied requests (e.g. CDN)
gzip_buffers 16 8k; # compression buffers (if we exceed this value, disk will be used instead of RAM)
gzip_vary on; # add header Vary Accept-Encoding (more on that in Caching section)
# define files which should be compressed
gzip_types text/plain;
gzip_types text/css;
gzip_types application/javascript;
gzip_types application/json;
gzip_types application/vnd.ms-fontobject;
gzip_types application/x-font-ttf;
gzip_types font/opentype;
gzip_types image/svg+xml;
gzip_types image/x-icon;
六、反向代理缓存
proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
server {
listen 80;
server_name study_nginx.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache my_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 302 1d;
proxy_pass http://local;
}
}
七、GoAccess可视化access.log
goaccess access.log -o /usr/local/nginx/html/report.html --real-time-html --time-format='%H:%M:%:S' --date-format='%d/%b/%Y' --log-format=COMBINED
八、Nginx反向代理获取真实ip
问题:这里的场景是多机器,从A机器进来,proxy转发到B机器,在B机器获取的ip是A的内网ip。
解决方案:
安装http_realip_module模块 --with-http_realip_module
server {
listen 80;
server_name xxx.com; # 添加域名
set_real_ip_from 172.25.78.11; # 真实服务器上一级代理的IP地址或者IP段,可以写多行
real_ip_header X-Forwarded-For; # 告知Nginx真实客户端IP从哪个请求头获取
real_ip_recursive off; # 是否递归解析,off表示默认从最后一个地址开始解析
location / {
return 200 "client real ip: $remote_addr\n";
}
}

浙公网安备 33010602011771号