Nginx随笔

 

正向代理:正向代理代理的是客户端

反向代理:反向代理代理的是服务端

 

Ngnix支持接近5万个并发连接数的响应且对内存和cpu消耗很低

 

营业员访问页面整个流程(面试题)

webapp/html/css/图片)

 

./configure --prefix=/usr/local/ngnix

yum -y install  zlib-devel

yum -y install pcre-devel

 

Make  && make install

 

启动和停止服务

./nginx -c 配置文件

./nginx -c /usr/local/ngnix/conf/nginx.conf

 

 

Nginx进程模型

Master

worker

 

停止

./nginx -s stop  停止

./nginx -s quit   退出

./nginx -s reload 重新加载

发送信号的方式

Kill -QUIT master进程号  通过发送信号的方式让其停止,保证请求处理完

Kill -TERM 立即停止进程

 

核心配置

修改配置完成后验证配置文件

./nginx -t

Main(全局)

user  nobody表示用户和用户组

worker_processes 工作线程数,一般配置cpu的核心数

Event

worker_connections  1024网络允许连接数

http

 Server段:

 

 

虚拟主机的配置

 

基于域名的虚拟主机的配置

  server{

       listen       80;

        server_name  www.nginxtest.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html/test;

            index  index.html index.htm;

        }

}

本地修改host,测试  

 

基于IP的虚拟主机

 

基于端口的虚拟主机

 

   server{

        listen       9090;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html/port;

            index  index.html index.htm;

        }

}        

 

端口号相同的话在基于server_name 然后location匹配

 

http://www.nginxtest.com:9090/    

 

Nginx日志配置      

 

 

 

Log_format

#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                  

                     

 

自己配置一个日志格式

log_format mylogformat '$remote_addr - $remote_user [$time_local]

                           ';

 

access_log  logs/mylog.log  mylogformat      

 

关闭日志

Access_log  off

 

Nginx 日志切割

Crontab,定时执行的指令进行脚本控制

Mv acces.log acessDATETIME.log

Kill -   USRI  nginx     主进程号            让nginx重新生成一个日志     

 

     

 

location的语法和匹配规则

正则匹配

location [~|=|^~|~*] /uri {

 

}

~:一般匹配,区分大小写  location /uri{}

优先级高于正则匹配,一般匹配安装location的层级度(长度)./pre/test  /pre

=:精准匹配  location = uri{}  优先级最高

~* 正则匹配,不区分大小写

 

Location ~* \.(png|jpg|css|js)${

#匹配静态资源

}

Location ^~ /static/{

Root /webapps/ROOT/html

}

 

rewrite

 

 location / {

            rewrite '^/images/([a-z]{3})/(.*)\.(png)$' /huangjj?file=$2.$3 ;

            set $image_file $2;

            set $image_type $3;

        }

        location /huangjj {

             root html;

             try_files /$arg_file /image404.html;

        }

 

        location = /image404.html{

           return 404 "image not found exception" ;

        }

 

 

缓存设置

 

 

        location / {

            root html;

            index index.html index.html;

        }

        location ~ \.(png|jpg|js|css|gif)$ {

           root html/images;

          expires 5m;

         }

 

直接回车就不访问服务器,刷新页面还会访问服务器,做到缓存?

 

服务器端的缓存

 

Io 模型 epoll   io多路复用  linux

 

Gzip压缩

 

浏览器请求 -> 告诉服务端当前浏览器可以支持压缩类型->服务端会把内容根据浏览器所支持的压缩策略去进行压缩返回

->浏览器拿到数据以后解码;  常见的压缩方式:gzipdeflate sdch

 

配置gzip on

 

 

反向代理:

 

 

增加请求头:

 

 

负载均衡

vpstream

ups支持的调度算法

ip_hash  根据iphash值来做转发

 

 

Weight  配置权重

 

 

默认是轮询机制

 

 

 

配置

Accept_mutx  加锁保证只有一个进程处理请求

 

进程模型

 

Master   worker

Epoll模型,异步非阻塞,io多路复用

Master  接受请求  安排worker,由于多个进程竞争,所以用Accept_mutx  进程锁,每个worker是单线程的,用io多路复用,异步非阻塞

 

 

Sendfile  开启高效传输模式

 

posted on 2017-12-21 16:30  huangjj369  阅读(112)  评论(0)    收藏  举报