Nginx的配置文件
我的演示環(huán)境是在mac上使用homebrew安裝的nginx叶摄,它的配置文件為/usr/local/etc/nginx/nginx.conf
。
其實(shí)所有的配置都可以寫在這個(gè)文件中碗淌,但是當(dāng)文件過大時(shí)難以維護(hù)盏求,所以通常的做法是按照模塊
或域名
將配置拆分。
在nginx.conf
文件的統(tǒng)計(jì)目錄創(chuàng)建兩個(gè)目錄:servers/
和upstreams/
亿眠。
servers/
中保存nginx的server
的配置碎罚;
upstreams/
中保存nginx的upstream
的配置。
在servers/
下創(chuàng)建m.domain.conf
文件纳像,內(nèi)容如下:
server {
listen 80;
server_name m.domain.com;
location / {
proxy_pass http://mobile.path;
}
location ~* ^/path($|/) {
proxy_pass http://mobile.path;
}
location ~* ^/path01($|/) {
proxy_pass http://mobile.path01;
}
location ~* ^/path02($|/) {
proxy_pass http://mobile.path02;
}
}
這樣配置的目的是希望根據(jù)訪問者url的二級(jí)目錄來執(zhí)行如下的分流規(guī)則:
http://m.domain.com/ --> http://mobile.path
http://m.domain.com/path --> http://mobile.path
http://m.domain.com/path01 --> http://mobile.path01
http://m.domain.com/path02 --> http://mobile.path02
在upstreams/
下創(chuàng)建m.upstream.conf
文件荆烈,內(nèi)容如下:
upstream mobile.path{
server 127.0.0.1:8080;
}
upstream mobile.path01{
server 127.0.0.1:8081;
}
upstream mobile.path02{
server 127.0.0.1:8082;
}
這里配置的是分流的規(guī)則對(duì)應(yīng)的主機(jī)列表。
在nginx.conf
中配置如下的內(nèi)容來指定日志的格式竟趾、日志的目錄憔购,以及包含配置文件:
...
http {
...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"';
access_log /Users/john/logs/nginx/access.log main;
...
include servers/*.conf;
include upstreams/*.conf;
}
...