[Web Server] Nginx/Apache 環境變數設定 (Environment Variables)

Intro

Nginx/Apache 環境變數設定速查


Nginx設定

fastcgi_param RUNTIME_ENVIROMENT 'DEV'

For example:

server {
    listen   80; 
    root /var/www/html;
    index index.php;
    server_name localhost;
    location /
    {   
         index index.php;
    }   

    location ~ .*\.(php|php5)?$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param RUNTIME_ENVIROMENT 'DEV'
    }   
}

Nginx 加入 Header

Module ngx_http_headers_module - nginx.org

server {

add_header Node 'server-a';
fastcgi_hide_header X-Powered-By;
#...
}

Nginx Logging

Logging 資料格式設定

/etc/nginx/nginx.conf:

http {
    # ...
    log_format log_format_name '"$time_local" client=$remote_addr '
                   'method=$request_method request="$request" '
                   'request_length=$request_length '
                   'status=$status bytes_sent=$bytes_sent '
                   'body_bytes_sent=$body_bytes_sent '
                   'referer=$http_referer '
                   'user_agent="$http_user_agent" '
                   'upstream_addr=$upstream_addr '
                   'upstream_status=$upstream_status '
                   'request_time=$request_time '
                   'upstream_response_time=$upstream_response_time '
                   'upstream_connect_time=$upstream_connect_time '
                   'upstream_header_time=$upstream_header_time';

    # 設定該 log format 給指定 log
    access_log /var/log/nginx/access.log log_format_name;
    # ...
}
  • NGINX Built‑In Timing Variables

    • $request_time – Full request time, starting when NGINX reads the first byte from the client and ending when NGINX sends the last byte of the response body
    • $upstream_connect_time – Time spent establishing a connection with an upstream server
    • $upstream_header_time – Time between establishing a connection to an upstream server and receiving the first byte of the response header
    • $upstream_response_time – Time between establishing a connection to an upstream server and receiving the last byte of the response body

    Using NGINX Logging for Application Performance Monitoring

  • nginx原始碼層面探究 request_time... (Variables definition)

    $request_time定論是在 TCP handshake 後收到第一個可讀 TCP event 且在應用層讀取前開始計算。個人理解為第一個 TCP application data package,所以推測在 HTTPS 中是在 TLS handshake 收到 Client Hello 讀取前開始計算


Apache設定

Environment Variables in Apache

SetEnv RUNTIME_ENVIROMENT DEV

For example:


    DocumentRoot "/var/www/html"
    ServerName localhost
    SetEnv RUNTIME_ENVIROMENT DEV

Leave a Reply

Your email address will not be published. Required fields are marked *