차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
tech:nginx [2019/04/28 20:43] – 104.131.75.86 | tech:nginx [2024/05/17 07:35] (현재) – V_L | ||
---|---|---|---|
줄 1: | 줄 1: | ||
- | 77ㅓ억 | + | {{tag> |
+ | ======Nginx====== | ||
+ | |||
+ | 엔진엑스 nginx는 러시아 개발자(Igor Sysoev)가 혼자서 만든 프로젝트이지만, | ||
+ | |||
+ | http:// | ||
+ | |||
+ | 특히 nginx는 http:// | ||
+ | 현재 Nginx를 쓰는 유명한 곳으로는 페이스북, | ||
+ | |||
+ | {{http:// | ||
+ | |||
+ | nginx 는 비동기(async) 이벤트 기반(ioctl, | ||
+ | |||
+ | 따라서, 더 적은 자원으로 더 빠르게 데이터를 서비스 할 수 있다. | ||
+ | |||
+ | =====성능===== | ||
+ | |||
+ | 성능은 접속자가 어느 정도 이상 되고 다양한 사이트가 운영되면 차이가 많이 난다. 동접자가 많을수록 체감할수 있다 물론 설정을 시스템에 맞게 잘 해주면 효과가 극대화 되겠다. | ||
+ | 서버가 리눅스일경우 그리고 멀티코어일 경우 엔진엑스와 아파치의 차이가 많이 난다. | ||
+ | |||
+ | 한국에서 엔진엑스를 많이 안쓰는 이유는 한글 문서들이 많이 없기 때문이다. 호스팅 서비스의 경우 rewrite 관련 부분을 사용하는 호스팅 사용자가 많은 경우 상당히 귀찮아 질 수 있다. | ||
+ | |||
+ | 보통 접속자가 많은 사이트 (토렌트, 유머 등등) 인 경우 엔진엑스를 많이 쓰고 있는 추세이다, | ||
+ | 성능을 원하면 Nginx로 셋팅하자. | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | Use nginx in the front as proxy, and serving all static content (css, images, js) and leaving the php process to Apache. | ||
+ | |||
+ | =====기본구조비교===== | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | |||
+ | =====단점===== | ||
+ | | ||
+ | |||
+ | =====윈도용===== | ||
+ | |||
+ | 윈도우용 Nginx는 native win32 api를 이용하며, | ||
+ | |||
+ | http:// | ||
+ | =====부가기능===== | ||
+ | |||
+ | nginx는 | ||
+ | |||
+ | - SSL | ||
+ | - load balancing | ||
+ | - gzip | ||
+ | - url rewriting | ||
+ | - webdav | ||
+ | - access authentication | ||
+ | - smtp, pop3, imap | ||
+ | - flv/mp4 streaming | ||
+ | - speed limitation | ||
+ | |||
+ | =====설치===== | ||
+ | |||
+ | 우분투에서 패키지를 이용해 설치하면된다. | ||
+ | Nginx에서 [[tech: | ||
+ | |||
+ | sudo apt-get install php5-fpm | ||
+ | |||
+ | 이제 다음 명령으로 Nginx를 설치. | ||
+ | |||
+ | < | ||
+ | sudo -s | ||
+ | nginx=stable # use nginx=development for latest development version | ||
+ | add-apt-repository ppa: | ||
+ | apt-get update | ||
+ | apt-get install nginx | ||
+ | </ | ||
+ | |||
+ | 혹시 업그레이드가 잘 안먹으면 | ||
+ | |||
+ | sudo apt-get dist-upgrade | ||
+ | |||
+ | =====설정===== | ||
+ | |||
+ | Nginx 설정은 ''/ | ||
+ | |||
+ | PHP5와 Nginx 연동하기위해 사이트 설정 파일에 다음 줄을 추가한다. | ||
+ | |||
+ | < | ||
+ | fastcgi_split_path_info ^(.+\.php)(/ | ||
+ | if (!-f $document_root$fastcgi_script_name) { | ||
+ | return 404; | ||
+ | } | ||
+ | fastcgi_pass unix:/ | ||
+ | fastcgi_index index.php; | ||
+ | fastcgi_param | ||
+ | include fastcgi_params; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | 설정을 모두 마쳤으면 Nginx를 재시작. (줄 끝 '';'' | ||
+ | |||
+ | ====fpm unix socket 연결==== | ||
+ | |||
+ | ''/ | ||
+ | |||
+ | Look for the line | ||
+ | listen = 127.0.0.1: | ||
+ | and change it to something like | ||
+ | listen = / | ||
+ | listen = / | ||
+ | |||
+ | After doing so, restart PHP FPM: | ||
+ | |||
+ | sudo service php5-fpm restart | ||
+ | sudo service nginx restart | ||
+ | |||
+ | |||
+ | |||
+ | ===소켓 연결 에러=== | ||
+ | |||
+ | 유닉스소켓 연결이 조금 더 빠르기는 하지만 TCP/IP 만큼 Scalable 하지는 않다. 일일 방문자가 만명 수준인 본 사이트(http:// | ||
+ | |||
+ | 이런 경우 nginx가 502 에러를 뿜는다고 하는데, 그냥 먹통이 되기도 한다. 소켓 세팅을 만지거나 그냥 | ||
+ | |||
+ | In your fastcgi conf change: | ||
+ | |||
+ | fastcgi_pass unix:/ | ||
+ | to: | ||
+ | |||
+ | fastcgi_pass 127.0.0.1: | ||
+ | |||
+ | http:// | ||
+ | |||
+ | |기본 자료 폴더|/ | ||
+ | |기본 디먼|www-data| | ||
+ | |로그|/ | ||
+ | |로그|/ | ||
+ | |||
+ | ====Rewrite==== | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | * last vs rewrite | ||
+ | * Okay, so " | ||
+ | * " | ||
+ | |||
+ | ===서브도메인 === | ||
+ | |||
+ | < | ||
+ | server_name m.openwiki.kr; | ||
+ | - return 301 $scheme:// | ||
+ | | ||
+ | }</ | ||
+ | |||
+ | [[tech: | ||
+ | |||
+ | ===핫링크막기=== | ||
+ | |||
+ | < | ||
+ | |||
+ | location ~ \.(gif|png|jpg|jpeg|JPG|GIF|JPEG|PNG)$ { | ||
+ | valid_referers none blocked | ||
+ | | ||
+ | | ||
+ | if ($invalid_referer) { | ||
+ | rewrite \.(gif|png|jpg|jpeg|JPG|GIF|JPEG|PNG)$ | ||
+ | | ||
+ | redirect; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===Dokuwiki (도메인루트 & try_files 사용)=== | ||
+ | < | ||
+ | server { | ||
+ | listen | ||
+ | root / | ||
+ | server_name vaslor.net; | ||
+ | |||
+ | location / { | ||
+ | try_files $uri $uri/ @dk; | ||
+ | rewrite ^/$ /doku.php last; | ||
+ | } | ||
+ | |||
+ | location @dk { | ||
+ | rewrite ^/(.*) / | ||
+ | } | ||
+ | |||
+ | rewrite ^/ | ||
+ | rewrite ^/ | ||
+ | rewrite ^/ | ||
+ | |||
+ | include common.conf; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | ====Expire==== | ||
+ | < | ||
+ | # 브라우져 캐쉬 세팅 | ||
+ | location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { | ||
+ | expires 65d; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====htaccess==== | ||
+ | 보안을 위하여 아파치의 htaccess를 감춘다. | ||
+ | < | ||
+ | location ~ /\.ht { | ||
+ | deny all; | ||
+ | } | ||
+ | </ | ||
+ | ====Compression==== | ||
+ | / | ||
+ | |||
+ | http section | ||
+ | < | ||
+ | |||
+ | - enable gzip compression | ||
+ | gzip on; | ||
+ | gzip_min_length | ||
+ | gzip_buffers | ||
+ | gzip_types | ||
+ | gzip_vary on; | ||
+ | - end gzip configuration | ||
+ | </ | ||
+ | |||
+ | ====domain==== | ||
+ | |||
+ | < | ||
+ | ## Server Block one | ||
+ | server { | ||
+ | server_name www.domain1.com; | ||
+ | access_log logs/ | ||
+ | root / | ||
+ | } | ||
+ | ## Server Block two | ||
+ | server { | ||
+ | server_name www.domain2.com; | ||
+ | access_log logs/ | ||
+ | root / | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====subdomain==== | ||
+ | |||
+ | 이런식이다. | ||
+ | |||
+ | < | ||
+ | listen 80; | ||
+ | root / | ||
+ | index index.php; | ||
+ | server_name test.vaslor.net; | ||
+ | |||
+ | location / { | ||
+ | try_files $uri $uri/ index.php; | ||
+ | } | ||
+ | include common.conf; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====redirect subdomain to port ==== | ||
+ | |||
+ | < | ||
+ | server { | ||
+ | listen 80; | ||
+ | server_name app.example.com; | ||
+ | |||
+ | location / { | ||
+ | proxy_pass http:// | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====max children==== | ||
+ | ''/ | ||
+ | < | ||
+ | pm.max_children = 128 | ||
+ | |||
+ | pm.start_servers = 10 | ||
+ | |||
+ | pm.min_spare_servers = 4 | ||
+ | |||
+ | pm.max_spare_servers = 16 | ||
+ | |||
+ | pm.max_requests = 500 | ||
+ | </ | ||
+ | ====성가신 로그 끄기==== | ||
+ | [[var_log]] | ||
+ | |||
+ | <file conf> | ||
+ | - Avoids filling up the error logs with commonly requested files. | ||
+ | - Note that if you have a custom 404 page, the request may be logged anyway. | ||
+ | location = /robots.txt { log_not_found off; } | ||
+ | location = / | ||
+ | location = / | ||
+ | location = / | ||
+ | location = / | ||
+ | location = / | ||
+ | </ | ||
+ | |||
+ | 접근 로그는 '' | ||
+ | access_log off; | ||
+ | 에러 로그도 끌 수 있다. | ||
+ | error_log off; | ||
+ | =====아파치 호환성===== | ||
+ | |||
+ | ====getallheaders()==== | ||
+ | |||
+ | getallheaders는 아파치의 헤더를 받아오는 것으로 nginx에서는 작동하지 않는다. 호환성을 위해 다음과 같은 함수를 만들어 쓰면 된다. | ||
+ | |||
+ | <file php> | ||
+ | if (!function_exists(' | ||
+ | { | ||
+ | function getallheaders() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | { | ||
+ | if (substr($name, | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * 참고: [[http:// | ||
+ | |||
+ | =====참고===== | ||
+ | |||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ |