学习Nginx

1、安装nginx

yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
wget  http://nginx.org/download/nginx-1.22.0.tar.gz
useradd -M -s /sbin/nologin nginx
mkdir -p /opt/nginx
chown -R nginx:nginx /opt/nginx
tar xf nginx-1.22.0.tar.gz 
cd nginx-1.22.0/
./configure --user=nginx --group=nginx --prefix=/opt/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module 
make 
make install
ln -s  /opt/nginx-1.9.12/  /opt/nginx

2、反向代理

# cat nginx.conf
  upstream backend {
    ip_hash;                                                       #ip会话保持
    server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s;  #失败3次后
    server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s;  #30秒内不发请求
  }

  location / {
    root   html;
    index  index.html index.htm;
    proxy_pass http://backend;
  }

3、tcp代理

案例1:
# cat nginx.conf
  stream {  
    upstream tcp_proxy {
    hash $remote_addr consistent;        #远程地址一致性hash算法
    server 192.168.0:11:22;
     }
  server {
    listen 2222;
    proxy_connect_timeout 1s;            #后端连接超时时间
    proxy_timeout 10s;                   #后端响应超时时间
    proxy_pass tcp_proxy;
      }
  }

案例2:
# cat nginx.conf
  server {
    listen 3306 so_keepalive=on;        #开启tcp长连接
    proxy_pass 172.17.0.3:3306; 
    proxy_connect_timeout 5s;           #建立连接时间
    proxy_timeout 3600s;                #保持连接时间
  }
###so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
###so_keepalive=30m::10;
###keepidle:等待时间,keepintvl:发送间隔,keepcent:发送次数

其他:
proxy_timeout;                          #没有数据时,连接保持时间,默认75秒
proxy_connect_timeout;                  #建立连接超时, 不超过75s
proxy_send_timeout;                     #发送超时时间,默认为60秒
proxy_read_timeout;                     #接收超时时间,默认为60秒
# sysctl -a  |grep tcp_keepalive        
  net.ipv4.tcp_keepalive_time = 1200    #tcp在多少秒后,没有数据传输,启动探测
  net.ipv4.tcp_keepalive_probes = 9     #探测次数
  net.ipv4.tcp_keepalive_intvl = 75     #探测间隔

 4、性能测试及优化

1、静态测试:
# ab -n 2000 -c 2 http://www.abc.com/test.html
  ab                                                #n(总请求数),c(并发数),k(是否开启长连接)
  Server Software: nginx/1.12.2                     #程序名
  Server Hostname: www.abc.com                      #域名
  Server Port: 80                                   #端口
  Document Path: /test.html                         #访问的页面
  Document Length: 228 bytes                        #内容的长度
  Concurrency Level: 2                              #并发数          
  Time taken for tests: 1.777 seconds               #测试的时间
  Complete requests: 2000                           #完成的请求数量
  Failed requests: 0                                #失败的请求数量
  Write errors: 0                                   #写入错误数量
  Total transferred: 708000 bytes                   #总共传输的数据量(文本内容和请求头)
  HTML transferred: 22800 bytes                     #html文件数据量(test.html)
  Requests per second: 1125.37 [#/sec] (mean)       #每秒的请求数
  Time per request: 1.777 [ms] (mean)               #每个请求耗费的时间
  Time per request: 0.889 [ms] (mean,all requests)  #服务器完成一个请求的时间
  Transfer rate: 389.05 [Kbytes/sec] received       #传输速度
                       
2、动态测试:                       
# ab -n 20 -c 2 http://www.abc.com/test.jsp                       
  Document Path: /test.jsp                          #访问的页面
  Document Length: 669 bytes                        #内容的长度
  Concurrency Level: 2                              #并发数          
  Time taken for tests: 55.03 seconds               #测试的时间
  Complete requests: 20                             #完成的请求数量
  Total transferred: 99600 bytes                    #总共传输的数据量(文本内容和请求头)
  HTML transferred: 66900 bytes                     #html文件数据量(test.html)
  Requests per second: 1.36 [#/sec] (mean)          #每秒的请求数
  Time per request: 553.3 [ms] (mean)               #每个请求耗费的时间
  Time per request: 271.6 [ms] (mean,all requests)  #服务器完成一个请求的时间
  Transfer rate: 1.1 [Kbytes/sec] received          #传输速度
                   
3、优化
# cat /etc/security/limits.conf
  * hard nofile 1000000
  * soft nofile 1000000            
#ulimit -a
# cat nginx.conf
  worker_processes   4;                             #进程数
  worker_cpu_affinity auto;                         #自动绑定cpu 
  worker_rlimit_nofile 655350;                      #nginx文件打开数
  worker_connections  10240;                        #每个进程最大连接数
  sendfile  on;                                     #开启高效传输模式
  keepalive_timeout 65;                             #长连接超时时间
  gzip  on;                                         #gzip压缩
# ps -eo pid,args,psr |grep [n]ginx                 #进程绑定不同的cpu 

 5、nginx日志误删后恢复

前提:没有重启nginx
1、备份nginx的error日志并删除
# cp error.log error.log-bak
# rm -f error.log
       
2、查找nginx日志目录                       
# ps -elf | grep nginx          #查看pid
# cd /proc/10177/fd             
# ls -la                        #查看nginx日志目录

3、恢复
# cat 3  > error.log            #重定向到文件,3是文件名

 6、网络名词解释

1、静态网页
常见扩展名:
  纯文本:如htm、html、xml、shtml、js、css
  图片或数据文档:如jpg、gif、png、bmp、txt、doc、ppt
  视频类:mp4、swf、avi、wmv、flv
特点:
1)客户端解析,效率更高
2)后端没有数据库,节约成本
3)地址里没有特殊符号,如 && ?等
4)方便百度收录
5)方便cdn缓存

2、动态网页
常见扩展名:
  .asp、.aspx、.php、.jsp、.do、.cgi
特点
1)服务端解析,效率慢
2)地址里有特殊符号,例如&& ?3)后端有数据库,内容来自于数据库中
4)磁盘存在动态程序文件,内容没有实体文件
5)以数据库技术为基础,降低了网站维护的工作量
6)采用动态技术,实现用户交互,用户可以注册、登录、购物、发博文等 

3、伪静态网页
利用rewrite技术把动态网页伪装成静态网页(URL地址改写)
特点:
1)便于搜索引擎收录,提升用户体验和网站的访问量
2)消耗资源,性能下降,

4、总结:
静态效率高,静态客户端解析
动态消耗资源大,动态服务端解析
性能方面:静态好
功能方面:动态好
维护方面:动态好
5、网站访问量 IP:独立的IP次数,一个电脑的公网IP PV:访问一个网站页面次数,点击量 UV:一台客户端设备访问网站次数,独立访客 并发数:单位时间内服务器能够处理的最大连接数

 7、Nginx 变量参数

$args                    #请求中的参数值
$query_string            #同 $args
$arg_NAME                #GET请求中NAME的值
$is_args                 #如果请求中有参数,值为"?",否则为空字符串
$uri                     #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"。
$document_uri            #同 $uri
$document_root           #当前请求的文档根目录或别名
$host                    #优先级:HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名.请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称
$hostname                #主机名
$https                   #如果开启了SSL安全模式,值为"on",否则为空字符串。
$binary_remote_addr      #客户端地址的二进制形式,固定长度为4个字节
$body_bytes_sent         #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$bytes_sent              #传输给客户端的字节数
$connection              #TCP连接的序列号
$connection_requests     #TCP连接当前的请求数量
$content_length          #"Content-Length" 请求头字段
$content_type            #"Content-Type" 请求头字段
$cookie_name             #cookie名称
$limit_rate              #用于设置响应的速度限制
$msec                    #当前的Unix时间戳
$nginx_version           #nginx版本
$pid                     #工作进程的PID
$pipe                    #如果请求来自管道通信,值为"p",否则为"."
$proxy_protocol_addr     #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
$realpath_root           #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
$remote_addr             #客户端地址
$remote_port             #客户端端口
$remote_user             #用于HTTP基础认证服务的用户名
$request                 #代表客户端的请求地址
$request_body            #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
$request_body_file       #将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传 递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off,uwsgi_pass_request_body off,or scgi_pass_request_body off
$request_completion      #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
$request_filename        #当前连接请求的文件路径,由root或alias指令与URI请求生成
$request_length          #请求的长度 (包括请求的地址,http请求头和请求主体)
$request_method          #HTTP请求方法,通常为"GET""POST"
$request_time            #处理客户端请求使用的时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$request_uri             #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
$scheme                  #请求使用的Web协议,"http""https"
$server_addr             #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
$server_name             #服务器名
$server_port             #服务器端口
$server_protocol         #服务器的HTTP版本,通常为 "HTTP/1.0""HTTP/1.1"
$status                  #HTTP响应代码
$time_iso8601            #服务器时间的ISO 8610格式
$time_local              #服务器时间(LOG Format 格式)
$cookie_NAME             #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
$http_NAME               #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
$http_cookie        #请求的所有cookie
$http_host               #请求地址,即浏览器中你输入的地址(IP或域名)
$http_referer            #url跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent         #用户终端浏览器等信息
$http_x_forwarded_for    #客户端的IP和代理服务器的IP,以逗号隔开;可伪造
$http_x_forwarded_proto  #请求的协议
$sent_http_NAME          #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location

8、nginx  匹配不同目录

方法1:
    location ~ ^/(.+)/(.+)/(.+\.js)$ {
        alias /web/jar/mcms/template/1/pc/$3;
    }

方法2:
    location ~ ^/.+/.+/.+\.png$ {
        root /web/jar/mcms/template/1/pc/;
        rewrite ^/(.+/)(.+\.png)  /images/$2  break;
    }

方法3:
    location ~ ^/a/.+/.+\.png$ {
          root /web/jar/mcms/template/1/pc/;
      if($http_user_agent ~* (iphone|android)) {
          root /web/jar/mcms/html/phone/;
    }
        rewrite ^/(.+/)(.+\.png)  /images/$2 break ;
    }

方法4:
    location /c/ {
       alias /web/jar/mcms/template/1/pc/images/;
    }

方法5:
    location /b/ {
               root    /web/jar/mcms/template/1/pc/;
    location /b/ {
               alias   /web/jar/mcms/template/1/pc/;
        }
}

方法6:
    location  ~* \.png$  {
               root   /web/jar/mcms/template/1/pc/;
        }

 

 

 

posted on 2019-09-02 16:31  五光十色  阅读(291)  评论(0编辑  收藏  举报

导航