Nginx基礎篇(2)- Nginx基本配置文件和變量詳解

Nginx基本配置文件和變量詳解

1. 基本配置文件

/etc/nginx/nginx.conf

# nginx運行的用戶
user  nginx;
# nginx進程數,建議設置爲等於CPU總核心數。
worker_processes  1;

# 全局錯誤日誌文件名稱和所在目錄,錯誤日誌記錄級別[ debug | info | notice | warn | error | crit ]
error_log  /var/log/nginx/error.log warn;
# 進程文件nginx.pid所在目錄
pid        /var/run/nginx.pid;

# 一個nginx進程打開的最多文件描述符數目,
# 理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,
# 可是nginx分配請求並不均勻,因此建議與ulimit -n的值保持一致。
# worker_rlimit_nofile 65535;

events {
    # 參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 
    # epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,若是跑在FreeBSD上面,就用kqueue模型。
    # use epoll;

    # 單個進程最大鏈接數(最大鏈接數 = 鏈接數 * 進程數)
    worker_connections  1024;
    
    # multi_accept on;
}

# http服務器配置
http {
    # 引入http協議的Content-Type與擴展名對應關係的文件
    include       /etc/nginx/mime.types;
    # 默認文件類型
    default_type  application/octet-stream;
    
    # 指定日誌格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    # HTTP服務器請求日誌文件的名稱和所在目錄、日誌記錄級別
    access_log  /var/log/nginx/access.log  main;

    # 開啓高效文件傳輸模式。
    # sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲on,
    # 若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。
    # 注意:若是圖片顯示不正常把這個改爲off。
    sendfile        on;
    # 防止網絡阻塞
    # tcp_nopush    on;
    # tcp_nodelay   on;

    # 開啓目錄列表訪問,合適下載服務器,默認關閉。
    # autoindex on; 
    
    # 開啓限制IP鏈接數的時候須要使用
    # limit_zone crawler $binary_remote_addr 10m; 
    
    # 長鏈接超時時間,單位是秒
    keepalive_timeout  65;

    # gzip模塊設置
    # 開啓gzip壓縮輸出
    # gzip  on;
    # 最小壓縮文件大小
    # gzip_min_length 1k; 
    # 壓縮緩衝區
    # gzip_buffers 4 16k; 
    # 壓縮版本(默認1.1,前端若是是squid2.5請使用1.0)
    # gzip_http_version 1.0; 
    # 壓縮等級
    # gzip_comp_level 2; 
    # 壓縮類型,默認就已經包含text/html,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。
    # gzip_types text/plain application/x-javascript text/css application/xml;
    # gzip_vary on;
        
     
    # FastCGI相關參數是爲了改善網站的性能:減小資源佔用,提升訪問速度。
    # fastcgi_connect_timeout 300;
    # fastcgi_send_timeout 300;
    # fastcgi_read_timeout 300;
    # fastcgi_buffer_size 128k;
    # fastcgi_buffers 8 128k;
    # fastcgi_busy_buffers_size 128k;
    # fastcgi_temp_file_write_size 128k;

    # 容許客戶端請求的最大單文件字節數。 
    # 若是請求大於指定的大小,則NGINX發回HTTP 413(Request Entity too large)錯誤。 
    # 若是服務器處理大文件上傳,則該指令很是重要。
    # client_max_body_size 20m;
    # 用於請求主體的緩衝區大小。 
    # 若是主體超過緩衝區大小,則完整主體或其一部分將寫入臨時文件。 
    # 若是NGINX配置爲使用文件而不是內存緩衝區,則該指令會被忽略。 
    # 默認狀況下,該指令爲32位系統設置一個8k緩衝區,爲64位系統設置一個16k緩衝區。 
    # 該指令在NGINX配置的http,server和location區塊使用。
    # client_body_buffer_size 128k;
    
    # upstream abc.com {
        # upstream的負載均衡,weight是權重,能夠根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的概率越大。
    #     server 192.168.10.121:80 weight=3;
    #     server 192.168.10.122:80 weight=2;
    #     server 192.168.10.123:80 weight=3;
    # }
    
    # 引入 '/etc/nginx/conf.d/' 目錄下以 '.conf' 結尾的文件
    include /etc/nginx/conf.d/*.conf;
    
    # 虛擬主機的配置
    server {
        # 監聽端口
        listen       80;
        # 域名,能夠有多個,用空格隔開
        server_name  localhost;
        
        # 默認編碼
        # charset utf-8; 
        
        # 日誌格式設定
        # log_format access '$remote_addr - $remote_user [$time_local] "$request" '
        # '$status $body_bytes_sent "$http_referer" '
        # '"$http_user_agent" $http_x_forwarded_for';
        # 當前虛擬主機請求日誌文件的名稱和所在目錄、日誌記錄級別
        # access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # 將服務器錯誤頁面重定向到靜態頁面 /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        # 將 PHP 腳本反向代理到監聽 127.0.0.1:80 端口的Apache
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        # 將 PHP 腳本反向代理到監聽127.0.0.1:9000端口的FastCGI服務器
        #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;
        #}
    
        # 若是Apache的文檔根目錄與nginx的相同,拒絕訪問.htaccess文件。
        #location ~ /\.ht {
        #    deny  all;
        #}
        
        # 圖片緩存時間設置
        # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        #     expires 10d;
        # }
        
        # JS和CSS緩存時間設置
        # location ~ .*\.(js|css)?$ {
        #     expires 1h;
        # }
        
        # 對 "/" 啓用反向代理
        # location / {
        #     proxy_pass http://127.0.0.1:88;
        #     proxy_redirect off;
        #     # 後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
        #     proxy_set_header X-Real-IP $remote_addr;
        #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #     # 如下是一些反向代理的配置,可選。
        #     proxy_set_header Host $host;
        #     # nginx跟後端服務器鏈接超時時間(代理鏈接超時)
        #     proxy_connect_timeout 90; 
        #     # 後端服務器數據回傳時間(代理髮送超時)
        #     proxy_send_timeout 90; 
        #     # 鏈接成功後,後端服務器響應時間(代理接收超時)
        #     proxy_read_timeout 90; 
        #     # 設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
        #     proxy_buffer_size 4k; 
        #     # proxy_buffers緩衝區,網頁平均在32k如下的設置
        #     proxy_buffers 4 32k; 
        #     # 高負荷下緩衝大小(proxy_buffers*2)
        #     proxy_busy_buffers_size 64k; 
        #     # 設定緩存文件夾大小,大於這個值,將從upstream服務器傳
        #     proxy_temp_file_write_size 64k;
        # }
    }
}

2. Nginx變量

HTTP請求變量

  • arg_參數名:例如,$arg_userid,能夠引用到請求參數userid的值
  • http_請求HEADER名:例如,$http_user_agent,能夠引用到請求頭信息User-Agent的值
  • sent_http_返回HEADER名:在響應客戶端可添加header頭信息

內置變量

Nginx內置的變量,參考Nginx文檔的 Logging to syslog

自定義變量

服務器管理員本身定義的變量javascript

原文地址:http://www.javashuo.com/article/p-mmbdbhro-bz.htmlphp

相關文章
相關標籤/搜索