nginx动静分离 、负载均衡、反向代理 - \c'
配置nginx动静分离 、负载均衡、反向代理(wordpressd的安装部署) 拼多多帮助力入口
rewrite
语法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
此处的$1用于引用(.*.jpg)匹配到的内容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
如上例所示,replacement可以是某个路径,也可以是某个URL
常见的flag
flag作用last基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个 一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理 而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程break中止Rewrite,不再继续匹配 一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求, 且不再会被当前location内的任何rewrite规则所检查redirect以临时重定向的HTTP状态302返回新的URLpermanent以永久重定向的HTTP状态301返回新的URL
rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)
nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:
标识符意义^必须以^后的实体开头$必须以$前的实体结尾.匹配任意字符[]匹配指定字符集内的任意字符[^]匹配任何不包括在指定字符集内的任意字符串|匹配 | 之前或之后的实体()分组,组成一组用于匹配的实体,通常会有 | 来协助
捕获子表达式,可以捕获放在()之间的任何文本,比如:
^(hello|sir)$ //字符串为“hi sir”捕获的结果:$1=hi$2=sir
//这些被捕获的数据,在后面就可以当变量一样使用了
if
语法:if (condition) {...}
应用场景:
server段location段
常见的condition
变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)正则表达式的模式匹配操作
~:区分大小写的模式匹配检查~*:不区分大小写的模式匹配检查!和!*:对上面两种测试取反 测试指定路径为文件的可能性(-f,!-f)测试指定路径为目录的可能性(-d,!-d)测试文件的存在性(-e,!-e)检查文件是否有执行权限(-x,!-x)
基于浏览器实现分离案例
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) {
rewrite ^(.*)$ /chrome/$1 break;
}
防盗链案例
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.idfsoft.com;
if ($invalid_referer) {
rewrite ^/ http://www.idfsoft.com/403.html;
}
}
反向代理与负载均衡
nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。
nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。
但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。
Http Proxy`模块,功能很多,最常用的是`proxy_pass`和`proxy_cache
如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如: ./configure --add-module=../ngx_cache_purge-1.0 ......
nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内
在upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:
upstream idfsoft.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。
定义好upstream后,需要在server段内添加如下内容:
server {
location / {
proxy_pass http://idfsoft.com;
}
}
实验环境准备3台虚拟机
一台配置nginx实现负载均衡(192.168.159.141) 一台配置apache(192.168.159.139) 一台配置lnmp(192.168.159.147)
配置nginx负载均衡,反向代理
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
添加upstream webs 与 下面server平齐 修改location
upstream webs {
server 192.168.159.139;
server 192.168.159.147;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://webs;
}
浏览器访问IP地址达到效果
动静分离
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# systemctl restart nginx
upstream danymic {
server 192.168.159.147;(动态)
}
upstream static {
server 192.168.159.139;(静态)
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://static;
}
#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://danymic;
}
单独访问IP地址只有静态页面
加上/index.php就会访问动态
wordpress
[root@node1 ~]# tar xf wordpress-6.4.3-zh_CN.tar.gz
[root@node1 ~]# ls
anaconda-ks.cfg pass wordpress
mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz php-8.3.0 wordpress-6.4.3-zh_CN.tar.gz
nginx-1.24.0 php-8.3.0.tar.xz
[root@node1 ~]# ls wordpress
index.php wp-blog-header.php wp-includes wp-settings.php
license.txt wp-comments-post.php wp-links-opml.php wp-signup.php
readme.html wp-config-sample.php wp-load.php wp-trackback.php
wp-activate.php wp-content wp-login.php xmlrpc.php
wp-admin wp-cron.php wp-mail.php
[root@node1 ~]# mv wordpress/* /usr/local/nginx/html/
[root@node1 ~]# cd /usr/local/nginx/html/
[root@node1 html]# ls
index.php wp-blog-header.php wp-includes wp-settings.php
license.txt wp-comments-post.php wp-links-opml.php wp-signup.php
readme.html wp-config-sample.php wp-load.php wp-trackback.php
wp-activate.php wp-content wp-login.php xmlrpc.php
wp-admin wp-cron.php wp-mail.php
创建一个数据库
[root@node1 html]# mysql -uroot -pPassw0rd@_
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>create database wordpress;
Query OK, 1 row affected (0.01 sec)
在静态页面添加ip达到负载均衡的效果
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
upstream danymic {
server 192.168.159.147;
}
upstream static {
server 192.168.159.139;
server 192.168.159.147;
}
加上wp-login.php则访问动态
动静分离(lnmp部署zabbix)
[root@node1 ~]# cd /usr/local/nginx/html/
[root@node1 ~]# ls
50x.html index.html index.php
[root@node1 html]# mkdir test
[root@node1 html]# mv index.html test/
[root@node1 html]# ls
50x.html index.php test
[root@node1 html]# systemctl restart nginx
zabbix安装
安装依赖包
[root@localhost ~]# yum -y install net-snmp-devel libevent-devel
下载zabbix安装包
[root@localhost ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/6.4/zabbix-6.4.10.tar.gz
--2024-02-20 21:56:15-- https://cdn.zabbix.com/zabbix/sources/stable/6.4/zabbix-6.4.10.tar.gz
Resolving cdn.zabbix.com (cdn.zabbix.com)... 104.26.6.148, 172.67.69.4, 104.26.7.148
Connecting to cdn.zabbix.com (cdn.zabbix.com)|104.26.6.148|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 45608366 (43M) [application/octet-stream]
Saving to: ‘zabbix-6.4.10.tar.gz’
zabbix-6.4.10.tar.gz 100%[=========================>] 43.50M 3.54MB/s in 18s
2024-02-20 21:56:34 (2.45 MB/s) - ‘zabbix-6.4.10.tar.gz’ saved [45608366/45608366]
解压zabbix安装包
[root@localhost ~]# tar xf zabbix-6.4.10.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg apr-util-1.6.3 php-8.3.0 zabbix-6.4.10.tar.gz
apr-1.7.4 httpd-2.4.58 zabbix-6.4.10
创建zabbix用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin zabbix
配置zabbix数据库
[root@localhost ~]# mysql -uroot -pPassw0rd@_
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.00 sec)
mysql>create user 'zabbix'@'localhost' identified by 'zabbix@123';
Query OK, 0 rows affected (0.01 sec)
mysql>grant all privileges on zabbix.* to 'zabbix'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>SET GLOBAL log_bin_trust_function_creators = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>quit
Bye
将数据导入数据库
[root@localhost ~]# hostnamectl set-hostname zabbix-server
[root@zabbix-server ~]# cd zabbix-6.4.10/
[root@zabbix-server zabbix-6.4.10]# ls
aclocal.m4 ChangeLog config.sub database install-sh man README
AUTHORS compile configure depcomp m4 misc sass
bin conf configure.ac include Makefile.am missing src
build config.guess COPYING INSTALL Makefile.in NEWS ui
[root@zabbix-server zabbix-6.4.10]# cd database/mysql/
[root@zabbix-server mysql]# ls
data.sql images.sql Makefile.am Makefile.in option-patches schema.sql
[root@zabbix-server mysql]# mysql -uroot -pPassw0rd@_ zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zabbix-server mysql]# mysql -uroot -pPassw0rd@_ zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zabbix-server mysql]# mysql -uroot -pPassw0rd@_ --default-character-set=utf8mb4 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
将此项值改为0
[root@zabbix-server ~]# mysql -uroot -pPassw0rd@_
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>SET GLOBAL log_bin_trust_function_creators = 0;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>quit
Bye
设置gnu扩展
[root@zabbix-server ~]# cd zabbix-6.4.10/
[root@zabbix-server zabbix-6.4.10]# cd database/mysql/
[root@zabbix-server mysql]# export CFLAGS="-std=gnu99"
编译安装zabbix
[root@zabbix-server zabbix-6.4.10]# ./configure --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2
......................................................
Enable agent 2: no
Enable web service: no
Enable Java gateway: no
LDAP support: no
IPv6 support: no
cmocka support: no
yaml support: no
***********************************************************
* Now run 'make install' *
* *
* Thank you for using Zabbix! *
**
***********************************************************
[root@zabbix-server zabbix-6.4.10]# make install
编辑zabbix服务端配置文件
[root@zabbix-server ~]# cd /usr/local/etc/
[root@zabbix-server etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d zabbix_server.conf zabbix_server.conf.d
[root@zabbix-server etc]# vim zabbix_server.conf
DBPassword=zabbix@123
启动zabbix
[root@zabbix-server etc]# zabbix_server
[root@zabbix-server etc]# zabbix_agentd
[root@zabbix-server etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 4096 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 511 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
设置zabbix的web页面
[root@zabbix-server ~]# cd zabbix-6.4.10/
[root@zabbix-server zabbix-6.4.10]# ls
aclocal.m4 compile config.sub depcomp Makefile missing
ui
AUTHORS conf configure include Makefile.am NEWS
bin config.guess configure.ac INSTALL Makefile.in README
build config.log COPYING install-sh man sass
ChangeLog config.status database m4 misc src
[root@zabbix-server zabbix-6.4.10]# cp -a ui/*
/usr/local/apache/htdocs/test/
cp: overwrite '/usr/local/apache/htdocs/test/index.php'? y
[root@zabbix-server zabbix-6.4.10]# ls /usr/local/apache/htdocs/test/
api_jsonrpc.php conf include
report4.php
api_scim.php data index.html
robots.txt
app disc_prototypes.php index_http.php setup.php
assets favicon.ico index.php
sysmap.php
audio graphs.php index_sso.php
sysmaps.php
browserwarning.php history.php items.php
templates.php
chart2.php host_discovery.php js test
chart3.php hostinventoriesoverview.php jsLoader.php
toptriggers.php
chart4.php hostinventories.php jsrpc.php
tr_events.php
chart6.php host_prototypes.php local
trigger_prototypes.php
chart7.php httpconf.php locale
triggers.php
chart.php httpdetails.php map.php vendor
composer.json image.php modules widgets
composer.lock imgstore.php report2.php
zabbix.php
[root@localhost zabbix-6.4.10]# chown -R apache.apache /usr/local/apache/
[root@localhost zabbix-6.4.10]# ll /usr/local/apache/
total 40
drwxr-xr-x. 2 apache apache 4096 Feb 20 18:44 bin
drwxr-xr-x. 2 apache apache 167 Feb 20 18:44 build
drwxr-xr-x. 2 apache apache 78 Feb 20 18:44 cgi-bin
drwxr-xr-x. 4 apache apache 84 Feb 20 16:12 conf
drwxr-xr-x. 3 apache apache 4096 Feb 20 18:44 error
drwxr-xr-x. 3 apache apache 36 Feb 20 16:09 htdocs
drwxr-xr-x. 3 apache apache 8192 Feb 20 18:44 icons
drwxr-xr-x. 2 apache apache 4096 Feb 20 18:44 include
drwxr-xr-x. 2 apache apache 58 Feb 20 16:12 logs
drwxr-xr-x. 4 apache apache 30 Feb 20 18:44 man
drwxr-xr-x. 14 apache apache 8192 Feb 20 18:44 manual
drwxr-xr-x. 2 apache apache 4096 Feb 20 18:44 modules
重启apache
[root@zabbix-server zabbix-6.4.10]# systemctl restart httpd
访问web界面
设置web界面安装配置
[root@localhost ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@localhost ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@localhost ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@localhost ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@localhost ~]# systemctl restart php-fpm
登录zabbix (账号:Admin 密码:zabbix)
实现动静分离
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
upstream danymic {
server 192.168.159.147;(动态)
}
upstream static {
server 192.168.159.139;(静态)
server 192.168.159.147;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://static;
}
#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://danymic;
}
[root@localhost ~]# systemctl restart nginx
成功达到动静分离的效果
推荐阅读