총 게시물 44건, 최근 0 건 안내 RSS 글쓰기
이전글  다음글  목록 글쓰기

Nginx 튜닝 우분투 How To Optimize Nginx Configuration

글쓴이 : 카이로 날짜 : 2014-07-08 (화) 09:01 조회 : 5442
글주소 : http://www.androidside.com/B25/64
  고성능 캐시 서버 STON
DB/WAS 부하 절감과 캐싱 기반의 다양한 미디어 기능을 월과금으로 이용해보세요
  오픈소스 전문기업 락플레이스
오픈소스 국내 업계 No 1. 클라우드, 빅데이터 컨설팅 및 마이그레이션 전문기업
  NGINX 교보문고
NGINX, 신간, 베스트셀러 업데이트, 최대1만원 할인+바로드림 서비스!



Nginx is a fast and lightweight alternative to the sometimes overbearing Apache 2. However, Nginx just like any kind of server or software must be tuned to help attain optimal performance.


Worker Processes and Worker Connections

The first two variables we need to tune are the worker processes and worker connections. Before we jump into each setting, we need to understand what each of these directives control. The worker_processes directive is the sturdy spine of life for Nginx. This directive is responsible for letting our virtual server know many workers to spawn once it has become bound to the proper IP and port(s). It is common practice to run 1 worker process per core. Anything above this won't hurt your system, but it will leave idle processes usually just lying about.

To figure out what number you'll need to set worker_processes to, simply take a look at the amount of cores you have on your setup. If you're using the DigitalOcean 512MB setup, then it'll probably be one core. If you end up fast resizing to a larger setup, then you'll need to check your cores again and adjust this number accordingly. We can accomplish this by greping out the cpuinfo:

grep processor /proc/cpuinfo | wc -l

Let's say this returns a value of 1. Then that is the amount of cores on our machine!

The worker_connections command tells our worker processes how many people can simultaneously be served by Nginx. The default value is 768; however, considering that every browser usually opens up at least 2 connections/server, this number can half. This is why we need to adjust our worker connections to its full potential. We can check our core's limitations by issuing a ulimit command:

ulimit -n

On a smaller machine (512MB droplet) this number will probably read 1024, which is a good starting number.

Let's update our config:

sudo nano /etc/nginx/nginx.conf

worker_processes 1;
worker_connections 1024;

Remember, the amount of clients that can be served can be multiplied by the amount of cores. In this case, we can server 1024 clients/second. This is, however, even further mitigated by the keepalive_timeout directive.


Another incredibly important tweak we can make is to the buffer size. If the buffer sizes are too low, then Nginx will have to write to a temporary file causing the disk to read and write constantly. There are a few directives we'll need to understand before making any decisions.

client_body_buffer_size: This handles the client buffer size, meaning any POST actions sent to Nginx. POST actions are typically form submissions.

client_header_buffer_size: Similar to the previous directive, only instead it handles the client header size. For all intents and purposes, 1K is usually a decent size for this directive.

client_max_body_size: The maximum allowed size for a client request. If the maximum size is exceeded, then Nginx will spit out a 413 error or Request Entity Too Large.

large_client_header_buffers: The maximum number and size of buffers for large client headers.

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;


Timeouts can also drastically improve performance.

The client_body_timeout and client_header_timeout directives are responsible for the time a server will wait for a client body or client header to be sent after request. If neither a body or header is sent, the server will issue a 408 error or Request time out.

The keepalive_timeout assigns the timeout for keep-alive connections with the client. Simply put, Nginx will close connections with the client after this period of time.

Finally, the send_timeout is established not on the entire transfer of answer, but only between two operations of reading; if after this time client will take nothing, then Nginx is shutting down the connection.

client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;

Gzip Compression

Gzip can help reduce the amount of network transfer Nginx deals with. However, be careful increasing the gzip_comp_level too high as the server will begin wasting cpu cycles.

gzip             on;
gzip_comp_level  2;
gzip_min_length  1000;
gzip_proxied     expired no-cache no-store private auth;
gzip_types       text/plain application/x-javascript text/xml text/css application/xml;

Static File Caching

It's possible to set expire headers for files that don't change and are served regularly. This directive can be added to the actual Nginx server block.

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;

Add and remove any of the file types in the array above to match the types of files your Nginx servers.


Nginx logs every request that hits the VPS to a log file. If you use analytics to monitor this, you may want to turn this functionality off. Simply edit the access_log directive:

access_log off

Save and close the file, then run:

sudo service nginx restart


At the end of the day a properly configured server is one that is monitored and tweaked accordingly. None of the variables above are set in stone and will need to be adjusted to each unique case. Even further down the road, you may be looking to further your machine performance with research in load balancing and horizontal scaling. These are just a few of the many enhancements a good sysadmin can make to a server.

로그인 창이 있는 "정보 수정"에서
서명 이쁘게 등록해보세요 ^^

이전글  다음글  목록 글쓰기

총 게시물 44건, 최근 0 건 안내 RSS
번호 분류 제목 글쓴이 날짜 조회
44 서버
현재 대학생인데 과제중입니다 핸드폰을 이용하여 마우스를 대체하는 어플리케이션 및 프로그램을 만드려고하…
박민용 10-05 1154
43 OS
이카루스031 05-09 2962
42 OS
이카루스031 05-09 3092
41 OS
Linux 시스템 로그인에 관련된 정보 로그 파일혹 Linux 관리가 필요한 분들 참고하세요.....시스템에 접근한 사람들 …
예민한인어 03-14 3560
40 OS
Postfix를 이용한 메일서버 개설 http://www.atblog.co.kr/?p=4877
카이로 12-18 3894
39 서버
sudo apt-get install ntpdate sudo apt-get install rdate sudo ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime sudo rdate -s time.bora.net
카이로 08-01 5445
38 VPS
VPS 간단 비교 설명  디지털오션 DigitalOcean  1. 저렴한 가격(최소 5$부터 시작) 2. 매우 쉬운 UI 3. 인기있…
카이로 07-10 5643
37 서버
SCP를 사용하여 자동으로 데이터백업   v     상황: 특정 디렉터리나 특정 데이터를 원…
카이로 07-10 5460
36 서버
리눅스 서버 간에 파일 복사에 사용되는 명령어는 scp가 대표적이겠죠? scp에 대해서 간략하게 설명하고자 합니…
카이로 07-10 7230
35 서버
Introduction Nginx Nginx is a fast and lightweight alternative to the sometimes overbearing Apache 2. However, Nginx just like any kind of server or…
카이로 07-08 5443
34 디지털오션
출처. http://www.clien.net/cs2/bbs/board.php?bo_table=lecture&wr_id=226630 들어가기에 앞서...-블로그에 쓴 글을 그대로 …
카이로 07-07 5627
33 서버
최상위 index.php에 아래 코드 입력 ini_set('session.cookie_domain', '.domain.com' );
카이로 07-06 4328
32 VPS
DigitalOcean vs Linode  디지털오션과 리노드 상세 비교 This is a very detailed DigitalOcean vs Linode VPS cloud hosting comparison r…
카이로 07-06 5548
31 디지털오션
How To Choose an Effective Backup Strategy for your VPS Introduction An important consideration when storing your work and data in a digital environ…
카이로 07-06 4613
30 VPC
2013 VPS Benchmarks - Linode, Digital Ocean, Hot Drupal Every year or two, I like to get a good overview of different hosting providers' VPS performa…
카이로 07-06 6238
29 VPC
Linode vs DigitalOcean, performance benchmarks I’ve been a Linode fan and customer for years, but just this week I was made aware of a competing …
카이로 07-06 3814
28 VPC
Live Benchmarks from the Cloud Cloudlook provides constantly updated benchmarksfor server instances at popular public cloud providers. …
카이로 07-06 4356
27 VPC
사양이나 필요한 부분만 보세요^^ Linode 2GB VPS versus DigitalOcean 2GB VPS – Centmin Mod Nginx CLI install benchmarks Linode has…
카이로 07-06 4398
26 서버
for file in *.log; do     iconv -f ISO-8859-1 -t UTF-8 -o "$file".utf "$file" && mv "$file".utf "$file" done
카이로 07-05 3903
25 서버
우분투에서 한국시간으로 변경하기 ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
카이로 07-04 4037
 1  2  3  맨끝

Copyright ⓒ www.androidside.com. All rights reserved.
채팅 권한: 글쓰기 1개
2레벨 이상만 대화 가능
공개 채팅: 평일 !(9시 ~ 17시),토,일
안사2 변경사항 보러가기 챗방이 잘 안보이면 크롬에서 접속해주세요
챗방 숨기기 |  챗방 보이기