在線班課程-第11周 LNMP服務架構搭建、nginx負載均衡、keepalived高可用

在線班課程-第10周
6.9 Nginx Location
location(位置)指令的做用是能夠根據用戶請求的URI來執行不一樣的應用,其實就是根據用戶的請求的網站的網址URL匹配,匹配成功即進行相關的操做。
location = / { ##www.etiantian.org www.etiantian.org/ www.etiantian.org/index.html
[ configuration A ]
}
location / { ##默認狀況,無路可走 jd.com/hello/oldboy.html
[ configuration B ]
}
location /documents/ { ##uri裏面有/documents oldboyedu.com/documents/dsfsadfasdf.jpg
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~ .(gif|jpg|jpeg)$ { ##~ 不區分大小寫的正則匹配,以.gif或.jpg或.jpeg結尾
[ configuration E ]
}php

[root@www extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}html

location /documents/ {
        return 403;
    }
    location ^~ /images/ {
        return 404;

}mysql

location ~* \.(gif|jpg|jpeg)$ {
        return 500;
    }
    access_log logs/access_www.log main ;
}

/application/nginx/sbin/nginx -t #檢查語法

nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successfullinux

/application/nginx/sbin/nginx #記得啓動nginx進程

/application/nginx/sbin/nginx -s reload #從新加載nginx配置

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null 10.0.0.8
402nginx

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/
402
[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8
402
[root@web01 extra]#web

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/oldboy
401
[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/index.html
401面試

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/documents/
403sql

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/documents/1.jpg
500數據庫

[root@web01 extra]# curl -sw "%{http_code}\n" -o /dev/null http://10.0.0.8/images/1.jpg
404vim

不用URI及特殊字符組合匹配順序 匹配說明
第一名:「location = / {」 精確匹配/
第二名:「location ^~ /images/ {」 匹配常規字符串,不作正則匹配檢查, 優先匹配路徑
第三名:「location ~* .(gif|jpg|jpeg)$ {」 正則匹配
第四名:「location /documents/ {」 匹配常規字符串,若是有正則則優先匹配正則。
第五名:「location / {」 全部location都不能匹配後的默認匹配。

Nginx Rewrite
和Apace等web服務軟件同樣,Nginx Rewrite的主要功能也是實現URL地址重寫

etiantian.org ====>www.etiantian.org
etiantian.org 變換爲 www.etiantian.org

vim www.conf

server {
    listen       80;
    server_name  etiantian.org;
    rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}

etiantian.org/index.html ==== www.etiantian.org/index.html

cat www.conf

server {
    listen 80;
    server_name etiantian.org;
    rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
server {
    listen       80;
    server_name  www.etiantian.org ;

access_log logs/access_www.log main;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web01 extra]# curl -L etiantian.org/index.html
web01 www.etiantian.org

[root@web01 extra]# curl -Lv etiantian.org/index.html #顯示訪問過程

  • About to connect() to etiantian.org port 80 (#0)
  • Trying 10.0.0.8... connected
  • Connected to etiantian.org (10.0.0.8) port 80 (#0)

    GET /index.html HTTP/1.1
    User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
    Host: etiantian.org
    Accept: /

    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.12.2
    < Date: Tue, 27 Feb 2018 12:31:27 GMT
    < Content-Type: text/html
    < Content-Length: 185
    < Connection: keep-alive
    < Location: http://www.etiantian.org//index.html
    <

  • Ignoring the response-body
  • Connection #0 to host etiantian.org left intact
  • Issue another request to this URL: 'http://www.etiantian.org//index.html'
  • About to connect() to www.etiantian.org port 80 (#1)
  • Trying 10.0.0.8... connected
  • Connected to www.etiantian.org (10.0.0.8) port 80 (#1)

    GET //index.html HTTP/1.1
    User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
    Host: www.etiantian.org
    Accept: /

    < HTTP/1.1 200 OK
    < Server: nginx/1.12.2
    < Date: Tue, 27 Feb 2018 12:31:27 GMT
    < Content-Type: text/html
    < Content-Length: 24
    < Last-Modified: Mon, 12 Feb 2018 18:11:30 GMT
    < Connection: keep-alive
    < ETag: "5a81d8d2-18"
    < Accept-Ranges: bytes
    <
    web01 www.etiantian.org

  • Connection #1 to host www.etiantian.org left intact
  • Closing connection #0
  • Closing connection #

Http狀態碼301和302概念簡單區別及企業應用案例
http://oldboy.blog.51cto.com/2561410/1774260
內容:Http狀態碼301和302的區別及企業應用案例

一、什麼是301重定向?
  301重定向/跳轉通常,表示本網頁永久性轉移到另外一個地址。
301是永久性轉移(Permanently Moved),SEO經常使用的招式,會把舊頁面的PR等信息轉移到新頁面;

二、什麼是302重定向?
  302重定向表示臨時性轉移(Temporarily Moved ),當一個網頁URL須要短時間變化時使用。

三、301重定向與302重定向的區別
301重定向是永久的重定向,搜索引擎在抓取新內容的同時也將舊的網址替換爲重定向以後的網址。
302重定向是臨時的重定向,搜索引擎會抓取新的內容而保留舊的網址。由於服務器返回302代碼,搜索引擎認爲新的網址只是暫時的。

四、常見網站的應用案例
12345678910111213141516 [root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org200[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://etiantian.org    200[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://baidu.com200[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://taobao.com302[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://qq.com    302[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://jd.com302[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://51cto.com301[root@oldboy ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://sina.com.cn301
更多http狀態碼知識 請瀏覽http://oldboy.blog.51cto.com/2561410/716294

[root@web01 extra]# curl -I jd.com baidu.com taobao.com
HTTP/1.1 302 Moved Temporarily
Server: JengineD/1.7.2.1
Date: Tue, 27 Feb 2018 12:37:26 GMT
Content-Type: text/html
Content-Length: 165
Location: http://www.jd.com
Connection: keep-alive

HTTP/1.1 200 OK
Date: Tue, 27 Feb 2018 12:37:27 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Wed, 28 Feb 2018 12:37:27 GMT
Connection: Keep-Alive
Content-Type: text/html

HTTP/1.1 302 Found
Server: Tengine
Date: Tue, 27 Feb 2018 12:37:27 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
Location: http://www.taobao.com/

[root@web01 extra]# curl status.etiantian.org
Active connections: 1
server accepts handled requests
84 84 135
Reading: 0 Writing: 1 Waiting: 0

##status.conf
server {
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
auth_basic "closed site";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
}

[root@www extra]# yum install httpd-tools -y
[root@www extra]# which htpasswd
/usr/bin/htpasswd
htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
chmod 400 /application/nginx/conf/htpasswd
chown www /application/nginx/conf/htpasswd

[root@web01 extra]# rpm -qa httpd-tools
httpd-tools-2.2.15-60.el6.centos.6.x86_64
[root@web01 extra]# rpm -ql httpd-tools
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd
/usr/bin/logresolve
/usr/share/doc/httpd-tools-2.2.15
/usr/share/doc/httpd-tools-2.2.15/LICENSE
/usr/share/man/man1/ab.1.gz
/usr/share/man/man1/htdbm.1.gz
/usr/share/man/man1/htdigest.1.gz
/usr/share/man/man1/htpasswd.1.gz
/usr/share/man/man1/logresolve.1.gz

q Apache select和Nginx epoll模型區別形象比喻(面試常考);
q 虛擬主機概念及類型分類詳解;
q Nginx錯誤及訪問日誌及訪問日誌切割;
q Nginx location介紹及配置實踐;
q Nginx Rewrite介紹及配置實踐;
q Nginx Web訪問認證介紹及配置實踐。

https://downloads.mysql.com/archives/community/

#老男孩教育-火爆MySQL二進制部署現場
#二進制安裝MySQL-5.6.39

###1.添加用戶
useradd -s /sbin/nologin -M mysql

####2.解壓 mysql 二進制包
cd /home/oldboy/tools
tar xf mysql-5.6.39-*-x86_64.tar.gz

####3.把MySQL 移動到 /application/
mkdir -p /application/
mv /home/oldboy/tools/mysql-5.6.39-*-x86_64 /application/mysql-5.6.39

####4.建立軟鏈接
ln -s /application/mysql-5.6.39/ /application/mysql

####5.讓MySQL用戶管理 /application/mysql
chown -R mysql.mysql /application/mysql/data

####6.安裝這個軟件 初始化數據庫
#1.軟件安裝在哪裏
#2.數據存放在哪裏
#3.MySQL使用的用戶誰?

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

#####To start mysqld at boot time you have to copy
#####support-files/mysql.server to the right place for your system
#####mysql啓動腳本 默認放在support-files/mysql.server
#####
#####記得給MySQL設置個密碼
#####PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
#####To do so, start the server, then issue the following commands:
#####

/application/mysql/bin/mysqladmin -u root password 'new-password'
/application/mysql/bin/mysqladmin -u root -h web01 password 'new-password'

####7.複製啓動腳本 受權
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

####8.修改啓動腳本 和 mysql命令 中的路徑
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

####9.複製 默認的配置文件
\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf
/etc/init.d/mysqld start

###故障
##1./tmp權限
##2.主機名解析 hosts解析 #ping 主機名
##3.一步一步執行

##
##/application/mysql/bin/mysql
##Welcome to the MySQL monitor. Commands end with ; or \g.
##Your MySQL connection id is 1
##Server version: 5.5.49 MySQL Community Server (GPL)
##
##Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
##
##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>

####10.PATH路徑
echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
source /etc/profile
which mysql

####11.加入開機自啓動
chkconfig --add mysqld
chkconfig mysqld on

####12.給MySQL root用戶設置密碼
/application/mysql/bin/mysqladmin -u root password 'oldboy123'

####13.從新登陸MySQL數據庫
mysql -uroot -poldboy123

####14.數據庫基礎框架
#1.數據庫 test mysql
#2.表格

#mysql SQL語句
#查看系統中全部數據庫
#show databases;
#查看系統中全部的用戶
#使用某一個數據庫

mysql> #查看當前都有啥
mysql> show databases; ****
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.07 sec)

####初級 查看系列-開始
##使用某一個數據庫
###至關於進入 mysql 數據庫中 cd mysql ; cd test
#use mysql

##我想查看當前在哪? pwd 當前正在使用哪一個數據庫
select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

##我是誰? 查看當前用戶
select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

###當前系統都有什麼用戶? 他們能夠在哪裏登陸? *****
select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web01 |
| root | web01 |
+------+-----------+
6 rows in set (0.02 sec)
####初級 查看系列-結束
###show databases;
###select user,host from mysql.user;

####初級 添加刪除系列
#建立數據庫
create database wordpress;
#刪除數據庫
drop database wordpress;

#添加用戶
grant all on wordpress.* to 'wordpress'@'172.16.1.0/255.255.255.0' identified by '123456';

grant all on wordpress.* to 'wordpress'@'172.16.1.0/255.255.255.0' identified by '123456';
受權全部的權限, wordpress數據庫全部的權限 給 wordpress用戶 能夠在172.16.1.0/255.255.255.0 網段登陸數據庫 這個用戶的密碼123456;

#更新系統的權限表
flush privileges;

###進行測試
mysql -uwordpress -p123456

mysql -uwordpress -p -h 172.16.1.8

#刪除用戶
drop user wordpress@'172.16.1.8';

###1.查看都有什麼數據庫
###2.查看都有什麼用戶
###3.添加用戶

#help sql語句。

#跳過受權表(不用密碼登陸)
#/etc/init.d/mysqld restart --skip-grant-table

#mysql 命令行
#-u 指定用戶
#-p 指定密碼(不要有空格)
#-h 鏈接到某一臺服務器

#更改密碼 mysqladmin -uroot -poldboy123 password '新的密碼'
#

db01上部署一個mysql5.6.39

#部署php
#解壓PHP軟件,進行編譯安裝,將程序安裝到/application目錄中,而且建立軟連接
yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel curl-devel -y
yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y
rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel curl-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel

#安裝其它相關程序---libmcrypt
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install libmcrypt-devel mhash mcrypt
rpm -qa libmcrypt-devel mhash mcrypt
http://php.net/releases/

#安裝PHP軟件程序
tar xf php-5.5.32.tar.gz
cd php-5.5.32 #----正式編譯前也能夠把這個軟件安裝上(libxslt*)
./configure --prefix=/application/php-5.5.32 \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no

##提示 以下內容 即成功
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

ln -s /application/mysql/lib/libmysqlclient.so.18  /usr/lib64/  #能夠不建立

touch ext/phar/phar.phar
make && make install
ln -s /application/php-5.5.32/ /application/php

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
invertedregexiterator.inc
directorygraphiterator.inc
directorytreeiterator.inc
phar.inc

Build complete.

[root@web01 php-5.5.32]# make install
Installing PHP CLI binary: /application/php-5.5.32/bin/
Installing PHP CLI man page: /application/php-5.5.32/php/man/man1/
Installing PHP FPM binary: /application/php-5.5.32/sbin/
Installing PHP FPM config: /application/php-5.5.32/etc/
Installing PHP FPM man page: /application/php-5.5.32/php/man/man8/
Installing PHP FPM status page: /application/php-5.5.32/php/php/fpm/
Installing PHP CGI binary: /application/php-5.5.32/bin/
Installing PHP CGI man page: /application/php-5.5.32/php/man/man1/
Installing build environment: /application/php-5.5.32/lib/php/build/
Installing header files: /application/php-5.5.32/include/php/
Installing helper programs: /application/php-5.5.32/bin/
program: phpize
program: php-config
Installing man pages: /application/php-5.5.32/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /application/php-5.5.32/lib/php/
[PEAR] Archive_Tar - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.3.0
[PEAR] PEAR - installed: 1.10.1
Wrote PEAR system config file at: /application/php-5.5.32/etc/pear.conf
You may want to add: /application/php-5.5.32/lib/php to your php.ini include_path
/home/oldboy/tools/php-5.5.32/build/shtool install -c ext/phar/phar.phar /application/php-5.5.32/bin
ln -s -f phar.phar /application/php-5.5.32/bin/phar
Installing PDO headers: /application/php-5.5.32/include/php/ext/pdo/
[root@web01 php-5.5.32]# echo $?
0

#複製php.ini配置文件

[root@web01 php-5.5.32]# cp /home/oldboy/tools/php-5.5.32/php.ini-production /application/php-5.5.32/lib/php.ini

#複製php-fpm配置文件
[root@web01 php-5.5.32]# cd /application/php-5.5.32/etc/
[root@web01 etc]# ls
pear.conf php-fpm.conf.default
[root@web01 etc]# cp php-fpm.conf.default php-fpm.conf
[root@web01 etc]# ll
total 52
-rw-r--r-- 1 root root 1332 Feb 27 22:53 pear.conf
-rw-r--r-- 1 root root 22609 Feb 27 22:56 php-fpm.conf
-rw-r--r-- 1 root root 22609 Feb 27 22:53 php-fpm.conf.default

#
[root@web01 etc]# /application/php-5.5.32/sbin/php-fpm -t
[27-Feb-2018 22:56:53] NOTICE: configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful

[root@web01 etc]# /application/php-5.5.32/sbin/php-fpm
[root@web01 etc]# ss -lntup |grep 9000
tcp LISTEN 0 16384 127.0.0.1:9000 : users:(("php-fpm",129733,7),("php-fpm",129734,0),("php-fpm",129735,0))

LNMP搭建網站前的測試。
測試nginx與php配合是否成功
php與MySQL配合是否成功
部署網站

測試nginx與php配合是否成功

server {
listen 80;
server_name blog.etiantian.org;
root html/blog;
index index.php index.html index.htm;
location ~ .*.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}

echo '<?php phpinfo();?>' >/application/nginx/html/blog/test_info.php

php與MySQL配合是否成功

test_mysql.php

<?php
//$link_id=mysql_connect('主機名','用戶','密碼');
$link_id=mysql_connect('172.16.1.51','wordpress','123456') or mysql_error();
if($link_id){
echo "mysql successful by oldboy ! \n";
}else{
echo mysql_error();
}
?>

#部署博客

https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

chown -R www.www *

負載均衡與反向代理

HOSTNAME IP 說明
lb01 10.0.0.5 Nginx主負載均衡器
lb02 10.0.0.6 Nginx輔負載均衡器
web01 10.0.0.8 web01服務器
web02 10.0.0.7 web02服務器

[root@web01 ~]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module

#web01 web02 nginx.confworker_processes 1;

相關文章
相關標籤/搜索