nginx.conf各參數的意義

搬運+翻譯至 http://qiita.com/syou007/items/3e2d410bbe65a364b603

/etc/nginx/nginx.conf

記錄各個參數的意義

 

user

user nginx;

nginx開啓後會啓動3個進程master processworker processcache manager process
本參數指定了master process以外的進程的用戶。
master process是用root啓動的

 

worker_processes

worker_processes auto

worker_processes 2

指定Nginx運行時使用的CPU核數。
設成auto會自動判斷CPU的核數。

用以下命令可以看CPU核數。

$ grep processor /proc/cpuinfo |wc

以下參數指定了哪個cpu分配給哪個進程,一般來說不用特殊指定。如果一定要設的話,用0和1指定分配方式,比如:

worker_processes 4     #4核CPU

worker_cpu_affinity 0001 0010 0100 1000    #這樣設就是給1-4個進程分配單獨的核來運行,出現第5個進程是就是隨機分配了

 

worker_rlimit_nofile

worker_rlimit_nofile 4096;

設置毎個進程的最大文件打開數。如果不設的話上限就是系統的ulimit –n的數字。
一般來說設成下面提到的worker_connections的3-4倍就夠用了。

 

error_log

error_log /var/log/nginx/error.log;

nginx的日誌,沒特殊要求的話默認值就可以了。

 

pid

pid /var/run/nginx.pid;

指定pid文件的位置,默認值就可以。

Events模塊

events {

...

        }

用來定義Event模塊。
以下3個項目需要記載在event模塊中

 

worker_connections

worker_connections 1024;

一個worker進程的最大連接數。默認爲512,按自己系統的硬件配置調整,不能超過worker_rlimit_nofile。

 

multi_accept

multi_accept on;

默認是on。設置爲on後,多個worker按串行方式來處理連接,也就是一個連接只有一個worker被喚醒,其他的處於休眠狀態。

設置爲off後,多個worker按並行方式來處理連接,也就是一個連接會喚醒所有的worker,知道連接分配完畢,沒有取得連接的繼續休眠。

當你的服務器連接數不多時,開啓這個參數會讓負載有一定程度的降低。但是當服務器的吞吐量很大時,爲了效率,請關閉這個參數。

 

use

use epoll

Linux內核2.6以上爲epoll,BSD爲kqueue。

http模塊

http {

...

     }

用作Web服務器的配置。

 

server_tokens

server_tokens off;

錯誤頁面的標籤上是否表示 Nginx的版本。
安全上的考慮還是off掉吧。

 

include

include /etc/nginx/mime.types;

定義MIME類型和後綴名關聯的文件的位置。

types {

text/html html htm shtml;

text/css css;

text/xml xml;

image/gif gif;

image/jpeg jpeg jpg;

application/javascript js;

...

      }

mime.types文件中大概是這個樣子的。

 

default_type

default_type application/octet-stream;

指定mime.types文件中沒有記述到的後綴名的處理方法。
默認值是text/plain。

 

log_format

log_format main 'time:$time_iso8601\t'...

log_format ltsv 'time:$time_iso8601\t'...

定義日誌的格式。可以選擇main或者ltsv,後面定義要輸出的內容。
 1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
 2.$remote_user :用來記錄客戶端用戶名稱;
 3.$time_local :用來記錄訪問時間與時區;
 4.$request :用來記錄請求的url與http協議;
 5.$status :用來記錄請求狀態;
 6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
 7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
 8.$http_user_agent :記錄客戶端瀏覽器的相關信息;

 

access_log

access_log /var/log/nginx/access.log main;

連接日誌的路徑,上面指定的日誌格式放在最後。

access_log off;

也可以關掉。

 

charset

charset UTF-8;

設置應答的文字格式。

 

sendfile

sendfile on;

指定是否使用OS的sendfile函數來傳輸文件
普通應用應該設爲on,下載等IO重負荷的應用應該設爲off。默認值是off

 

tcp_nopush

tcp_nopush on;

sendfileon時這裏也應該設爲on,數據包會累積一下再一起傳輸,可以提高一些傳輸效率。

 

 

tcp_nodelay

tcp_nodelay on;

小的數據包不等待直接傳輸。默認爲on
看上去是和tcp_nopush相反的功能,但是兩邊都爲on時nginx也可以平衡這兩個功能的使用。

 

keepalive_timeout

keepalive_timeout 75;

HTTP連接的持續時間。設的太長會使無用的線程變的太多。
設成0關閉此功能。
默認爲75

 

keepalive_requests

keepalive_requests 100;

keepalive_timeout時效內同樣的客戶端超過指定數量的連接時會被強制切斷。
一般的話keepalive_timeout 5keepalive_requests 20差不多就夠了。
默認爲100

 

set_real_ip_fromreal_ip_header

set_real_ip_from 10.0.0.0/8;

real_ip_header X-Forwarded-For;

可以防止經過代理或者負載均衡服務器時丟失源IP
set_real_ip_from指定代理或者負載均衡服務器的IP,可以指定複數個IP
real_ip_header
指定從哪個header頭檢索出要的IP地址

 

 

client_header_timeoutclient_body_timeout

client_header_timeout 10;

client_body_timeout 10;

讀取客戶端的請求head部分和客戶端的請求body部分的超時時間。

 

client_body_buffer_sizeclient_body_temp_path

client_body_buffer_size 32k;

client_body_temp_path /dev/shm/client_body_temp 1 2;

接受的請求body部分到client_body_buffer_size爲止放在內存中,超出的部分輸出至client_body_temp_path文件裏

 

client_max_body_size

client_max_body_size 1m;

客戶端上傳的body的最大值。超過最大值就會發生413(Request Entity Too Large)錯誤
默認爲1m,最好改大一點。

 

client_header_buffer_sizelarge_client_header_buffers

client_header_buffer_size 1k;

large_client_header_buffers 4 8k;

一般來說默認就夠了。
發生414 (Request-URI Too Large) 錯誤時請增大這兩個參數

 

limit_connlimit_conn_zone

limit_conn_zone $binary_remote_addr zone=addr:10m;

limit_conn addr 100;

限制某條件下的同時連接數。

 

Proxy相關

proxy_buffering on;

proxy_buffer_size 8k;

proxy_buffers 100 8k;

proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=CACHE:512m inactive=1d max_size=60g;

作爲反向代理時需要用到的參數。

gzip相關

gzip on;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_proxied expired no-cache no-store private auth;

gzip_vary off;

gzip_types text/plain

text/css

text/xml

...

application/json;

gzip_min_length 1000;

gzip_disable "MSIE [1-6]\.";

應答時使用gzip時設爲on。可以減少服務器間的數據傳輸量。
gzip_types只對指定的文件類型起效。
gzip_proxied只對指定的請求類型起效。
gzip_min_lengthlength比此值小的不壓縮。
gzip_disable:其他不壓縮的情況,一般設爲IE6以下。

 

open_file_cache相關

open_file_cache max=100 inactive=10s;

open_file_cache_valid 30s;

open_file_cache_min_uses 2;

open_file_cache_errors on;

文件描述信息,大小,更新時間等信息可以保存在cache中。

 

server_names_hash_bucket_size

server_names_hash_bucket_size 64

nginx啓動時出現could not build the server_names_hash, you should increase錯誤時請提高這個參數的值
一般設成64就夠了。

 

types_hash_max_size

types_hash_max_size 1024;

types_hash_max_size影響散列表的衝突率。types_hash_max_size越大,就會消耗更多的內存,但散列key的衝突率會降低,檢索速度就更快。types_hash_max_size越小,消耗的內存就越小,但散列key的衝突率可能上升。
默認爲1024

 

types_hash_bucket_size

types_hash_bucket_size 64;

types_hash_bucket_size 設置了每個散列桶佔用的內存大小。
默認爲64

 

listen

listen 80 default_server;

nginx當網頁服務器使用的時候寫在http模塊中,一般來說用作虛擬主機的情況下不會寫在這裏。

server_name_in_redirect

server_name_in_redirect off;

重定向的時候需不需要把服務器名寫入head,基本上不會設成on。

 

port_in_redirect

port_in_redirect on;

設爲on後,重定向的時候URL末尾會帶上端口號

 

upstream

upstream resinserver{

ip_hash;

server 127.0.0.1:8000 down;

server 127.0.0.1:8080 weight=2;

server 127.0.0.1:6801;

server 127.0.0.1:6802 backup;

                    }

Ip_hash:每個請求按訪問iphash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題
down
:不參與負責均衡
weight
:比重,越大的分配的越多
backup
:其他的非backup機器都down或者忙的時候纔會請求到這臺機器

 

 

proxy_set_header

proxy_pass http://127.0.0.1:3000;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-Server $host;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_hide_header X-Powered-By;

proxy_ignore_headers Expires;

重定義發送至後端服務器的head。
一般這樣設置就沒問題了。

 

proxy_redirect

proxy_redirect off;

重定向到後端服務器時的location head。on 時按照proxy_pass重定向。Off時按照服務器的指示重定向。

 

error_pageproxy_intercept_errors

proxy_intercept_errors on;

error_page 404 /404.html;

error_page 403 =404 /notfound.html; # 403の場合404に変換される。

error_page 500 502 503 504 /50x.html;

錯誤頁面的顯示。

 

include

include /etc/nginx/vhost.d/*.conf;

放在這個文件夾的設定文件也可以被讀取。

 

Server模塊

http {

server {

...

       }

     }

Nginx用作虛擬主機時使用。
每一個server模塊生成一個虛擬主機。
寫在http模塊內部

 

Listenserver_name

listen 80;

server_name localhost;

連接虛擬主機的信息。
listen指定端口號。
server_name指定服務器的域名。

 

root

root /path/public

定義服務器的默認網站根目錄位置。

 

rewrite

rewrite /(.*)/index.html $1.html permanent;

需要重定向的時候使用。

 

satisfy,auth_basic

satisfy any;

auth_basic "basic authentication";

auth_basic_user_file /etc/nginx/.htpasswd;

satisfy any|all 部分地址Basic認證的方式

 

allow

Deny

satisfy any

不認證

Basic認證

satisfy all

Basic認證

拒絕連接

auth_basic認證的名稱
auth_basic_user_file:密碼文件

 

 

try_files

try_files $uri $uri.html $uri/index.html @unicorn;

從左邊開始找指定文件是否存在。
比如連接http://***/hoge時按hoge.htmlhoge/index.htmllocation @unicorn {}的順序查找

 

Location模塊

http {

server {

location / {

...

           }

       }

     }

指定位置(文件或路徑)時使用。
也可以用正則表達式。

location ~ /\.(ht|svn|git) {

deny all;

                           }

不想讓用戶連接.htaccess.svn.git文件時用上面的設置。

 

stub_statusallowdeny

stub_status on;

access_log off;

allow 127.0.0.1;

deny all;

stub_status連接指定的位置時可以顯示現在的連接數。一般來說不會公開。
Allow允許指定IP的連接。
Deny拒絕指定IP的連接。

Nginx規則是從上到下判斷的,上面的例子先判斷的是allow 127.0.0.1,所以127.0.0.1允許連接
如果反過來設成下面這樣,所有的IP都被拒絕了。(注意和Apache不一樣。)

deny all;

allow 127.0.0.1;

 

expires

expires 10d;

使用瀏覽器緩存時設置。上面的例子用了10天內的瀏覽器緩存。

 

add_header

add_header Cache-Control public;

設置插入response header的值。
不是很懂…

 

breaklast

break|last;

rewrite後接break指令,完成rewrite之後會執行完當前的location(或者是if)指令裏的其他內容(停止執行當前這一輪的ngx_http_rewrite_module指令集),然後不進行新URL的重新匹配。
rewrite後接last指令,在完成rewrite之後停止執行當前這一輪的ngx_http_rewrite_module指令集已經後續的指令,進而爲新的URL尋找location匹配。

 

internal

error_page 404 /404.html;

location /404.html {

internal;

                   }

只在內部重定向的時候使用。
上面的例子就無法直接訪問/404.html頁面

 

附:配置文件例

複製代碼
user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
    listen       80;
    server_name  www.hoge.co.jp;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    proxy_set_header Host            $host;
    proxy_set_header X-Real-IP       $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;

    location / {
        proxy_pass http://backend;
    }

}

upstream backend {
      ip_hash;
      server 172.20.1.201:80 max_fails=3 fail_timeout=30s ;
      server 172.20.1.202:80 max_fails=3 fail_timeout=30s ;
      server 127.0.0.1:8080  down;
}

}
複製代碼
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章