天天看點

nginx配置詳解

nginx配置詳解

nginx是一種服務端的負載均衡代理,Http代理,反向代理:作為web伺服器最常用的功能之一,尤其是反向代理。Nginx在做反向代理時,提供性能穩定,并且能夠提供配置靈活的轉發功能。Nginx可以根據不同的正則比對,采取不同的轉發政策。本篇部落客将收集到的配置選項總結出來,供小夥伴們參考。

#user  nobody;   #配置使用者或者組,預設為nobody nobody。
worker_processes  1;  #允許生成的程序數,預設為1

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;   #制定日志路徑,級别

#pid        logs/nginx.pid;   #指定nginx程序運作檔案存放位址

events {
    accept_mutex on;   #設定網路連接配接序列化,防止驚群現象發生,預設為on
    worker_connections  65535;   #最大連接配接數,預設為512
    multi_accept on;   #設定一個程序是否同時接受多個網絡連接配接,預設為off
}

http {
    include       mime.types;    #檔案擴充名與檔案類型映射表
    default_type  application/octet-stream;   #預設檔案類型,預設為text/plain

    #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  logs/acc.log  main;  #combined為日志格式的預設值
    
    sendfile        on;   #允許sendfile方式傳輸檔案,預設為off,可以在http塊,server塊,location塊
    tcp_nopush     on;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;    #連接配接逾時時間,預設為75s,可以在http,server,location塊
    
   upstream fzjh {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備 若無則是負載均衡 
    } 
    server {
        listen       8000;   #監聽端口
        server_name  localhost; #監聽位址

        charset utf-8; 

        #access_log  logs/host.access.log  main;  #combined為日志格式的預設值

        location / {   #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。
            #root path;  #根目錄
           #index vv.txt;  #設定預設頁
            proxy_pass http://127.0.0.1:3000;  #指向位址
           # proxy_pass  fzjh ;  請求轉向mysvr 負載均衡
           # deny 127.0.0.1;  #拒絕的ip
           # allow 172.18.5.54; #允許的ip  
        }

        location /check_token {   #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。
            proxy_pass  http://127.0.0.1:8088;   #指向位址
            # proxy_redirect     on;  
            # proxy_set_header   Host             $host;
            # proxy_set_header   X-Real-IP        $remote_addr;
            # proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        location /logout {  #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。

            proxy_pass http://127.0.0.1:8088;    #指向位址
            # proxy_redirect     on;  
            proxy_set_header   Host             $host  ;
            proxy_set_header   X-Real-IP        $remote_addr  ;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for  ;
        }

        location ~ \.aaaa$ {  #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。
        
                        rewrite ^/([\w]+)/(.*)\.air$ /$2 break ;
            proxy_pass http://127.0.0.1:8088;
            # proxy_redirect     on;  
            proxy_set_header   Host             $host  ;
            proxy_set_header   X-Real-IP        $remote_addr  ;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for  ;
        }
        
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {   #錯誤頁
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

      

2.其他配置

  1. include mime.types; #檔案擴充名與檔案類型映射表
  2. default_type application/octet-stream; #預設檔案類型,預設為text/plain
  3. #access_log off; #取消服務日志
  4. log_format myFormat ’ r e m o t e a d d r – remote_addr– remoteaddr–remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for’; #自定義格式
  5. access_log log/access.log myFormat; #combined為日志格式的預設值
  6. sendfile on; #允許sendfile方式傳輸檔案,預設為off,可以在http塊,server塊,location塊。
  7. sendfile_max_chunk 100k; #每個程序每次調用傳輸數量不能大于設定的值,預設為0,即不設上限。
  8. keepalive_timeout 65; #連接配接逾時時間,預設為75s,可以在http,server,location塊。
  9. proxy_connect_timeout 1; #nginx伺服器與被代理的伺服器建立連接配接的逾時時間,預設60秒
  10. proxy_read_timeout 1; #nginx伺服器想被代理伺服器組發出read請求後,等待響應的逾時間,預設為60秒。
  11. proxy_send_timeout 1; #nginx伺服器想被代理伺服器組發出write請求後,等待響應的逾時間,預設為60秒。
  12. proxy_http_version 1.0 ; #Nginx伺服器提供代理服務的http協定版本1.0,1.1,預設設定為1.0版本。
  13. #proxy_method get; #支援用戶端的請求方法。post/get;
  14. proxy_ignore_client_abort on; #用戶端斷網時,nginx伺服器是否終端對被代理伺服器的請求。預設為off。
  15. proxy_ignore_headers “Expires” “Set-Cookie”; #Nginx伺服器不處理設定的http相應投中的頭域,這裡空格隔開可以設定多個。
  16. proxy_intercept_errors on; #如果被代理伺服器傳回的狀态碼為400或者大于400,設定的error_page配置起作用。預設為off。
  17. proxy_headers_hash_max_size 1024; #存放http封包頭的哈希表容量上限,預設為512個字元。
  18. proxy_headers_hash_bucket_size 128; #nginx伺服器申請存放http封包頭的哈希表容量大小。預設為64個字元。
  19. proxy_next_upstream timeout; #反向代理upstream中設定的伺服器組,出現故障時,被代理伺服器傳回的狀态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
  20. #proxy_ssl_session_reuse on; 預設為on,如果我們在錯誤日志中發現“SSL3_GET_FINSHED:digest check failed”的情況時,可以将該指令設定為off。

繼續閱讀