nginx負載均衡準備
web01 web02
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf server { listen 80; server_name www.oldboy.com; access_log /var/log/nginx/access_www.log main ; root /app/www; location / { index index.html index.htm; } } [root@web01 /etc/nginx/conf.d]# cat 02-blog.conf server { listen 80; server_name blog.oldboy.com; access_log /var/log/nginx/access_blog.log main; root /app/blog; location / { index index.php index.html index.htm; } location ~* \.(php|php5)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
[root@web01 /etc/nginx/conf.d]# mkdir -p /app/{www,blog} for n in www blog ; do echo `hostname` $n.oldboy.com >/app/$n/index.html ;done
[root@web01 /etc/nginx/conf.d]# tree /app/ /app/ ├── blog │ └── index.html └── www └── index.html 2 directories, 2 files
[root@web01 /etc/nginx/conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /etc/nginx/conf.d]# systemctl reload nginx
[root@lb01 ~]# curl -H Host:www.oldboy.com 10.0.0.[7-8] [1/2]: 10.0.0.7 --> <stdout> --_curl_--10.0.0.7 www.oldboy.com [2/2]: 10.0.0.8 --> <stdout> --_curl_--10.0.0.8 www.oldboy.com [root@lb01 ~]# curl -H Host:blog.oldboy.com 10.0.0.[7-8] [1/2]: 10.0.0.7 --> <stdout> --_curl_--10.0.0.7 blog.oldboy.com [2/2]: 10.0.0.8 --> <stdout> --_curl_--10.0.0.8 blog.oldboy.com
[root@lb01 ~]# cat /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; 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; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream web_pools { server 10.0.0.7:80; server 10.0.0.8:80; } # include /etc/nginx/conf.d/*.conf; server { listen 80; server_name www.oldboy.com; location / { proxy_pass http://web_pools; } } }
CDN: 藍汛 網宿 阿里雲
CDN: 網站加速 緩存網站靜態頁面 視頻(切片)
用戶先訪問cdn
cdn緩存沒有 源站php
[root@lb01 ~]# for n in {1..1000};do curl 10.0.0.5/index.html ;sleep 1 ;done web01 www.oldboy.com ^C [root@lb01 ~]# cat /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; 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; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream web_pools { server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s; server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s; } # include /etc/nginx/conf.d/*.conf; server { listen 80; server_name www.oldboy.com; location / { proxy_pass http://web_pools; } } }
nginx負載均衡 處理多個虛擬主機
[root@lb01 ~]# cat /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; 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; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream web_pools { server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s; server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s; } # include /etc/nginx/conf.d/*.conf; server { listen 80; server_name www.oldboy.com; location / { proxy_pass http://web_pools; } } server { listen 80; server_name blog.oldboy.com; location / { proxy_pass http://web_pools; proxy_set_header Host $host; } } }