常見的Nginx配置選項整理

2012-04-10 10:00:04來源:Linux Today作者:天明

Google 上有豐富的 Nginx 的教程和樣本配置文件,但很多時候時候,配置這些是一些技巧,一直對大家很有幫助。

Google 上有豐富的 Nginx 的教程和樣本配置文件,但很多時候時候,配置這些是一些技巧,一直對大家很有幫助。

Include 文件

不要在您的主 nginx.conf 文件中配置所有的東西,你需要分成幾個較小的文件。您的同事會很感激你的。比如我的結(jié)構(gòu),我定義我的 upstream 的 pool 的為一個文件,和一個文件定義 location 處理服務(wù)器上其它的應用。

例子:

upstreams.conf

upstream cluster1 {
fair;
server app01:7060;
server app01:7061;
server app02:7060;
server app02:7061;
}

upstream cluster2 {
fair;
server app01:7071;
server app01:7072;
server app02:7071;
server app02:7072;
}

locations.conf

location / {
root /var/www;
include cache-control.conf;
index index.html index.htm;
}

location /services/service1 {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

add_header Pragma "no-cache";

proxy_pass http://cluster1/;
}

location /services/service2 {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

add_header Pragma "no-cache";

proxy_pass http://cluster2/service2;
}

servers.conf

server {
listen 80;
include locations.conf;
}

現(xiàn)在,你的 nginx.conf 看起來非常的干凈和簡單(仍然可以分開更多,來更包括文件,比如分離gzip的配置選項)

nginx.conf

worker_processes 4;
worker_rlimit_nofile 10240;

events {
worker_connections 10240;
use epoll;
}

http {
include upstreams.conf;

include mime.types;
default_type application/octet-stream;

log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" $request_time';

access_log /usr/local/nginx/logs/access.log custom;

proxy_buffering off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;

gzip on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/xml+rss image/svg+xml application/x-font-ttf application/vnd.ms-fontobject;
gzip_disable "MSIE [1-6]\.";

# proxy cache config
proxy_cache_path /mnt/nginx_cache levels=1:2
keys_zone=one:10m
inactive=7d max_size=10g;
proxy_temp_path /var/tmp/nginx_temp;

proxy_next_upstream error;

include servers.conf;
}

這 nginx.conf 文件是使用了一些不太常見的配置選項,它值得指出其中一些重要的。

多個 worker  的配置(進程)

如果你的 Nginx 是多個 CPU 和多核,需要配置成多核的數(shù)量比較好。

worker_processes 4;

增加打開的文件句柄

如果 Nginx 服務(wù)很大的流量,增加最大可以打開的文件句柄還是很有用的,因為默認只有 1024 個,可以使用 'ulimit -n' 看到當前系統(tǒng)中的設(shè)置。

worker_rlimit_nofile 10240;

定制的日志

可以看看 log_format 和 access_log 二個選項的設(shè)置。通常我們有幾個參數(shù)最常使用,例如"$http_x_forwarded_for" 可以見到 load balancer 的設(shè)備之前的 IP,還有 "$request_time" 可以見到 Nginx 來處理這個主動所花的時間。

壓縮

壓縮對于文本非常非常的有用

gzip on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/xml+rss image/svg+xml application/x-font-ttf application/vnd.ms-fontobject;
gzip_disable "MSIE [1-6]\.";

代理的選項

這些選項可以在每個 location 中設(shè)置

proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Pragma "no-cache";

這個中加了一個定制的參數(shù),就是 'no-cache',這樣就不會使用 cache 的內(nèi)容了。

代理的 Cache

使用 Nginx 可以給一些文件來 cache 到本地來當 Cache 的服務(wù)器,需要設(shè)置  proxy_cache_path 和  proxy_temp_path  在你的 HTTP 的 directive 中。在 location 中配置,如果有你想 cache 的內(nèi)容的話。

proxy_cache_path /mnt/nginx_cache levels=1:2
keys_zone=one:10m
inactive=7d max_size=10g;
proxy_temp_path /var/tmp/nginx_temp;

這可能還想增加一些其它的參數(shù)

proxy_cache one;
proxy_cache_key mylocation.$request_uri;
proxy_cache_valid 200 302 304 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

HTTP caching options

有時你想使用其它的東西來做 Cache,你可能需要指定怎么樣 cache。你可以給 cache 的信息的文件 include 到你的 root 的 location 中

location / {
root /var/www;
include cache-control.conf;

index index.html index.htm;
}

你可以指定不同的頭到于不同的文件

# default cache 1 day
expires +1d;

if ($request_uri ~* "^/services/.*$") {
expires +0d;
add_header Pragma "no-cache";
}

if ($request_uri ~* "^/(index.html)?$") {
expires +1h;
}

SSL

如果你要配置 ssl 的連接的話

server {
server_name www.example.com;
listen 443;
ssl on;
ssl_certificate /usr/local/nginx/ssl/cert.pem;
ssl_certificate_key /usr/local/nginx/ssl/cert.key;

include locations.conf;
}
關(guān)鍵詞:Nginx配置

贊助商鏈接: