Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

nginx.conf 逐段詳解

以下逐段拆解一份典型的 nginx 主設定檔,包含全域、events 與 http 三個層級,以及虛擬主機與反向代理的設定。

全域設定

# 啟動 nginx 的使用者與群組
user www www;

# 工作進程數量,通常等於 CPU 數或 2 倍 CPU 數
worker_processes 2;

# 錯誤日誌位置,等級可為 debug|info|notice|warn|error|crit
error_log /var/htdocs/logs/nginx_error.log crit;

# PID 檔位置
pid /usr/local/nginx/nginx.pid;

# 單一進程可開啟的最大檔案描述符數
worker_rlimit_nofile 51200;

worker_rlimit_nofile 常被忽略,但它必須大於等於 worker_connections,否則高併發時會撞到 ulimit 上限。

events 區塊

events
{
    # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]
    use epoll;              # Linux 2.6 高效能模型;FreeBSD 建議 kqueue;Windows 不指定
    worker_connections 51200;   # 每個進程的最大連線數
}

最大併發連線數 = worker_connections × worker_processes。

http 區塊

http
{
    include mime.types;                       # 副檔名與 MIME 類型對照表
    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"';

    log_format download '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $bytes_sent '
                        '"$http_referer" "$http_user_agent" '
                        '"$http_range" "$sent_http_content_range"';

    charset gb2312,utf-8;
    server_names_hash_bucket_size 128;

    sendfile on;              # 開啟高效能檔案傳輸
    tcp_nopush on;            # 以下兩項搭配使用,避免網路阻塞
    tcp_nodelay on;

    keepalive_timeout 300;    # 長連線逾時

    # FastCGI 相關逾時與緩衝
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_temp_path /dev/shm;      # 放進記憶體檔案系統,減少磁碟 I/O

    # gzip 壓縮
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_http_version 1.1;           # 前端為 squid 2.5 時改用 1.0
    gzip_types text/plain application/x-javascript text/css text/html
               text/javascript application/xml;

    error_page 404 https://www.g.cn;
    error_page 403 https://www.g.cn;

    client_max_body_size 20m;        # 上傳大小限制
    client_header_buffer_size 16k;
    large_client_header_buffers 4 64k;

upstream:後端伺服器池

upstream mysvr {
    # weigth 為權值,越高被分到的機率越大
    server 192.168.8.1:3128 weight=5;    # 本機 Squid 開在 3128
    server 192.168.8.2:80   weight=1;
    server 192.168.8.3:80   weight=6;
}

實務上,在同一台機器上開四組獨立的 php-cgi(各 8 個子進程),效能通常不如一組 32 子進程——因為單一進程組能共享 eAccelerator 的 PHP 二進位檔快取,命中率更高。多組的好處是某組 php 假死時其他埠還能接手,實測 502 的機率明顯下降。

虛擬主機

server
{
    listen 80;
    server_name www.52crack.com;
    index index.html  index.htm index.php;
    root /var/htdocs/52crack;

    if (-d $request_filename)
    {
        rewrite ^/(.*)([^/])$ https://$host/$1$2/ permanent;
    }

    access_log logs/www.52crack.com.access.log main;

    # PHP 請求交給 FastCGI
    location ~ .*\.php?$
    {
        include fcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }

    # 靜態資源直接讀本地,並設定 24 小時客戶端快取
    # 若這類檔案數量龐大,反而不建議這樣做,交給 squid 快取效果更好
    location ~ ^/(img|js|css)/ {
        root /var/htdocs/52crack;
        expires 24h;
    }

    # 其餘請求走反向代理
    location / {
        proxy_pass https://127.0.0.1;
        proxy_redirect off;
        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 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;

        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 256k;
    }
}

三個 proxy_set_header 是反向代理的必寫項:不傳 Host,後端會拿到 nginx 自己的位址;不傳 X-Real-IP 與 X-Forwarded-For,後端記錄到的客戶端 IP 全都會是 nginx,access log 也就失去意義。

修改設定後記得先 nginx -t 檢查語法,再用 nginx -s reload 平滑重載,避免直接重啟造成連線中斷。

Copyright © Linux教程網 All Rights Reserved