haproxy+keepalived+nginx+php-fpm
19508 단어 nginxkeepalivedhaproxy
nginx + keepalived 를 사용 하려 고 했 는데 keepalived 를 높 게 사용 하 는 것 은 문제 가 없 었 습 니 다. 한 사람 이 따라 가 려 고 했 지만 기계 에서 완전히 standby 를 사용 하여 자원 이 낭비 되 었 습 니 다.그래서 부하 균형 군집 을 생각 했다.nginx 가 부하 균형 을 맞 추고 웹 을 하 는 것 을 감안 하여 관리 가 번 거 로 울 까 봐 부하 균형 은 haproxy 로 만 들 었 고 haproxy 의 건강 검 사 는 매우 잘 되 었 다.한편, nginx 의 건강 검 사 는 실제 적 으로 가짜 이 므 로 고장 전 이 를 해 야 합 니 다. timeout 이 기한 이 지나 면 데이터 가 나 뉘 어 통 하지 않 는 것 을 발견 한 후에 고장 전 이 를 하면 웹 페이지 의 간헐 적 로드 가 느 릴 수 있 기 때 문 입 니 다.
본 고의 장점 은 서버 가 평소에 도 메 인 서버 의 데 이 터 를 분담 할 수 있 고 어떤 기계 가 다운 되 어도 웹 서비스 가 정상적으로 접근 할 수 있다 는 것 이다.
환경: CentOS 6.4 x8664
VIP 192.168.122.10
web01 192.168.122.11
web02 192.168.122.12
1. 설치 소프트웨어
- yum install haproxy keepalived nginx php-fpm php-gd php-mysql php-xml php-cli -y
- chkconfig keepalived on
- chkconfig haproxy on
- chkconfig nginx on
- chkconfig php-fpm on
- chkconfig iptables off #
2. 시스템 과 커 널 설정
- sed -i ‘s/enforcing/disabled/g' /etc/sysconfig/selinux
- sysctl -w "net.ipv4.ip_nonlocal_bind = 1"
- echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
위의 이 커 널 매개 변 수 는 서버 가 존재 하지 않 는 주소 에 감청 할 수 있 도록 합 니 다.
3. keepalived 설정 (주종 설정 이 약간 다 름)
홈 서버 웹 01 설정
- global_defs {
- notification_email {
- root@localhost
- }
- notification_email_from keepalived@localhost
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id web01
- }
-
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- preempt
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.122.10 label eth0:1
- }
- }
서버 에서 설정
- global_defs {
- notification_email {
- root@localhost
- }
- notification_email_from keepalived02@localhost
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id web02
- }
-
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 90
- advert_int 1
- #preempt
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.122.10 label eth0:1
- }
- }
4. haproxy 설정 (서버 설정 이 똑 같 고 감청 주소 192.168.12.10: 80)
- global
- log 127.0.0.1 local2
- chroot /var/lib/haproxy
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
-
- defaults
- mode http
- log global
- option httplog
- option dontlognull
- option http-server-close
- option forwardfor except 127.0.0.0/8
- option redispatch
- retries 3
- timeout http-request 10s
- timeout queue 1m
- timeout connect 10s
- timeout client 1m
- timeout server 1m
- timeout http-keep-alive 10s
- timeout check 10s
- maxconn 3000
-
- listen status
- bind *:10086
- stats uri /haproxy-status
- stats auth admin:123456
- stats hide-version
-
-
- frontend haproxy-nlb
- bind 192.168.122.10:80
- default_backend nginx-web
-
-
- backend nginx-web
- option httpchk HEAD /check.txt HTTP/1.0
- balance roundrobin
- server web01 192.168.122.11:80 weight 3 check inter 5s rise 2 fall 3
- server web02 192.168.122.12:80 weight 3 check inter 5s rise 2 fall 3
5. nginx 설정 (감청 주소 가 다른 것 을 제외 하고 모두 같 음)
홈 서버 감청 192.168.122.11: 80
서버 감청 192.168.12.12: 80
- user nginx;
- worker_processes 2;
- worker_rlimit_nofile 65535;
-
-
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
-
- google_perftools_profiles /tmp/tcmalloc;
-
- events {
- use epoll;
- worker_connections 2048;
- }
-
-
- http {
- include /etc/nginx/mime.types;
- include /etc/nginx/naxsi_core.rules;
- default_type application/octet-stream;
-
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
-
- #access_log /var/log/nginx/access.log main;
-
- sendfile on;
- server_tokens off;
-
- #tcp_nopush on;
-
- keepalive_timeout 65;
-
- gzip on;
- gzip_static on;
- gzip_disable "msie6";
- gzip_http_version 1.1;
- gzip_vary on;
- gzip_comp_level 6;
- gzip_proxied any;
- gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x$
- gzip_buffers 16 8k;
-
- client_max_body_size 20m;
- client_body_buffer_size 128k;
- server {
- listen 192.168.122.11:8080;
- server_name localhost;
- root /usr/share/nginx/html;
- index index.html index.htm index.php;
-
- #charset koi8-r;
- access_log /var/log/nginx/host.access.log main;
-
- location / {
- include /etc/nginx/naxsi_conf ;
-
- if (!-e $request_filename) {
- rewrite ^/(.*)$ /index.php?q=$1 last;
- }
- }
-
- #error_page 404 /404.html;
-
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
-
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- location ~ \.php$ {
- #try_files $uri = 404;
- fastcgi_pass 127.0.0.1:9000;
- #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_buffer_size 128k;
- fastcgi_buffers 256 16k;
- fastcgi_busy_buffers_size 256k;
- fastcgi_temp_file_write_size 256k;
- fastcgi_read_timeout 240;
- include fastcgi_params;
- }
-
- if ($fastcgi_script_name ~ \..*\/.*php) {
- return 403;
- }
-
- # deny access to hiden file . (filename begin with ".")
- location ~ /\. {
- access_log off;
- log_not_found off;
- deny all;
- }
-
- # deny access to bakup file .(any filename end with "~" )
- location ~ ~$ {
- access_log off;
- log_not_found off;
- deny all;
- }
-
- # cache image file
- location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|swf)$ {
- expires 1d;
- }
-
- # don't log robots and favion
- location = /robots.txt { access_log off; log_not_found off; }
- location = /favicon.ico { access_log off; log_not_found off; }
-
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- location ~ /\.ht {
- deny all;
- }
- }
- }
6. php - fpm 설정
- sed -i ‘s/apache/nginx/g’ /etc/php-fpm.d/www.conf
7. 기타
웹 01 에 검색 페이지 추가
- echo web01 >/usr/share/nginx/html/check.txt
웹 02 에 검색 페이지 추가
- echo web02 >/usr/share/nginx/html/check.txt
8. 기계 두 대 재 부팅
9. 검증
폴 링 이기 때문에 부하 균형 을 쉽게 감지 할 수 있 습 니 다. (브 라 우 저 를 사용 하지 않 는 것 이 좋 습 니 다. 브 라 우 저 에 캐 시 가 있 습 니 다)
- $ for n in {1..10};do curl http://192.168.122.10/check.txt;done
- web02
- web01
- web02
- web01
- web02
- web01
- web02
- web01
- web02
- web01
OK, 큰 성 과 를 거 두 었 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단! Certbot을 사용하여 웹 사이트를 SSL(HTTPS)화하는 방법초보자가 인프라 주위를 정돈하는 것은 매우 어렵습니다. 이번은 사이트를 간단하게 SSL화(HTTP에서 HTTPS통신)로 변경하는 방법을 소개합니다! 이번에는 소프트웨어 시스템 Nginx CentOS7 의 환경에서 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.