本文将继Nginx (二) 继续讲解nginx
nginx (三)
下面集群访问测试可如此进行:
[root@lb02 ~]# curl -H host:www.xiaoxi.com 10.0.0.5
web01 page
[root@lb02 ~]# curl -H host:www.xiaoxi.com 10.0.0.5
web02 page
[root@lb02 ~]# curl -H host:www.xiaoxi.com 10.0.0.5
web03 page
1、LNMP配置
1.1安装部署软件
安装部署:Nginx
mariadb: 安装部署
yum install -y mariadb-server mariadb
PHP: 安装部署
yum remove php-mysql php php-fpm php-common
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
1.2软件配置 测试 Nginx-->php 建立连接
编写 nginx 配置文件
server {
listen 80;
server_name blog.xiaoxi.com;
location /{
root /html/blog;
index index.php index.html index.htm ;
location ~ \.php$ {
root /html/blog;
fastcgi_index index.php; #当匹配到这个location 的时候默认加载.php的文件
fastcgi_pass 127.0.0.1:9000; #指定本地php-fpm端口为9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# fastcgi_param SCRIPT_FILENAME 这一行是固定模式,意为调用php脚本文件的存放位置;
# $document_root$fastcgi_script_name; <==> /html/blog/index.php($document_root=/html/blog;$fastcgi_script_name=index.php )
# include fastcgi_params fastcgi变量存放文件,用于识别这里的一些变量信息。
编写php动态测试文件,用于测试php页面能否正常显示
[root@web03 /html/blog]#echo "<?php phpinfo (); ?>" /html/blog/index.php
[root@web03 /html/blog]# cat /html/blog/index.php
<?php phpinfo (); ?>
1.3编写测试文件 php-->mysql建立关系
mysqladmin -uroot password "123456"
[root@web03 /html/blog]# cat index.php
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
//$link_id=mysql_connect('主机名','用户','密码');
//mysql -u用户 -p密码 -h 主机
$conn = mysqli_connect($servername, $username, $password);
if ($conn) {
echo "mysql successful by oldboy !\n";
}else{
die("Connection failed: " . mysqli_connect_error());
}
?>
至此可以看到LNMP环境测试成功
2、利用WordPress搭建博客环境
2.1 将上述的LNMP环境搭建完成
2.2实现代码上线
a、将代码解压放置站点目录中 并更改属主、属主为运行程序用户
chown -R www.www blog/ --- 属主为nginx worker进程用户
b、 调整站点目录数据权限
vim /etc/php-fpm.d/www.conf
user = www
group = www
c、将wp-config-sample.php 更改文件名为 wp-config.php
cp wp-config-sample.php wp-config.php
wp-config.php
2.3创建数据库环境
[root@web03 /html/blog]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
3、使用mysqldump 对数据库进行简单备份(数据并不多的数据库)
[root@web03 ~]# mysqldump -uroot -p123456 -A >/tmp/mysql.bak #对数据库进行备份
[root@web03 ~]# scp /tmp/mysql.bak 10.0.0.8:/tmp #对数据库进行传输
[root@web02 ~]# mysql -uroot -p123456 </tmp/backup.sql #另一台主机进行数据库恢复
4、实现数据共享存储
image.png5、伪静态处理
这里以wordpress 博客网站为例
第一步: 修改网站后台设置
设置---固定链接---自定义结构
/%post_id%.html
第二步: 编写nginx配置文件
[root@web01 html]# cat /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.oldboy.com;
rewrite ^(.*) https://$server_name$1 redirect;
}
server {
listen 443 ssl;
server_name www.xiaoxi.ren;
client_max_body_size 5m;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
rewrite /wp-admin$ $scheme://$host$uri/ permanent; --- 伪静态地址信息重写
location / {
root /html/blog;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args==$uri; --- 访问伪静态页面
}
location ~ \.php$ {
root /html/blog;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
6、反向代理负载均衡
第一步 环境准备(4台主机)
lb01 负载均衡服务器 10.0.0.5
web01 web集群服务器 10.0.0.7
web02 web集群服务器 10.0.0.8
web03 web集群服务器 10.0.0.9
#web01
mkdir /html/www/ -p
echo "web01 www.oldboy.com" >/html/www/oldboy.html
#web02
mkdir /html/www/ -p
echo "web02 www.oldboy.com" >/html/www/oldboy.html
#web03
mkdir /html/www/ -p
echo "web02 www.oldboy.com" >/html/www/oldboy.html
第二步 web服务器进行环境配置(三台web主机同样配置)
[root@web01 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www/;
index index.html;
}
第三步 在lb01 配置负载均衡反向代理服务
负载均衡模块 -- upstream
反向代理模块 -- proxy_pass
注意,负载均衡上尽量不要存放站点目录
[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;
include /etc/nginx/conf.d/*.conf;
upstream xiaoxi { #upstream 定义可以进行负载均衡的web节点信息,xiaoxi 表示集群名;
server 10.0.0.8:80;
server 10.0.0.9:80;
server 10.0.0.7:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://xiaoxi; #---反向代理将强求发送给指定集群;
}
}
}
7、负载均衡模块详细说明
Syntax: upstream name { ... }
Default: —
Context: http
upstream backend {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
7.1 upstream模块功能参数
7.1.1 weight :可以实现权重轮询分配资源,默认是轮询分配资源(简单说就是按照一定比例访问单节点web)
参照配置:
upstream xiaoxi {
server 10.0.0.8:80 weitht=3;
server 10.0.0.9:80 weitht=1;
server 10.0.0.7:80 weitht=1;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://xiaoxi;
proxy_set_header Host $host;
}
}
7.1.2 least_conn :按照节点 连接数分配资源
参照配置:
upstream xiaoxi {
least_conn;
server 10.0.0.8:80 ;
server 10.0.0.9:80 ;
server 10.0.0.7:80 ;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://xiaoxi;
proxy_set_header Host $host;
}
}
7.1.3 ip_hash: 确保一个用户多次访问, 负载均衡都会分配给相同web节点 了解。缺点:会出现负载不均衡的情况。
参照配置:
upstream xiaoxi {
ip_hash;
server 10.0.0.8:80 ;
server 10.0.0.9:80 ;
server 10.0.0.7:80 ;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://xiaoxi;
proxy_set_header Host $host;
}
}
7.1.4 负载均衡web节点健康状态检查
使用参数:max_fails=3; 最大的失败次数--访问三次后还是无法访问,将此web节点踢出局。
fail_timeout=30s; 失败的超时时间--30s后,访问一次之前访问失败的web节点,如果还是失败则继续等待30s,如果访问成功,则加入正常访问队列。、
参照配置:
upstream xiaoxi {
ip_hash;
server 10.0.0.8:80 max_fails=3 fail_timeout=30s;
server 10.0.0.9:80 max_fails=3 fail_timeout=30s;
server 10.0.0.7:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://xiaoxi;
proxy_set_header Host $host;
}
}
**7.1.5 backup节点备份功能:如果有正常的web节点 ,则不会访问备份节点;如果正常web节点全部都死掉,backup节点自动接受访问请求。
ip_hash: 确保一个用户多次访问, 负载均衡都会分配给相同web节点 了解。缺点:会出现负载不均衡的情况。
参照配置:
upstream xiaoxi {
server 10.0.0.8:80 backup ;
server 10.0.0.9:80 ;
server 10.0.0.7:80 ;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://xiaoxi;
proxy_set_header Host $host;
}
}
8、proxy 反向代理模块详细说明
8.1反向代理指令调用负载均衡集群(upstream)
*proxy_pass http://xiaoxi;
8.2设置请求头信息
proxy_set_header HOST $host;
8.3 客户端访问时,使后端web节点日志可以记录真实访问IP地址
下面两个任选其中一个,作用相同:
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8.4检查网站页面是否正确
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:proxy_next_upstream error timeout;
Context: http, server, location
pro1xy_next_upstream error timeout http_404;
9、反向代理负载均衡企业应用 (1)
第一步: 部署web集群服务
web01站点信息:
[root@web01 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location /upload {
root /html/www/;
index index.html;
}
}
[root@web01 ~]# mkdir /html/www/upload/
[root@web01 ~]#echo "upload page" >/html/www/upload/index.html
web02
[root@web02 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location /static {
root /html/www/;
index index.html;
}
}
[root@web02 ~]# mkdir /html/www/static/
[root@web02 ~]# echo "static page" >/html/www/static/index.html
web03
[root@web03 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location / {
root /html/www/;
index index.html;
}
}
[root@web03 ~]# echo "default page" >/html/www/index.html
第二步修改负载均衡配置文件
}
[root@lb01 /etc/nginx/upstream]# 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;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/upstream/*.com;
}
[root@lb01 /etc/nginx/upstream]# cat www.xiaoxi.com
upstream upload {
server 10.0.0.7:80;
}
upstream static {
server 10.0.0.8:80;
}
upstream default {
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout http_404;
}
location /upload/ {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout http_404;
}
location /static/ {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout http_404;
}
}
[root@lb01 /etc/nginx/upstream]#
第三步访问测试
www.xiaoxi.com
www.xiaoxi.com/static
www.xiaoxi.com/upload
10、反向代理负载均衡企业应用 (浏览器分离)
第一步: 部署web集群服务
web01站点信息:
[root@web01 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location / {
root /html/www/;
index index.html;
}
}
[root@web01 ~]# mkdir /html/www/upload/
[root@web01 ~]#echo "iphone page " >/html/www/index.html
web02
[root@web02 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location / {
root /html/www/;
index index.html;
}
}
[root@web02 ~]# mkdir /html/www/static/
[root@web02 ~]# echo "chrome page" >/html/www/index.html
web03
[root@web03 ~]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.xiaoxi.com;
location / {
root /html/www/;
index index.html;
}
}
[root@web03 ~]# echo "default page" >/html/www/index.html
第二步配置负载均衡
[root@lb01 /etc/nginx/upstream]# 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;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/upstream/*.com;
}
[root@lb01 /etc/nginx/upstream]#
[root@lb01 /etc/nginx/upstream]# cat www.xiaoxi.com
upstream iphone {
server 10.0.0.7:80;
}
upstream chrome {
server 10.0.0.8:80;
}
upstream default {
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.xiaoxi.com;
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout http_404;
if ($http_user_agent ~* iphone) {
proxy_pass http://iphone;
}
if ($http_user_agent ~* Chrome) {
proxy_pass http://chrome;
}
}
}
网友评论