---
title: "nginx的几点优化"
author: "jayjayjays"
date: "2010-04-26T13:22:49Z"
tags: []
categories: ["研发管理"]
slug: "nginx"
draft: false
lastmod: "2010-04-26T13:22:49Z"
id: 653201
url: "https://jayjayjays.iteye.com/blog/653201"
---

在calomel.org这个网站上看到关于nginx的几个优化，觉得比较有用，并且能够方便大家配置nginx，所以翻译过来。

**1. nginx关于服务静态文件的配置**

我们的目标是配置一个服务最快且cpu/io利用最有效的服务器，更重要的是一个安全的web服务器，下面的配置文件适用于最新版nginx。

 写道

#######################################################  
\### Calomel.org /etc/nginx.conf BEGIN  
#######################################################  
\#  
pid /var/run/nginx.pid;  
user nginx nginx;  
worker\_processes 2;

events {  
worker\_connections 1024;  
}

http {  
\## MIME types  
include mime.types;  
\# types {  
\# image/gif gif;  
\# image/jpeg jpg;  
\# image/png png;  
\# image/bmp bmp;  
\# image/x-icon ico;  
\# text/css css;  
\# text/html html;  
\# text/plain bob;  
\# text/plain txt;  
}  
default\_type application/octet-stream;

\## Size Limits  
client\_body\_buffer\_size 8k;  
client\_header\_buffer\_size 1k;  
client\_max\_body\_size 1k;  
large\_client\_header\_buffers 1 1k;

\## Timeouts  
client\_body\_timeout 5;  
client\_header\_timeout 5;  
keepalive\_timeout 5 5;  
send\_timeout 5;

\## General Options  
ignore\_invalid\_headers on;  
limit\_zone gulag $binary\_remote\_addr 1m;  
recursive\_error\_pages on;  
sendfile on;  
server\_name\_in\_redirect off;  
server\_tokens off;

\## TCP options  
tcp\_nodelay on;  
tcp\_nopush on;

\## Compression  
gzip on;  
gzip\_static on;  
gzip\_buffers 16 8k;  
gzip\_comp\_level 9;  
gzip\_http\_version 1.0;  
gzip\_min\_length 0;  
gzip\_types text/plain text/html text/css image/x-icon image/bmp;  
gzip\_vary on;

\## Log Format  
log\_format main '$remote\_addr $host $remote\_user \[$time\_local] "$request" '  
'$status $body\_bytes\_sent "$http\_referer" "$http\_user\_agent" "$gzip\_ratio"';

\## Deny access to any host other than (www.)mydomain.com  
server {  
server\_name \_; #default  
return 444;  
}

\## Server (www.)mydomain.com  
server {  
access\_log /var/log/nginx/access.log main buffer=32k;  
error\_log /var/log/nginx/error.log info;  
expires 31d;  
limit\_conn gulag 5;  
listen 127.0.0.1:8080 rcvbuf=64k backlog=128;  
root /disk01/htdocs;  
server\_name mydomain.com www.mydomain;

\## SSL Options (only enable if you use a SSL certificate)  
\# ssl on;  
\# ssl\_certificate /ssl\_keys/mydomain.com\_ssl.crt;  
\# ssl\_certificate\_key /ssl\_keys/mydomain\_ssl.key;  
\# ssl\_ciphers HIGH:!ADH:!MD5;  
\# ssl\_prefer\_server\_ciphers on;  
\# ssl\_protocols SSLv3;  
\# ssl\_session\_cache shared:SSL:1m;  
\# ssl\_session\_timeout 5m;

\## Only allow GET and HEAD request methods  
if ($request\_method !~ ^(GET|HEAD)$ ) {  
return 444;  
}

\## Deny illegal Host headers  
if ($host !~* ^(mydomain.com|www.mydomain.com)$ ) {  
return 444;  
}

\## Deny certain User-Agents (case insensitive)  
\## The ~* makes it case insensitive as opposed to just a ~  
if ($http\_user\_agent ~* (Baiduspider|Jullo) ) {  
return 444;  
}

\## Deny certain Referers (case insensitive)  
\## The ~* makes it case insensitive as opposed to just a ~  
if ($http\_referer ~* (babes|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|sex|teen|video|webcam|zippo) ) {  
return 444;  
}

\## Redirect from www to non-www  
if ($host = 'www.mydomain.com' ) {  
rewrite ^/(.\*)$ http://mydomain.com/$1 permanent;  
}

\## Stop Image and Document Hijacking  
location ~* (\\.jpg|\\.png|\\.css)$ {  
if ($http\_referer !~ ^(http://mydomain.com) ) {  
return 444;  
}  
}

\## Restricted Access directory  
location ^~ /secure/ {  
allow 127.0.0.1/32;  
allow 10.10.10.0/24;  
deny all;  
auth\_basic "RESTRICTED ACCESS";  
auth\_basic\_user\_file /var/www/htdocs/secure/access\_list;  
}

\## Only allow these file types to document root  
location / {  
if ($request\_uri ~* (^\\/|\\.html|\\.jpg|\\.org|\\.png|\\.css|favicon\\.ico|robots\\.txt)$ ) {  
break;  
}  
return 444;  
}

\## Serve an empty 1x1 gif \_OR_ an error 204 (No Content) for favicon.ico  
location = /favicon.ico {  
#empty\_gif;  
return 204;  
}

\## System Maintenance (Service Unavailable)  
if (-f $document\_root/system\_maintenance.html ) {  
error\_page 503 /system\_maintenance.html;  
return 503;  
}

\## All other errors get the generic error page  
error\_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417  
500 501 502 503 504 505 /error\_page.html;  
location /error\_page.html {  
internal;  
}  
}  
}  
\#  
#######################################################  
\### Calomel.org /etc/nginx.conf END  
#######################################################

 

**2. nginx关于对后端服务器的反向代理配置**

有三个后端服务，一个为web内容服务，一个是论坛服务，一个为文件服务。

 

当一个请求来时，nginx代理服务器其查看url把请求定向到相应的服务器，这个配置也缓冲文件服务的内容，但是论坛的和数据下载的内容就不缓存了，这个配置也使用了压缩，更好的节省内存

 写道

#######################################################  
\### Calomel.org /etc/nginx.conf BEGIN  
#######################################################  
pid /var/run/nginx.pid;  
user nginx nginx;  
worker\_processes 10;

events {  
worker\_connections 1024;  
}

http {  
\## MIME types  
#include /etc/nginx\_mime.types;  
default\_type application/octet-stream;

\## Size Limits  
client\_body\_buffer\_size 128K;  
client\_header\_buffer\_size 128K;  
client\_max\_body\_size 1M;  
large\_client\_header\_buffers 1 1k;

\## Timeouts  
client\_body\_timeout 60;  
client\_header\_timeout 60;  
expires 24h;  
keepalive\_timeout 60 60;  
send\_timeout 60;

\## General Options  
ignore\_invalid\_headers on;  
keepalive\_requests 100;  
limit\_zone gulag $binary\_remote\_addr 5m;  
recursive\_error\_pages on;  
sendfile on;  
server\_name\_in\_redirect off;  
server\_tokens off;

\## TCP options  
tcp\_nodelay on;  
tcp\_nopush on;

\## Compression  
gzip on;  
gzip\_buffers 16 8k;  
gzip\_comp\_level 6;  
gzip\_http\_version 1.0;  
gzip\_min\_length 0;  
gzip\_types text/plain text/css image/x-icon application/x-perl application/x-httpd-cgi;  
gzip\_vary on;

\## Log Format  
log\_format main '$remote\_addr $host $remote\_user \[$time\_local] "$request" '  
'$status $body\_bytes\_sent "$http\_referer" "$http\_user\_agent" '  
'"$gzip\_ratio"';

\## Proxy options  
proxy\_buffering on;  
proxy\_cache\_min\_uses 3;  
proxy\_cache\_path /usr/local/nginx/proxy\_temp/ levels=1:2 keys\_zone=cache:10m inactive=10m max\_size=1000M;  
proxy\_cache\_valid any 10m;  
proxy\_ignore\_client\_abort off;  
proxy\_intercept\_errors on;  
proxy\_next\_upstream error timeout invalid\_header;  
proxy\_redirect off;  
proxy\_set\_header X-Forwarded-For $remote\_addr;  
proxy\_connect\_timeout 60;  
proxy\_send\_timeout 60;  
proxy\_read\_timeout 60;

\## Backend servers (web1 is the primary and web2 will come up if web1 is down)  
upstream webbackend {  
server web1.domain.lan weight=10 max\_fails=3 fail\_timeout=30s;  
server web2.domain.lan weight=1 backup;  
}

server {  
access\_log /var/log/nginx/access.log main;  
error\_log /var/log/nginx/error.log;  
index index.html;  
limit\_conn gulag 50;  
listen 127.0.0.1:80 default;  
root /usr/local/nginx/html;  
server\_name \_;

\## Only requests to our Host are allowed  
if ($host !~ ^(mydomain.com|www.mydomain.com)$ ) {  
return 444;  
}

\## Only allow these request methods  
if ($request\_method !~ ^(GET|HEAD|POST)$ ) {  
return 444;  
}

\## Only allow these file types to document root  
location / {  
if ($request\_uri ~* (^\\/|\\.html|\\.jpg|\\.pl|\\.png|\\.css|\\.ico|robots\\.txt)$ ) {  
break;  
}  
return 444;  
}

\## PROXY - Forum  
location /forum/ {  
proxy\_pass http://forum.domain.lan/forum/;  
}

\## PROXY - Data  
location /files/ {  
proxy\_pass http://data.domain.lan/;  
}

\## PROXY - Web  
location / {  
proxy\_pass http://webbackend;  
proxy\_cache cache;  
proxy\_cache\_valid 200 24h;  
proxy\_cache\_use\_stale error timeout invalid\_header updating http\_500 http\_502 http\_503 http\_504;  
proxy\_ignore\_headers Expires Cache-Control;  
}

\## All other errors get the generic error page  
error\_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417  
500 501 502 503 504 505 506 507 /error\_page.html;  
location /error\_page.html {  
internal;  
}  
}  
}  
\#  
#######################################################  
\### Calomel.org /etc/nginx.conf END  
#######################################################