nginx 설치 및 상용 설정

13475 단어
nginx 설치 및 상용 설정
nginx 소스 코드 설치
0       :yum -y install pcre-devel zlib-devel openssl-devel
1     nginx-1.14.0.tar.gz 
2    tar xf nginx-1.14.0.tar.gz
    cd nginx-1.14.0/
    ./config --help
    ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf 
    --user=nginx --group=nginx 
    --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log 
    --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock
     --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module 
    --with-debug

3   : /usr/local/nginx/sbin/nginx 
4   :ss -tlnp              ps aux            
nginx  :  epoll,aio,mmap
    master/worker,      

설정 사용
vim /etc/profile.d/nginx.sh
     export PATH=/usr/local/nginx/sbin:$PATH
. /etc/profile.d/nginx.sh
nginx -t
nginx -s reload
nginx -h       

 정상 실행 에 필요 한 설정
    1 user USERNAME【GROUPNAME】;  worker       
        user nginx nginx;
    2 pid /path/to/pid_file #pid  

    3 worker_rlimit_nofile #;       (worker)              

    4 worker_rlimit_sigpending#;          worker        

성능 최적화 관련 설정
1 worker_processes
1 worker_processes #; worker     ,       cpu     1;        
                worker_processes auto;          cpu   ,      
          worker                :
        worker_process * worker_connections

2 work_cpu_affinity
2 work_cpu_affinity cpumask cpumask...;   cpu
        cupmask: 0001 0010 0100 1000
        worker_cpu_affinity 00000001 00000010 00000100 --->  cpu  

3worker_priority
3 worker_priority nice:  #nice   
        [-20,19]    0

4accept_mutex
4 accept_mutex[on|off]; worker             ,
         on    worker            ;

5lock_file 
5 lock_file /path/to/lock_file       

디 버 깅, 포 지 셔 닝 문제 설정:
1daemon
1 daemon off|on
             nginx;

2 master_process
2 master_process on|off;
       master/worker    nginx;

3 error_log
3 error_log  /path/to/error_log level;
              ;       ,     debug;         --with-debug,   

소켓 과 호스트 관련 명령
1 server {} 가상 호스트 정의
server{
    listen PORT;
    server_name NAME;
    root /path/to/documentroot;
}
  :
     1:  port:        
      2:  hostname    server_name          ;

2 listen
  default_server:        ;    IP  ,
                       server name ,      
      :listen 80 default_server;
    ssl:        ssl      
    spdy:spdy protocol(speedy)       spdy  
    http2:http version 2;  spdy   

3 server_name NAME[...];n 개의 server name 을 동시에 지정 할 수 있 습 니 다. 이름 은 어댑터 * 와 정규 표현 식 (~) 을 사용 할 수 있 습 니 다.
1       :  :www.example.com
    2      :  :*.example.com
    3     :  :www.example.*
    4      ,  :~^.*\.example\.com$
    4 default_server    

4 tcp_nodelay on|off; keepalive 모드 에서 의 연결 에 tcp 사용 여부nodelay 옵션, 기본적으로 사용 하지 않 음
5 tcp_nopush on|off; tcp 사용 할 지 여부nopush (freebse) 또는 tcpcork(Linux);sendfile 이 on 일 때 만 유용 합 니 다.묵인 off
6 sendfile on |off ;커 널 공간 에서 메시지 에 직접 응답 하려 면 on 이 어야 합 니 다.
경로 관련 명령 어
7 root  웹 자원 의 경로 맵 설정 하기;요청 한 url 에 대응 하 는 문서 의 디 렉 터 리 경 로 를 표시 합 니 다.
    server{
        ...
        root /data/www/vhost1;  :http://www.example.com/images/logo.jpg-->/data/www/vhost1/images/logo.jpg
    }
    server{
        ...
        location /images/{
            root /data/imgs/;  //  :http://www.example.com/images/logo.jpg-->/data/imgs/images/logo.jpg
        }
    }

8 location [= | ~ | ~ * | ^ ~] url {...} 사용자 가 요청 한 uri 가 정 의 된 각 location 과 일치 하도록 합 니 다.
location @name{...}

=:URI       
~:        ,       
~*:        ,        
^~:URI       ,        

       :     ,^~ ,~ ~* ,     URL;

9 alias 별명 설정, 경로 별명 정의
        location   
    location /images/{
         root /data/imgs/;
    }
    location /images/{
        alias /data/imgs/;
    }
       :
    root  :      location “/”  URL;
        /images/test.jpg  -->/data/imgs/images/test.jpg
    alias  :      location “/url/”  URL;
        /images/test.jpg -->/data/imgs/test.jpg

10 index 는 일반적으로 첫 페이지 파일 을 가리 키 고 변 수 를 가 져 올 수 있 습 니 다. 예 를 들 어 서로 다른 언어의 첫 페이지, index file...
11 error_page code... 오류 페이지 를 사용자 정의 하고 http 상태 코드 에 따라 오류 페이지 로 변경 합 니 다.
   error_page 404   /404.html;
    error_page 404  =200 /404.html;    root  ,  200       

12 try_files
try_files file... url;
try_files file... =code;
     1  N-1   ,              :  1 N-1      ,        uri;
  uri   location  ,       
  :       location
root /data/imgs/;
index index.html;
location /images/{
    try_files index.html  /images/test1.html  /images/test2.html  /images/test3.html  //        
}
location=/images/test3.html{
    expires 10s;
}

사용자 요청 관련 설정
13 keepalive_timeout;keepalive 연결 의 시간 초과 시간 을 설정 하고 0 은 긴 연결 을 금지 합 니 다.기본 75 초
 
14 keepalive_requests number;keepalived 연결 에 요청 할 수 있 는 최대 자원 연결 수량;기본 값 100;
 
15 keepalive_disable 는 어떤 브 라 우 저 에서 keepalive 기능 을 사용 하 는 것 을 금지 하 는 지 알려 줍 니 다.
 
16 send_timeout;해당 메 시 지 를 보 내 는 시간 초과, 기본 값 은 60s 입 니 다.정확히 말 하면 두 번 조작 사이 의 시간 을 쓴다.
 
17 client_body_buffer_size 수신 클 라 이언 트 요청 메시지 body 의 버퍼 크기 는 기본적으로 16K (32 비트 시스템 은 8k) 입 니 다.지정 한 시간 을 초과 하면 디스크 에 저 장 됩 니 다. 공식 문 서 는 temporary file, 임시 파일 에 쓰 여 있다 고 합 니 다.
 18   client_body_temp_path path [level1 [level2 [level3]]]; 클 라 이언 트 가 body 를 요청 하 는 임시 파일 을 저장 할 곳 과 수량 을 설정 합 니 다.
    client_body_temp_path  /var/tmp/client_body 2 2;
클 라 이언 트 요청 에 대한 제한
19 limit_except METHOD{...}  지정 한 범위 밖의 다른 방법 에 대한 접근 제어
limit_except GET{

  allow 172.16.0.0/16;

  deny all;

}

20 limit_rate speed;클 라 이언 트 가 1 초 에 전송 할 수 있 는 바이트 수 를 표시 합 니 다. 기본 값 은 0 으로 무제 한 을 표시 합 니 다.
파일 작업 최적화 관련 설정
21  aio  on|off; 비동기 io 모드 사용 여부
22 directio   size|off    
23 open_file_cache off  
open_file_cache max=N [inactive=time]

nginx          :

(1)     ,              

(2)       ;

(3)                     ;

max=N             ,      ,    LRU                  

inactive=time:                        ,      ;

24 open_file_cache_errors on|off;
    

25 open_file_cache_valid time;
캐 시 에 있 는 캐 시 항목 의 유효성 을 얼마 간격 으로 검사 합 니까? 기본 값 은 60s 입 니 다.
26  open_file_cache_min_uses number;캐 시 항목 이 비 활동 기한 내 에 접근 해 야 할 최소 횟수;
 ngx_http_access_module 모듈 설정 (ip 기반 접근 제어)
27 allow address |CIDR |unix: | all;  접근 가능 한 주소
28 deny address |CIDR |unix: | all;   접근 금지 주소
ngx_http_auth_basic_module 모듈 설정 (basic 인증)
29 auth_basic string|off; http basic 인증 프로 토 콜 을 사용 하여 사용자 인증
30 auth_basic_user_file;사용자 인증 을 실현 하 는 계 정 파일;
    :
 name1:password1
 name2:password2:comment

    :
 1 encrypted with the crypt() function;
 2 md5  ;

예 를 들 면:
      :

location /admin/{
    auth_basic  "Admin Area"; 
  auth_basic_user_file /etc]/nginx/.ngxpasswd # }
yum install httpd -y # httpd
htpasswd -c -m /etc/nginx/.ngxhtpasswd tom # -c
htpasswd  -m /etc/nginx/.ngxhtpasswd jerry
nginx -s reload

 ngx_http_log_module: 모듈 설정 (접근 로그)
31 log_format 는 로그 형식 과 이름 을 정의 합 니 다. 로그 형식 은 일반적으로 내 장 된 변 수 를 통 해 정의 합 니 다.
32 access_log path [format[buffer=size[flush=time]]];
access_log off;
로그 파일 경로, 형식 이름과 캐 시 크기, 쓰기 시간 간격 에 접근 합 니 다.성능 향상 을 위해 버퍼 를 정의 하 는 것 을 권장 합 니 다.
33 open_log_file_cache max=N [inactive=time][min_uses=N][valid=time];
ngx_http_stub_status_module 모듈 설정: server 와 location 에 만 사용 할 수 있 습 니 다.
34 stub_status; 지정 한 uri 를 통 해 stub 출력status;
location /status/ {
   stub_status;  
}
        ,     ,        

active connections: 291
server accepts handle requests:
accepts handled requests
Reading 6 Writing 179 Waiting 106 ;

 ngx_http_referer_module 모듈 설정 (요청 메시지 의 Rerefer 첫 번 째 접근 제어 기반)
35 valid_referers none|blocked|server_names|string...   
none: 요청 메시지 에 referer 첫 번 째 부분 이 존재 하지 않 습 니 다.
blocked: 요청 메시지 에 referer 첫 번 째 부분 이 존재 하지만 유효한 값 이 없 거나 http: / 또는 https: / / 로 시작 하지 않 습 니 다.
server_names: 호스트 이름 입 니 다.
arbitrary string;직접 문자열, * 어댑터 사용 가능
regular expression: ~ 로 시작 하 는 정규 표현 식
내 장 된 변수: $invalidrefer (valid refer 가 지정 한 정의 에 부합 되 지 않 는 모든 인용 요청 은 비합법적 인 인용 입 니 다
도 난 방지 체인
valid_referers none blocked server_names  *.example.com  example.*   www.example.org/galleries/   ~\.google\;

if($invalid_referer){

  return 403;

}

도 난 방지 체인 의 예
location ~* \.(gif|jpg|png|jpeg)$ {       #          
    expires     30d;
    valid_referers *.hugao8.com www.hugao8.com m.hugao8.com *.baidu.com *.google.com;   #   ,       
    if ($invalid_referer) {
    rewrite ^/ http://www.hehe/404.jpg;  #           
    
    }
    }

 
 
 ngx http proxy module 모듈:
server{
  listen
  server_name
  location / {
    proxy_pass   http://localhost:8000/;
    proxy_set_header  Host   $host;  
    proxy_set_header X-Real_IP  $remote_addr;  #실제 방문 한 클 라 이언 트 ip
  }
}
형식:
  location  /url {          #이 url 경 로 는 존재 하지 않 을 수 있 습 니 다.
    proxy_pass http://back_server:port/newurl;
}
 
posted on
2018 - 09 - 13 09: 26 네 이 버 블 로그
...) 댓 글 (
...) 모음 집 편집
다음으로 전송:https://www.cnblogs.com/mmyy-blog/p/9638656.html

좋은 웹페이지 즐겨찾기