1、nginx负载均衡中常见的算法及原理有哪些?
轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.0.14 weight=8;
server 192.168.0.15 weight=10;
}
IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
2、使用rewrite规则实现将所有到a域名的访问rewrite到b域名
location / {
root /data/nginx/html/pc
index index.html;
rewrite /http://www.b.com permanent;
}
3、实现反向代理客户端IP透传
vi /apps/nginx/conf/conf.d/pc.conf
upstream webserver {
server 10.0.0.101:80 weight=1 fail_timeout=5s max_fails=3;
server 10.0.0.102:80 weight=1 fail_timeout=5s max_fails=3 backup;
}
server {
listen 80;
server_name www.magedu.org;
location / {
index index.html index.php
root /data/nginx/html/pc
}
location /web {
index index.html;
proxy_pass http://webserver/;
proxy_ser_header x-Forwarded-For $proxy_add_x_forwarded_for ;
}
}
4、用LNMP实现wordpress站点搭建
1、安装数据库
yum -y install mariadb-server
mysql_secure_installation
2、创建用户并授权
create database wordpress;
create user wordpress@'192.168.253.%' identified by '123456'
grant all on wordpress.* to wordpress@'192.168.253.%';
3、安装PHP
yum -y install php
yum -y install php-fpm
systemctl restart --now php-fpm.service
[root@localhost sbin]# grep '^[^;]' /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
ping.path = /ping
pm.status_path = /pm_status
4、安装nginx
yum -y install gcc pcre-devel openssl-devel zlib-devel
[root@localhost src]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
--2022-02-01 04:29:26-- http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://183.207.33.41:9011/nginx.org/c3pr90ntc0td/download/nginx-1.18.0.tar.gz [following]
--2022-02-01 04:29:26-- http://183.207.33.41:9011/nginx.org/c3pr90ntc0td/download/nginx-1.18.0.tar.gz
Connecting to 183.207.33.41:9011... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’
100%[====================================================================================================================>] 1,039,530 5.43MB/s in 0.2s
2022-02-01 04:29:27 (5.43 MB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]
[root@localhost src]# tar xf nginx-1.18.0.tar.gz
[root@localhost src]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]# ./configure --prefix=/apps/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
make && make install
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/wordpress;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /data/nginx/wordpress;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /document_root$fastcgi_script_name;
include fastcgi_params;
}
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ ^/(ping|pm_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED /document_root$fastcgi_script_name;
}
5、部署wordpress
wget https://wordpress.org/wordpress-5.4.2.tar.gz
cd /data/nginx/wordpress
tar xf https://wordpress.org/wordpress-5.4.2.tar.gz
define( 'DB_NAME', 'wordpress' );
/* MySQL database username /
define( 'DB_USER', 'wordpress' );
/* MySQL database password /
define( 'DB_PASSWORD', '123456' );
/* MySQL hostname /
define( 'DB_HOST', 'localhost' );