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

Nginx 튜닝 우분투 How To Optimize Nginx Configuration

글쓴이 : 카이로 날짜 : 2014-07-08 (화) 09:01 조회 : 4560
글주소 : http://www.androidside.com/B25/64
 
  G마켓 NGINX
NGINX, 베스트셀러, 관련도서, 15%혜택, 인기음반, 제휴카드 추가할인
www.gmarket.co.kr
  11번가 NGINX
카드최대15%할인, 무이자12개월, OK캐쉬백 적립! NGINX
www.11st.co.kr
  오픈소스 전문기업 락플레이스
오픈소스 국내 업계 No 1. 클라우드, 빅데이터 컨설팅 및 마이그레이션 전문기업
rockplace.co.kr
신청하기


Introduction

Nginx

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.

Requirements

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.

Buffers

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

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.

Logging

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

Conclusion

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.




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

hi
이전글  다음글  목록 글쓰기

총 게시물 44건, 최근 0 건 안내 RSS
번호 분류 제목 글쓴이 날짜 조회
44 서버
현재 대학생인데 과제중입니다 핸드폰을 이용하여 마우스를 대체하는 어플리케이션 및 프로그램을 만드려고하…
박민용 10-05 494
43 OS
이카루스031 05-09 2306
42 OS
이카루스031 05-09 2477
41 OS
Linux 시스템 로그인에 관련된 정보 로그 파일혹 Linux 관리가 필요한 분들 참고하세요.....시스템에 접근한 사람들 …
예민한인어 03-14 2857
40 OS
Postfix를 이용한 메일서버 개설 http://www.atblog.co.kr/?p=4877
카이로 12-18 3155
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 4598
38 VPS
VPS 간단 비교 설명  디지털오션 DigitalOcean  1. 저렴한 가격(최소 5$부터 시작) 2. 매우 쉬운 UI 3. 인기있…
카이로 07-10 4837
37 서버
SCP를 사용하여 자동으로 데이터백업   v     상황: 특정 디렉터리나 특정 데이터를 원…
카이로 07-10 4585
36 서버
리눅스 서버 간에 파일 복사에 사용되는 명령어는 scp가 대표적이겠죠? scp에 대해서 간략하게 설명하고자 합니…
카이로 07-10 6180
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 4561
34 디지털오션
출처. http://www.clien.net/cs2/bbs/board.php?bo_table=lecture&wr_id=226630 들어가기에 앞서...-블로그에 쓴 글을 그대로 …
카이로 07-07 4812
33 서버
최상위 index.php에 아래 코드 입력 ini_set('session.cookie_domain', '.domain.com' );
카이로 07-06 3673
32 VPS
DigitalOcean vs Linode  디지털오션과 리노드 상세 비교 This is a very detailed DigitalOcean vs Linode VPS cloud hosting comparison r…
카이로 07-06 4716
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 3774
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 5482
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 3165
28 VPC
Live Benchmarks from the Cloud Cloudlook provides constantly updated benchmarksfor server instances at popular public cloud providers. …
카이로 07-06 3670
27 VPC
사양이나 필요한 부분만 보세요^^ Linode 2GB VPS versus DigitalOcean 2GB VPS – Centmin Mod Nginx CLI install benchmarks Linode has…
카이로 07-06 3497
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 3242
25 서버
우분투에서 한국시간으로 변경하기 ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
카이로 07-04 3297
 1  2  3  맨끝
 


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