Nginx 配置文件主要分成四部分:main(全局設(shè)置)、http(HTTP 的通用設(shè)置)、server(虛擬主機(jī)設(shè)置)、location(匹配 URL 路徑)。還有一些其他的配置段,如 event,upstream 等。
通用設(shè)置
user nginx
指定運(yùn)行 nginx workre 進(jìn)程的用戶和組
worker_rlimit_nofile #
指定所有 worker 進(jìn)程能夠打開的最大文件數(shù)
worker_cpu_affinity
設(shè)置 worker 進(jìn)程的 CPU 粘性,以避免進(jìn)程在 CPU 間切換帶來的性能消耗。如 worker_cpu_affinity 0001 0010 0100 1000;(四核)
worker_processes 4
worker 工作進(jìn)程的個(gè)數(shù),這個(gè)值可以設(shè)置為與 CPU 數(shù)量相同,如果開啟了 SSL 和 Gzip,那么可以適當(dāng)增加此數(shù)值
worker_connections 1000
單個(gè) worker 進(jìn)程能接受的最大并發(fā)連接數(shù),放在 event 段中
error_log logs/error.log info
錯(cuò)誤日志的存放路徑和記錄級別
use epoll
使用 epoll 事件模型,放在 event 段中
http 服務(wù)器
server {}:
定義一個(gè)虛擬主機(jī)
listen 80;
定義監(jiān)聽的地址和端口,默認(rèn)監(jiān)聽在本機(jī)所有地址上
server_name NAME [...];
定義虛擬主機(jī)名,可以使用多個(gè)名稱,還可以使用正則表達(dá)式或通配符。
sendfile on
開啟 sendfile 調(diào)用來快速的響應(yīng)客戶端
keepalive_timeout 65
長連接超時(shí)時(shí)間,單位是秒。
send_timeout
指定響應(yīng)客戶端的超時(shí)時(shí)間
client_max_body_size 10m
允許客戶端請求的實(shí)體最大大小
root PATH
設(shè)置請求 URL 所對應(yīng)資源所在文件系統(tǒng)上的根目錄
location [ = | ~ | ~* | ^~ ] URI { ... }
設(shè)置一個(gè) URI 匹配路徑
- =:精確匹配
- ~:正則表達(dá)式匹配,區(qū)分字符大小寫
- ~*:正則表達(dá)式匹配,不區(qū)分字符大小寫
- ^~:URI 的前半部分匹配,且不實(shí)用正則表達(dá)式
- 優(yōu)先級:
- = > location 完整路徑 > ^~ > ~ > ~* > location 起始路徑 > location /
allow 和 deny
基于 IP 訪問控制,如:
僅允許 192.168.0.0/24 網(wǎng)段客戶端訪問
1
2
|
allow 192.168.0.0/24; deny all; |
stub_status on
開啟狀態(tài)顯式,僅能用于 location 中:
開啟狀態(tài)顯式頁面
1
2
3
4
5
|
location /status { stub_status on; allow 172.16.0.0/16; deny all; } |
rewrite <REGEX> <REPL> <FLAG>
URL 重寫,可以使用多種標(biāo)記
例如:
1
|
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; |
可用的 flag:
- - last:重寫完成后,繼續(xù)匹配其他 rewrite 規(guī)則
- - break:重寫完成后不再繼續(xù)匹配
- - redirect:返回 302 重定向(臨時(shí)重定向),客戶端對重定向的 URL 發(fā)起新的請求
- - permanent:返回 301 重定向(永久重定向),客戶端對重定向的 URL 發(fā)起新的請求
詳盡配置說明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#定義Nginx運(yùn)行的用戶和用戶組 user www www; #nginx進(jìn)程數(shù),建議設(shè)置為等于CPU總核心數(shù)。 worker_processes 8; #全局錯(cuò)誤日志定義類型,[ debug | info | notice | warn | error | crit ] error_log /var/log/nginx/error.log info; #進(jìn)程文件 pid /var/run/nginx.pid; #一個(gè)nginx進(jìn)程打開的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開文件數(shù)(系統(tǒng)的值ulimit -n)與nginx進(jìn)程數(shù)相除,但是nginx分配請求并不均勻,所以建議與ulimit -n的值保持一致。 worker_rlimit_nofile 65535; #工作模式與連接數(shù)上限 events { #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型,如果跑在FreeBSD上面,就用kqueue模型。 use epoll; #單個(gè)進(jìn)程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進(jìn)程數(shù)) worker_connections 65535; } #設(shè)定http服務(wù)器 http { include mime.types; #文件擴(kuò)展名與文件類型映射表 default_type application/octet-stream; #默認(rèn)文件類型 #charset utf-8; #默認(rèn)編碼 server_names_hash_bucket_size 128; #服務(wù)器名字的hash表大小 client_header_buffer_size 32k; #上傳文件大小限制 large_client_header_buffers 4 64k; #設(shè)定請求緩 client_max_body_size 8m; #設(shè)定請求緩 sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對于普通應(yīng)用設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的負(fù)載。注意:如果圖片顯示不正常把這個(gè)改成off。 autoindex on; #開啟目錄列表訪問,合適下載服務(wù)器,默認(rèn)關(guān)閉。 tcp_nopush on; #防止網(wǎng)絡(luò)阻塞 tcp_nodelay on; #防止網(wǎng)絡(luò)阻塞 keepalive_timeout 120; #長連接超時(shí)時(shí)間,單位是秒 #FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問速度。下面參數(shù)看字面意思都能理解。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip模塊設(shè)置 gzip on; #開啟gzip壓縮輸出 gzip_min_length 1k; #最小壓縮文件大小 gzip_buffers 4 16k; #壓縮緩沖區(qū) gzip_http_version 1.0; #壓縮版本(默認(rèn)1.1,前端如果是squid2.5請使用1.0) gzip_comp_level 2; #壓縮等級 gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型,默認(rèn)就已經(jīng)包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個(gè)warn。 gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接數(shù)的時(shí)候需要使用 upstream blog.ha97.com { #upstream的負(fù)載均衡,weight是權(quán)重,可以根據(jù)機(jī)器配置定義權(quán)重。weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大。 server 192.168.80.121:80 weight=3; server 192.168.80.122:80 weight=2; server 192.168.80.123:80 weight=3; } #虛擬主機(jī)的配置 server { #監(jiān)聽端口 listen 80; #域名可以有多個(gè),用空格隔開 server_name www.ha97.com ha97.com; index index.html index.htm index.php; root /data/www/ha97; location ~ .*.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } #圖片緩存時(shí)間設(shè)置 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d; } #JS和CSS緩存時(shí)間設(shè)置 location ~ .*.(js|css)?$ { expires 1h; } #日志格式設(shè)定 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; #定義本虛擬主機(jī)的訪問日志 access_log /var/log/nginx/ha97access.log access; #對 "/" 啟用反向代理 location / { proxy_pass http://127.0.0.1:88; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; #后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #以下是一些反向代理的配置,可選。 proxy_set_header Host $host; client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù) client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù), proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí)) proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí)) proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí)) proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的設(shè)置 proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳 } #設(shè)定查看Nginx狀態(tài)的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; #htpasswd文件的內(nèi)容可以用apache提供的htpasswd工具來產(chǎn)生。 } #本地動(dòng)靜分離反向代理配置 #所有jsp的頁面均交由tomcat或resin處理 location ~ .(jsp|jspx|do)?$ { 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_pass http://127.0.0.1:8080; } #所有靜態(tài)文件由nginx直接讀取不經(jīng)過tomcat或resin location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*.(js|css)?$ { expires 1h; } } } |