nginx 기초 지식 분석

16749 단어 nginx기초 해석
작업 구조 모델 은?   nginx 는 전단 역방향 대리 처리 요청 을 하고 백 엔 드 는 lamp (phop - fpm 도 대리 이 며 동적 웹 페이지 요청 을 대리 처리 합 니 다. 또한 Xcache 를 phop - fpm 로 동적 웹 페이지 가속 처리) 또는 lnmp 를 설치 할 수 있 습 니 다.
기초 지식 이해   1. 기초 지식 대리   
  :               ,             ,                                       ,                          
           
     ,          ,             ,           ,       ,       ,    httpd          ,           ,        ,              ,    ,        ,         ,          。     
         
    :          ,         ,              ,         ,             。    
       :             ,           ,                  
         
    :                  ,        ,                     ,          ,       。

  
패 킷, http 응용 층 첫 번 째, tcp 첫 번 째, ip 첫 번 째, 프레임 첫 번 째  
대리 서비스 작업 원리    다단 계 에이전트: 클 라 이언 트 에 게 어떤 대 리 를 거 쳤 는 지 알 게 합 니 다.    nginx: 작업 원리    
  :                ,                     
  :                 ,               ,          ,           ,           
squal nginx         ,              。        ,      ,     

2. 메모리 및 디스크 자원 배분   
1、client_body_in_file_only on|cleaan|off
http              ; off    ,       0          ,                 ,clean      ,   off
clean                       
2、client_body_in_single_buffer on|off     http           buffer  ;   off
3、client_body_buffer_size size     nginx   HTTP          
4、client_body_temp_path dir-path [level1 [level2 [level3]]];        http         
5、client_header_buffer_size size;                 http  header      buffer  ,   k
6、large_client_header_buffers number size         http       buffer     
7、connection_pool_size size               nginx         tcp             ,                ;   256,
2 8   ,    
8、request_pool size size    nginx     http             ,                ;   4k

nginx 의 두 가지 용도
정적 콘 텐 츠 의 웹 서버   역방향 에이전트
컴 파일 설치 시 epoll 사용 에 주의 하 십시오.   막다    막다    비 차단, 비동기
위 에서 아래로 하 는 것 이 하나의 성능 보다 좋다.
2. Nginx 설치:
1、      
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y && yum install openssl-devel pcre-devel -y
2、  
      nginx,      nginx    :    # groupadd -r nginx     # useradd -r -g nginx nginx
mkdir /data
         :    # ./configure \       --prefix=/usr/local/nginx \     
--error-log-path=/data/applogs/nginx/error.log \     
--http-log-path=/data/applogs/nginx/access.log \  
--pid-path=/var/run/nginx/nginx.pid  \      
--lock-path=/var/lock/nginx.lock \       
--user=nginx \      
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/client/ \ 
--http-proxy-temp-path=/usr/local/nginx/proxy/ \
--http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \ 
--http-scgi-temp-path=/usr/local/nginx/scgi \ 
--with-pcre

# make && make install
시작 스 크 립 트
#!/bin/sh    
#     
# nginx - this script starts and stops the nginx daemon     
#     
# chkconfig:   - 85 15     
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \     
#               proxy and IMAP/POP3 proxy server     
# processname: nginx     
# config:      /etc/nginx/nginx.conf     
# config:      /etc/sysconfig/nginx     
# pidfile:     /var/run/nginx.pid     
# Source function library.     
. /etc/rc.d/init.d/functions     
# Source networking configuration.     
. /etc/sysconfig/network     
# Check that networking is up.     
[ "$NETWORKING" = "no" ] && exit 0     
nginx="/usr/local/nginx/sbin/nginx"     
prog=$(basename $nginx)     
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"     
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx     
lockfile=/var/lock/subsys/nginx     
make_dirs() {     
   # make required directories     
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`     
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`     
   for opt in $options; do     
       if [ `echo $opt | grep '.*-temp-path'` ]; then     
           value=`echo $opt | cut -d "=" -f 2`     
           if [ ! -d "$value" ]; then     
               # echo "creating" $value     
               mkdir -p $value && chown -R $user $value     
           fi     
       fi     
   done     
}     
start() {     
    [ -x $nginx ] || exit 5     
    [ -f $NGINX_CONF_FILE ] || exit 6     
    make_dirs     
    echo -n $"Starting $prog: "     
    daemon $nginx -c $NGINX_CONF_FILE     
    retval=$?     
    echo     
    [ $retval -eq 0 ] && touch $lockfile     
    return $retval     
}     
stop() {     
    echo -n $"Stopping $prog: "     
    killproc $prog -QUIT     
    retval=$?     
    echo     
    [ $retval -eq 0 ] && rm -f $lockfile     
    return $retval     
}     
restart() {     
    configtest || return $?     
    stop     
    sleep 1     
    start     
}     
reload() {     
    configtest || return $?     
    echo -n $"Reloading $prog: "     
    killproc $nginx -HUP     
    RETVAL=$?     
    echo     
}     
force_reload() {     
    restart     
}     
configtest() {     
  $nginx -t -c $NGINX_CONF_FILE     
}     
rh_status() {     
    status $prog     
}     
rh_status_q() {     
    rh_status >/dev/null 2>&1     
}     
case "$1" in     
    start)     
        rh_status_q && exit 0     
        $1     
        ;;     
    stop)     
        rh_status_q || exit 0     
        $1     
        ;;     
    restart|configtest)     
        $1     
        ;;     
    reload)     
        rh_status_q || exit 7     
        $1     
        ;;     
    force-reload)     
        force_reload     
        ;;     
    status)     
        rh_status     
        ;;     
    condrestart|try-restart)     
        rh_status_q || exit 0     
            ;;     
    *)     
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"     
        exit 2     
esac

chmod +x nginx    그렇지 않 으 면 이런 상황 이 다시 발생 한다.    env: /etc/init.d/nginx: Permission denied
1. 실제 웹 방문 로 그 를 기록 합 니 다.   
proxy_set_header X-Real-IP $remote_addr;
LogFormat "%{X-Real-IP}i    service httpd restart
#gzip  on;   
       upstream webservers {      
       server 192.168.1.144 weight=3 max_fails=2 fail_time=3;
       server 192.168.1.145 weight=1 max_fails=2 fail_time=3;
       server 127.0.0.1 backup;
       ip hash session   
       lest_conn     
       }
       server {            
       listen 80;                     
       server_name www.node3.com;             
       proxy_set_header X-Real-IP $remote_addr; 
        
     http   LogFormat "%{X-Real-IP}i  
    ,         add_header X-Via $server_addr;          ,     ip,              

2. nginx 가 설치 할 때 주의해 야 할 작은 디 테 일     
         
location / {                             
proxy_pass http://webservers;                             
proxy_set_header       ip                 
proxy_hide_header                         
proxy_pass_header                        
proxy_pass_request_body        http                               proxy_pass_request_header    http                      
}             
}
    # another virtual host   
server {            
listen 8080;             
server_name 127.0.0.1;   #              
root /www/backup;            
 }

3. 일부 명령 의 의미 와 사용 규칙
proxy_redirect [default|off|redirect|replacement]    상위 서버 가 되 돌아 오 는 응답 이 방향 을 바 꾸 거나 새로 고침 요청 일 때 proxyredirect 는 http 의 첫 번 째 location 이나 refresh 를 다시 설정 합 니 다.    proxy_redirect http://localhost:8080/imgs  http://www.org.com/p_w_picpaths     proxy_redirect http://localhost:8080/imgs http://www.org.com/p_w_picpaths     홈 페이지 재 작성
upstream  정의 서버 그룹   서버 그룹 에서 상위 서버 정의    max_fails times     weight     backup     ip hash
proxy_buffer_size size          upsteam                  
proxy_buffering on|off     upsteam        ,      ,nginx                  
       proxy_buffering on  
proxy_max_temp_file_size   0         ,           ,                    
proxy_cache_path            
nginx           ,         mem-cache     
nginx     
(zone name)hash -----    ---        
    :                      ,     ,          wanish              
levels      
proxy_cache_use_stale error      
 server       
  nginx  (         )             
key       100m                   
4、    
    
proxy_cache_path /cache/webserve levels=1:2 keys_zone=web:100m max_size=1g inactive=12h;
          
server {            
listen 80;                     
server_name www.node3.com;             
proxy_set_header X-Real-IP $remote_addr;  
     http   LogFormat "%{X-Real-IP}i      ,                 
add_header X-Via $server_addr;          ,     ip,                                        
add_header X-Cache $upstream_cache_status;                      
           
             location / {                             
             proxy_pass http://webservers;                             
             proxy_set_header   #    ip                 
             proxy_hide_header  #                       
             proxy_pass_header  #                      
             proxy_pass_request_body     #   http                
             proxy_pass_request_header #   http                       
             proxy_cache web; #                     
             proxy_cache_valid 200 2h;                 
             proxy_cache_vaild 301 302 10m;                 
             proxy_cache_vaild any 1m; #                     
             }             
             }
mkdir /cache/webserver   
/cache/webserver #          
    
   ls               ,f12  hit    
cdn       

전역 서비스 캐 시 서버 
5. nginx 에 대해 동정 분 리 를 실현 한다.
            location              
            server {            
            listen 80;                    
            server_name www.node3.com;            
            proxy_set_header X-Real-IP $remote_addr;       http   
            LogFormat "%{X-Real-IP}i      ,                 
add_header X-Via $server_addr;          ,     ip,                                     
            add_header X-Cache $upstream_cache_status;                                      
            location / {                             
            #proxy_pass http://webservers;       nginx                   
            proxy_pass http://192.168.1.144;                                       proxy_set_header       ip                 
            proxy_hide_header                         
            proxy_pass_header                        
            proxy_pass_request_body        http                               proxy_pass_request_header    http                                      proxy_cache web;                      
            proxy_cache_valid 200 2h;                 
            proxy_cache_vaild 301 302 10m;                 
            proxy_cache_vaild any 1m; #                         
            }                      
            location ~* \.(jpg|jpeg|png|gif)$ { #                                   proxy_pass http://192.168.1.145;                 
            proxy_cache web;                      
            proxy_cache_valid 200 2h;                 
            proxy_cache_vaild 301 302 10m;           
            proxy_cache_vaild any 1m; #                         
            }             
            }

6. 기타 설명
lnmp  nginx 서버 자체 가 정적 콘 텐 츠 서 비 스 를 제공 합 니 다.    php 에 대한 요청 은 fastcgi 모듈 프 록 시 php - fpm 서버 를 통 해 이 루어 집 니 다.
httpd,nginx    cg 프로 토 콜, 유 니 버 설 게 이 트 웨 이 프로 토 콜
동적 웹 페이지 언어
php  jsp  python  perl
cgi   module   fastcgi
네 가지 모형   prework
event
thread
php - fpm 의 작업 원리   prework 모델 과 같 습 니 다.
python  web    uwsgi 프로 토 콜    isp   web     http 프로 토 콜    php    fastcgi 프로 토 콜    perl  web     http 프로 토 콜
php-fpm    mysql 이나 maridb 를 먼저 설치 합 니 다.    설치    php - fpm 재 설치
7. phop - fpm 결합 nginx 의 설치 설명  php 서비스 스 크 립 트 제공    압축 해제 중   
cp php.ini-production /etc/php.ini     
2、  php-fpm          
cp sapi/fpm/init.d.php /etc/rc.d/init.d/php-fpm     
3、  php-fpm         
cd  /usr/local/php/etc/     
cp php.fpm.conf.default php-fpm.conf     
  php-fpm.conf     
1、pid       pid       
[global]     
pid = /usr/local/php/var/run/php-fpm.pid     
2、        
error.log = /var/log/php-fpm     
pm = dynaminc        
pm.max.childen = 128               
pm.start-server = 5   php-fpm            
pm.max-space-server = 5            
pm.max-requests = 500                  0        ,    ,         
pm.status-path = /status            
ping.path = /ping    
ping.response = pong    
php-fpm         web     ,http    
php-fpm        
fastcgi_index: php          
http             
fastcgi_cache_path /cache/fastcgi levels=1:2 keys_zone=fcgi:50m  max_size=1g inactive=12h;     
 location     ,               
fastcgi_cache   proxy_cache           
                server {             listen 80;                     
                server_name www.node3.com;     
                location ~* \.(jpg|jpeg|png|gif)$ {                                    fastcgi_pass http://192.168.1.145;                 
                fastcgi_cache fcgi;                      
                fastcgi_cache_valid 200 2h;                 
                fastcgi_cache_vaild 301 302 10m;                 
                fastcgi_cache_vaild any 1m;                          
                }     
                }
                fastcgi_connect_timeout #  fastcgi            
                fastcgi_send_timeout # fastfcgi                 
                http://www.org.com/test.php

8. 건강 상태 페이지 설정
  fast_cgi           
            location ~* /(status|ping) 
            {         
            root    /www/a.com;         
            fastcgi_pass    127.0.0.1:9000;         
            fastcgi_param     SCRIPT_FILENAME $fastcgi_script_name;         
            include fastcgi_params;         
            }
                  http://www.a.com/status    
            http://www.a.com/status?html

9. 건강 상태 페이지 의 표시 이해
pool    
        
           
                    
cd /usr/local/php/etc     
vim php-fpm.conf     
process manager dynamic     
         static dynamic     
start time 25/Apri              
start since 2150              
acceptnd conn  4021                  
listen queue                    
max listen queue 90     fpm                    
listen queue len 128                
idle processes 4                
active processes 5               
total processes 5              
max active process 8     ftp                
max children  reached 0                   
slow requests 0            

10. memcache 의 도입
 
         :malloc()、free()            ,         ,       ,    ,               memcache                  memcache,                   ,                  ,    ,            ,         ,               ,                   。                  ,        ,            ,            ,         ,                   。    
  page    slab         ,    1MB   
  chunk:               
  slab class      
  chunk            
  memcache  :    ,     
      memcache                ,                              ,      ,      ,                  ,           ,   ,               ,    ,                        ,(            ,    ,                  ,          ,        ,         。
  memcache    ,        hash  ,                 ,       memcache  ,      。
          
libevent :epoll,poll,select
  memcache    
     libevent     
    memcache     
       rpm   

11. 명령 기본 설명  
yum install memcache -y
memcached  
-l           
-d              
-u               
-m              ,   64M     
-c        ,  1024     
-p    tcp  ,   tcp  ,  12111    
-u    udp  ,0    ,     
-M                       
-f       ,   1.25     
-n chuck    ,key+value+flags    48
memcached -u memcached #  memcached    
ss -tulp     
memcached -u memcached -f 1.5 -vv
      set add replace append  prepend    
        get delete incr/decr     
      :stats ,stats items , stats slabs, stats sizes     
      flush_all
memcache       ,           
       ,     memcached API,memcached            
memcached:        
memcached:php  memcached             
memcache: php  memcached                
libmemcached: c      
memcache      

좋은 웹페이지 즐겨찾기