Nginx. conf 설정 설명

8907 단어 Nginx
배치 상세 설명
Nginx 기본 설정
###########            。#################
#user administrator administrators;  #       ,   nobody nobody。
#worker_processes 2;  #        ,   1
#pid /nginx/pid/nginx.pid;   #  nginx          
error_log log/error.log debug;  #      ,  。           ,http ,server ,     :debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #         ,        ,   on
    multi_accept on;  #                  ,   off
    #use epoll;      #      ,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #     ,   512
}
http {
    include       mime.types;   #             
    default_type  application/octet-stream; #      ,   text/plain
    #access_log off; #          
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #     
    access_log log/access.log myFormat;  #combined         
    sendfile on;   #  sendfile      ,   off,   http ,server ,location 。
    sendfile_max_chunk 100k;  #                    ,   0,     。
    keepalive_timeout 65;  #      ,   75s,   http,server,location 。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #  
    }
    error_page 404 https://www.baidu.com; #   
    server {
        keepalive_requests 120; #         。
        listen       4545;   #    
        server_name  127.0.0.1;   #           
        location  ~*^.+$ {       #   url  ,    ,~      ,~*       。
           #root path;  #   
           #index vv.txt;  #     
           proxy_pass  http://mysvr;  #    mysvr         
           deny 127.0.0.1;  #   ip
           allow 172.18.5.54; #   ip           
        } 
    }
}

전역 설정
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


        main           

user    nginx worker           ,  nobody    

worker_processes  nginx         ,
               (   M~  M  )          ,
     CPU        

error_log                
【debug / info / notice / warn / error / crit】

pid      id        

worker_rlimit_nofile                     

이벤트: nginx 작업 모드 설정
events {
    worker_connections  1024;
    multi_accept on;
    use epoll;
}


       nginx               

worker_connections                ,
       ,        worker processes     。

multi_accept     nginx                       

use epoll             ,   linux2.6+,  epoll,
   BSD Mac   Kqueue

http: http 프로 토 콜 정 보 를 위 한 설정
  web   ,http   nginx        ,         ,
                ,               ,
     ,        


http {
    ##
    #     
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ##
    # SSL    
    ##
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    ##
    #     
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    ##
    # Gzip     
    ##
    gzip on;
    gzip_disable "msie6";
    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript
 text/xml application/xml application/xml+rss text/javascript;
    ##
    #       
    ##
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
 
sendfile on:  on sendfile    ,                  ,
           ,             

tc_nopush on: nginx               ,          

tcp_nodelay on: nginx      ,        ,          
           ,                 ,       

keepalive_timeout 10:            ,               。
        ,   nginx       

client_header_timeout 10:          

client_body_timeout 10:          

send_timeout 10:           ,                 ,
           

limit_conn_zone $binary_remote_addr zone=addr:5m :        key   
     .

limit_conn addr 100:    key       

server_tokens:     nginx      ,           nginx  
   ,              

include /etc/nginx/mime.types:                  

default_type application/octet-stream:                 

type_hash_max_size 2048:    ,       ,         ,
   key      ,      ;   key,      ,     ,
      

2)     
access_log logs/access.log:           
error_log logs/error.log:             

3) SSL    
ssl_protocols:             ,nginx 1.1.13 1.0.12     
 ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1 TLSv1.2   
OpenSSL >= 1.0.1 ,SSLv3                     。

ssl prefer server ciphers:         ,              ,
              
4)     
gzip    nginx  gzip         。             。

gzip_disable          gzip  。     IE6         
          。

gzip_static   nginx       ,        gzip      。 
           (           ),          
  ,  nginx           (      gzip_static   , 
    )。

gzip_proxied                    。     any,  
          。

gzip_min_length                。        1000  ,
         ,                          。

gzip_comp_level          。       1-9       ,9 
          。     4,           。

gzip_type            。           ,       
     。

5)       
open_file_cache                  ,       。  
              ,              20     。

open_file_cache_valid  open_file_cache              。

open_file_cache_min_uses    open_file_cache             
      。

open_file_cache_errors                    ,     
        。           ,            。  
             ,                。

server: 서버 접근 정보 설정
srever     http         ,            ,
               

server {
        listen        80;
        server_name localhost    192.168.1.100;
        root        /nginx/www;
        index        index.php index.html index.html;
        charset        utf-8;
        access_log    logs/access.log;
        error_log    logs/error.log;
        ......
}

server:         ,  http       server

server_name:     ip      ,           

root:    server         ,       web      

index:    web        

charset:    www/               

access_log:                        

error_log:                        

location: 접근 루트 설정 에 사용
location   nginx            ,            
                 、          ,  location
               


location / {
          root    /nginx/www;
          index    index.php index.html index.htm;
}
location /:         

root:          ,       web  

index:           ,           
 
 
location /api {
            proxy_pass http://www.baidu.com;

            #               

            proxy_redirect     off; 

            #   Web       X-Forwarded-For      IP

            proxy_set_header   Host $host; 

            proxy_set_header   X-Real-IP $remote_addr; 

            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 

            client_max_body_size       10m; #                

            client_body_buffer_size    128k; #                  

            proxy_connect_timeout      300; #nginx            (      )

            proxy_send_timeout         300; #           (      )

            proxy_read_timeout         300; #     ,         (      )

            proxy_buffer_size          4k; #       (nginx)             

            proxy_buffers              4 32k; #proxy_buffers   ,     32k     

            proxy_busy_buffers_size    64k; #        (proxy_buffers*2)

            proxy_temp_file_write_size 64k; #         ,     ,
                                                                  upstream    

    }

upstream: 부하 균형 설정 에 사용
upstream             ,
                      

  upstream name {
        ip_hash;
        server 192.168.1.100:8000;
        server 192.168.1.100:8001 down;
        server 192.168.1.100:8002 max_fails=3;
        server 192.168.1.100:8003 fail_timeout=20s;
        server 192.168.1.100:8004 max_fails=3 fail_timeout=20s;
  }
 
ip_hash:        ,   weight      ,    

server host:port:          

-- down:         

-- max_fails:        ,            

-- fail_timeout:          ,               

2. 크로스 에이전트
 http          server     ,   server       

  location /demo/ {
    root html;
    index index.html index.htm;
  }

               location    /demo       
      (root html),         index.html
![image.png](https://upload-images.jianshu.io/upload_images/17573670-8a75ac3d34411088.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


nginx 기본 명령
//    nginx   ,         ,  
nginx -s stop
//  nginx   ,         ,          
nginx -s quit
//  nginx   
nginx -s reload

좋은 웹페이지 즐겨찾기