一、nginx搭建文件服务器
1、安装nginx
#!/bin/bash
#安装nginx的脚本文件
#先按照nginx的依赖
yum -y install gcc openssl-devel pcre-devel &> /dev/null
echo "nginx依赖gcc,openssl-devel,pcre-devel安装完成"
#安装nginx
cd /root
tar -xf nginx-1.23.3.tar.gz
cd nginx-1.23.3
./configure \
--with-http_ssl_module \
--with-stream \
--with-http_stub_status_module
sleep 1
echo "nginx编译完成"
make && make install
sleep 1
echo "nginx安装完成"
cp /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx -V
nginx
echo "nginx已开启,可以在浏览器端输入本机IP地址验证。"
注意:在安装nginx的过程中,若是没有指定用户,nginx会以nobody这个用户来处理各种请求。nginx启动是以root用户启动的。
root 1426 0.0 0.2 46116 1184 ? Ss 08:28 0:00 nginx: master process nginx
nobody 1427 0.1 0.5 46584 2556 ? S 08:28 0:01 nginx: worker process
注意:实际上干活的是work process进程,master process不干活。nginx是一个多进程的程序。
注意:nginx是按照在名为proxy的主机上的,该主机有两个IP地址。分别是192.168.4.5、192.168.2.5。
2、修改nginx配置文件
- 修改配置文件的时候,先将配置文件做一个备份。
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
- 修改配置文件
17 http {
18 include mime.types;
19 default_type application/octet-stream;
27 sendfile on;
31 keepalive_timeout 65;
35 autoindex on; //要搭建文件系统,这个功能必须要开启
36 autoindex_exact_size on; //同样要搭建文件系统,这个要开启。
37 autoindex_localtime on; //要开启,搭建文件系统。
38 charset utf-8; //为了防止中文乱码,这个要开启。
39 server {
40 listen 80;
41 server_name localhost;
47 location / {
48 root html; //可以将要分享的文件目录,放在html这个目录下。
49 index index.html index.htm;
50 }
56 error_page 500 502 503 504 /50x.html;
location = /50x.html {
58 root html;
59 }
83 }
88 server {
89 listen 8080;
90 server_name localhost;
91
92 location / {
93 if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls|png|jpg|jpeg)$){
94 add_header Content-Disposition "attachment;";
95 } //这是一个逻辑判断,要是以.txt|.pdf|.doc|.xls|.png|.jpg|.jpeg结尾的文件
直接下载,不使用浏览器解析。
96 root /home/static; //要分享的文件目录的根目录,也就是说/home/static这个
目录要共享到文件服务器。
97 index index.html index.htm;
98 }
99 }
100 }
注:本次搭建了两个文件系统,一个在80端口监听,一个在8080端口监听。两个要共享的目录一个在/usr/local/nginx/html下,一个在/home/static下。
35 autoindex on; //要搭建文件系统,这个功能必须要开启
36 autoindex_exact_size on; //同样要搭建文件系统,这个要开启。
37 autoindex_localtime on; //要开启,搭建文件系统。
38 charset utf-8; //为了防止中文乱码,这个要开启。
注:这是http全局配置,该配置写在了http里面,serve外面。所以,对于http服务来说,是全局配置。要想只让这一个web服务是文件服务器服务,可以将该配置写在server里面。
3、客户端验证
![image.png](https://img-blog.csdnimg.cn/img_convert/8d8ccb13164dd9efbe10ae71216b5831.png#averageHue=#fdfbfb&clientId=u64da39d2-d75b-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u9c33b3df&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=78986&status=done&style=none&taskId=u60be1edc-2ac5-4b62-b815-f078fb6eb0c&title=&width=1536.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/2a3f4ac45a41f8de207905421ebb3915.png#averageHue=#fdfbfb&clientId=u64da39d2-d75b-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u17d8f256&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=70121&status=done&style=none&taskId=udddc417a-610d-439d-80a5-35f8d717198&title=&width=1536.8)
注意:防火墙和SELinux要关闭。
二、使用nginx实现web服务用户认证
1、修改nginx配置文件
88 server {
89 listen 8080;
90 server_name localhost;
91 auth_basic "input userAndpassword:"; //认证提示符信息
92 auth_basic_user_file "/usr/local/nginx/pass"; //认证的密码文件
93 location / {
94 if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls|png|jpg|jpeg)$){
95 add_header Content-Disposition "attachment;";
96 }
97 root /home/static;
98 index index.html index.htm;
99 }
100 }
2、生成密码文件
- 首先查看httpd-tools是否安装,一般安装nginx默认会安装。
yum -y install httpd-tools
[root@proxy ~]# htpasswd -c /usr/local/nginx/pass tom //生成密码文件并创建一个Tom用户
New password: //输入要创建的密码
Re-type new password: //确认密码
Adding password for user tom
[root@proxy ~]#
注意:因为在nginx.conf配置文件中,密码文件的存放位置在/usr/local/nginx/目录下,所以生成的密码文件要存放在该位置。这点和htppasswd -c /usr/local/nginx/pass对应上了。
3、重载nginx并在客户端验证
nginx -t //查看nginx配置文件是否错误
[root@proxy ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//这种情况说明nginx配置文件正确
nginx -s reload //nginx重载
![image.png](https://img-blog.csdnimg.cn/img_convert/b101aa89d888415c4f448b0f32eecc19.png#averageHue=#fefcfc&clientId=u4758c9b3-3367-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ua0703553&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=65502&status=done&style=none&taskId=u9ecd2586-624d-4aae-96c5-94ab7765f65&title=&width=1536.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/84b86d074d7221d6146f2d15c22740dc.png#averageHue=#fdfafa&clientId=u4758c9b3-3367-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u2c16c4ac&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=99203&status=done&style=none&taskId=u11f51604-65ec-4a46-b2c6-ccbfdc0dbbd&title=&width=1536.8)
三、基于域名的虚拟主机
- 实现两个基于域名的虚拟主机,域名分别为www.a.com、www.b.com
- www.a.com所提供的服务,需要进行用户认证,用户名tom,密码tom。
1、修改nginx配置
39 server {
40 listen 80;
41 server_name www.b.com;
42
43 #charset koi8-r;
44
45 #access_log logs/host.access.log main;
46
47 location / {
48 root html;
49 index index.html index.htm;
50 }
51
52 error_page 404 /404.html;
53
54 # redirect server error pages to the static page /50x.html
55 #
56 error_page 500 502 503 504 /50x.html;
57 location = /50x.html {
58 root html;
59 }
88 server {
89 listen 80;
90 server_name www.a.com;
91 auth_basic "input userAndpassword:";
92 auth_basic_user_file "/usr/local/nginx/pass";
93 location / {
94 if ($request_filename ~* ^.*?\.(txt|pdf|doc|x ls|png|jpg|jpeg)$){
95 add_header Content-Disposition "attachmen t;";
96 }
97 root /home/static;
98 index index.html index.htm;
99 }
100 }
注意:配置完nginx需要生成密码文件,密码文件的生成同上一个实验,本实验不在赘述。
2、重载nginx,客户端验证
nginx -s reload
- 客户端验证:
- 因为没有搭建DNS服务器,可以通过修改/etc/.hosts文件来实现ip地址解析:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.5 www.a.com www.b.com
![image.png](https://img-blog.csdnimg.cn/img_convert/05043199cad266ec1febf1cd160130c2.png#averageHue=#f8f6f6&clientId=u070372fe-bdf3-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u878fc911&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=82124&status=done&style=none&taskId=ua0cf74ae-a3eb-45f7-b681-2a60d6463a0&title=&width=1536.8)
注意:www.a.com和www.b.com都监听在80端口,但是两个web服务的网站根目录不一样。www.a.com的网站根目录在/home/static/。www.b.com的网站根目录在/usr/local/nginx/html/。如此,便实现了通过输入不同的域名来获得不同的web服务。
![image.png](https://img-blog.csdnimg.cn/img_convert/b478985d7a5bef5c9d519640e9d97b86.png#averageHue=#f8f6f6&clientId=uc5a02e06-0082-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ud7ad8fc5&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=83002&status=done&style=none&taskId=u141aa280-a2f6-4707-8bcb-71c0e00bc28&title=&width=1536.8)
四、SSL加密网站
- 域名为www.c.com
- 该网站通过https访问
- 通过私钥,证书对该站点所有数据加密
1、修改nginx配置文件
- nginx在源码安装的时候,需要安装–with-http_ssl_module模块。在nginx配置文件中需要添加ssl相关的指令来对网站进行加密。
- 生成私钥和证书:(证书用来加密,私钥用来解密)
[root@proxy ~]# cd /usr/local/nginx/conf/
[root@proxy conf]# openssl genrsa > cert.key //生成私钥,因为nginx配置文件中私钥默认放在/usr/local/nginx/conf下,所以,我们生成的私钥也放在这里。
Generating RSA private key, 2048 bit long modulus
..+++
...................+++
e is 65537 (0x10001)
[root@proxy conf]# openssl req -new -x509 -key cert.key > cert.pem //基于私钥生成证书,生成证书的过程中会让你回答一些问题,这些问题可以随便回答。
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HE^HN
Locality Name (eg, city) [Default City]:ZK
Organization Name (eg, company) [Default Company Ltd]:ZK
Organizational Unit Name (eg, section) []:ZK
Common Name (eg, your name or your server's hostname) []:HFSA^H
Email Address []:123456@163.COM^H^H
[root@proxy conf]#
通过以上命令便在/usr/local/nginx/conf下生成了私钥(cert.key)和证书(cert.pem)。
![image.png](https://img-blog.csdnimg.cn/img_convert/7adba04c0ce5b01ab4940575d6a11b4d.png#averageHue=#fcfbfb&clientId=u836b823c-49dc-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=648&id=u176e0952&margin=[object Object]&name=image.png&originHeight=810&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=26561&status=done&style=none&taskId=u32cb167e-7a0f-45f8-b7be-7ee38d9ad24&title=&width=1536.8)
105 server {
106 listen 443 ssl; //加密后的网站在443端口进行监听
107 server_name www.c.com;
108
109 ssl_certificate cert.pem;
110 ssl_certificate_key cert.key;
111
112 ssl_session_cache shared:SSL:1m;
113 ssl_session_timeout 5m;
114
115 ssl_ciphers HIGH:!aNULL:!MD5;
116 ssl_prefer_server_ciphers on;
117
118 location / {
119 root /home/www; //网站根目录变为了/home/www/,在浏览器输入https://www.c.com会将/home/www/目录下的index.html文件传送到浏览器显示
120 index index.html index.htm;
121 }
122 }
2、重载nginx并在客户端验证
重载nginx:
nginx -t
nginx -s reload
客户端验证:
![image.png](https://img-blog.csdnimg.cn/img_convert/9aa7a366ad7646691c3c5a84adae075c.png#averageHue=#919c9b&clientId=uf76d16df-350f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u13477ee8&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=121359&status=done&style=none&taskId=ua7262661-7c84-4b9e-9276-bedb8dbe4fd&title=&width=1536.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/0d6af365f433ce913276bbd617b4f549.png#averageHue=#f9f7f6&clientId=uf76d16df-350f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ufe81e5d8&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=92483&status=done&style=none&taskId=u0566e3bf-16da-4a43-bf90-6c692d98f61&title=&width=1536.8)
五、部署LNMP服务
LNMP实际上就是Linux、nginx、MySQL、PHP的简称。通过这四个服务,可以将一个完整的PHPweb项目上线。一个完整的PHPweb项目是需要动静分离的,即动态资源和静态资源分开,而LNMP环境可以实现动静分离。
LNMP实现效果:
- 如果用户访问的是静态资源,则到html目录寻找,直接返回。
- 如果用户访问的是动态脚本,则在html目录去找并交给PHP处理。
注:nginx监听在80端口,PHP监听在9000端口。
1、安装nginx、MySQL、PHP
yum -y install mariadb mariadb-server mariadb-devel
- mariadb:数据库客户端软件
- mariadb-server:数据库服务器软件
- mariadb-devel:其它客户端软件的依赖包
- 安装PHP
yum -y install php php-fpm php-mysql
- php:解释器
- php-fpm:PHP进程管理器服务
- php-mysql:PHP的数据库扩展包
2、开启nginx、MySQL、PHP
[root@proxy ~]# systemctl start mariadb //开启mariadb
[root@proxy ~]# systemctl enable mariadb //将mriadb设置为开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@proxy ~]# systemctl start php-fpm //开启php
[root@proxy ~]# systemctl enable php-fpm //将php设置为开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@proxy ~]# systemctl status mariadb //查看mariadb运行状态
[root@proxy ~]# systemctl status php-fpm //查看php运行状态
[root@proxy ~]# nginx //开启nginx
3、修改nginx配置文件
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
4、重载nginx并进行客户端验证
PHP测试文件:
<?php
$i="This is a test Page";
echo $i;
?>
测试结果如下:
![image.png](https://img-blog.csdnimg.cn/img_convert/acec595160960a1700f374b81c87c698.png#averageHue=#fefdfd&clientId=u39ad9763-9da3-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ucea45149&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=64894&status=done&style=none&taskId=u9abd6863-6c22-41fb-bfa8-b9efbfad7cf&title=&width=1536.8)
说明在获得test.php脚本的时候,在/usr/local/nginx/html中找到该文件,并将该文件交给php处理,并将处理结果返回到浏览器。否则,没有交给PHP处理的话,会得到PHP代码,但是,用户只想要PHP代码的处理结果。
PHP和MySQL结合:
<?php
$mysqli = new mysqli('localhost','root','','mysql'); //localhost为连接本地的MySQL数据库,root:表示用root用户连接,''':表示密码为空,MySQL:表示连接好数据库,进入名为mysql的数据库中。
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>
测试结果如下:
![image.png](https://img-blog.csdnimg.cn/img_convert/ff927911637f7e451dd044e1fc06277f.png#averageHue=#fefcfc&clientId=u8b76a2a7-7696-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ue706e407&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=76772&status=done&style=none&taskId=u2eab2c89-465a-4dba-bf46-001df657101&title=&width=1536.8)
PHP和mysql成功连接,并获得了MySQL中MySQL数据库中的user表中的数据。
添加MySQL用户,查看浏览器端是否动态变化
命令行输入mysql,进入mysql中。然后输入下行代码添加一个lisa用户密码为123
grant all on *.* to 'lisa'@'localhost' identified by '123';
grant all on *.* to 'hali'@'localhost' identified by '789'; //添加一个hali用户密码为789
测试结果:
![image.png](https://img-blog.csdnimg.cn/img_convert/548dd0f65a022345bf371335aaf66e3a.png#averageHue=#fefcfc&clientId=u66ea69b5-c5f4-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u78f4be36&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=88301&status=done&style=none&taskId=ue10aea6f-5673-4db7-90c3-551e933638a&title=&width=1536.8)
LNMP环境部署脚本
#!/bin/bash
#安装nginx
path=/usr/local/nginx/conf
tar -xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure
make && make install
cp /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx -V
echo "nginx安装成功"
#安装mariadb mariadb-server mariadb-devel
#安装php php-fpm php-myql
yum -y install mariadb mariadb-server mariadb-devel
yum -y install php php-fpm php-myql
#修改nginx的配置文件,实现动静分离
sed -i '65,71s/#//' $path/nginx.conf
sed -i '/SCRIPT_FILENAM/d' $path/nginx.conf
sed -i 's/fastcgi_params/fastcgi.conf/' $path/nginx.conf
systemctl start mariadb
systemctl enable mariadb
systemctl start php-fpm
systemctl enable php-fpm
nginx
六、地址重写
- 所有访问a.html的请求,重定向到b.html
- 所有访问192.168.4.5的请求重定向到www.tmooc.cn
- 所有访问192.168.4.5/下面的子页面,重定向到www.tmooc.cn/下相同的页面
- 实现firefox与curl访问相同页面文件,返回不同的内容
解决方案
- nginx服务器的地址重写,主要用到的配置参数是rewrite:
- rewrite 旧地址 新地址 [选项]
6.1
35 server {
36 listen 80;
37 server_name localhost;
38 rewrite /a.html /b.html; //要实现地址重定向只需要添加该行代码即可
39
40 #charset koi8-r;
41
42 #access_log logs/host.access.log main;
43
44 location / {
45 root html;
46 index index.html index.htm;
47 }
}
注:/a.html中的/,指网站根目录。本次实验使用的是默认的web服务,所以网站根目录为/usr/local/nginx/html/。
客户端验证
![image.png](https://img-blog.csdnimg.cn/img_convert/b1e222c3e855f1da049fecb029b51399.png#averageHue=#fcfafa&clientId=uf37e8b5e-d1ff-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ua24eb978&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=74492&status=done&style=none&taskId=u7e6e0e10-22a6-44e4-b449-f798bbb2d2e&title=&width=1536.8)
可以看到地址栏仍是192.168.4.5/a.html,要想地址栏变为192.168.4.5/b.html需要在nginx.conf中加上如下代码:
rewrite /a.html /b.html redirect;
6.2
35 server {
36 listen 80;
37 server_name localhost;
38 rewrite ^/ http://www.tmooc.cn/; //主要是这行代码起作用
39 #charset koi8-r;
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
}
要想输入一个网址跳转到其它网址,将localhost修改为网址即可。例如将localhost修改为https://www.hfs.com便会跳转到http://www.tmooc.cn/。
实验简单,不在做验证
6.3
35 server {
36 listen 80;
37 server_name localhost;
38 rewrite ^/(.*) http://www.tmooc.cn/$1$2;
39 #charset koi8-r;
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
}
- nginx中$1就相当于正则表达式中的\1,意思是将()中的内容复制到http://www.tmooc.cn/$1处,因为()中是(.*)两个字符。所以,是$1和$2。
- ()在正则表达式中是保留的意思,可以通过\1、\2来复制()中保留的字符。
客户端验证
![image.png](https://img-blog.csdnimg.cn/img_convert/8ec8ee1cd878d1a651945fd9ffec4587.png#averageHue=#86bcb4&clientId=uf37e8b5e-d1ff-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u1b1a8cbe&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=825114&status=done&style=none&taskId=ucf01d64e-4704-4ccc-962c-78f03e58f74&title=&width=1536.8)
浏览器地址栏输入,192.168.4.5/free
,跳转到https://www.tmooc.cn/free
6.4
本次实验的实验效果是,curl和火狐浏览器访问相同链接返回的页面不同。这种方式的应用场景适合不同客户端给予不同的访问结果。例如:手机访问www.sina.com是窄屏的,电脑访问www.sina.com是宽屏的。
补充:nginx中access.log日志中各字段的含义
192.168.4.1 - - [27/Dec/2022:15:25:03 +0800] "GET /vip HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr |
- |
$remote_user |
[$time_local] |
$request |
$status |
$body_bytes_sent |
$http_referer |
$http_user_agent |
$http_x_forwarded_for |
ip地址的来源 |
日志文件中就是一个"-" |
|
|
|
|
|
|
|
|
访问的时间 |
包括访问的类型,是get请求还是post请求,访问的协议是什么协议。 |
访问状态码,如果访问成功就是200,如果没有找到要访问的资源,就是404。 |
资源的大小,即有多少个字节,返回给访问者。 |
访问来源,即如果是通过百度搜索一个网站,点击而来。这里显示百度。如果是通过输入域名直接访问,这里什么也没有。 |
包括访问者用的什么系统,用的什么版本的浏览器。 |
|
|
|
|
192.168.4.1 |
- |
- |
[27/Dec/2022:15:25:03 +0800] |
“GET /vip HTTP/1.1” |
302 |
161 |
“-” |
“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36” |
|
创建测试页面
echo "i am normal page" > /usr/local/nginx/html/test.html
mkdir -p /usr/local/nginx/html/firefox/
echo "firefox page" > /usr/local/nginx/html/firefox/test.html
修改nginx配置文件
35 server {
36 listen 80;
37 server_name localhost;
38 #charset koi8-r;
39
40 #access_log logs/host.access.log main;
41 if ($http_user_agent ~* firefox){
42 rewrite ^/(.*) /firefox/$1$2;
43 }
44 location / {
45 root html;
46 index index.html index.htm;
47 }
}
41 if ($http_user_agent ~* firefox){
42 rewrite ^/(.*) /firefox/$1$2;
43 }
通过上面的日志文件,我们可以知道,$http_user_agent列会显示用户使用的什么浏览器来进行访问的。
- ~:进行正则匹配,正则匹配包含即可。
- *:不区分大小写
41到43行代码的含义是,如果地址栏输入192.168.4.5/test.html
之后,如果在日志文件中显示是Firefox浏览器,就地址重定向到/usr/local/nginx/firefox/下去找test.html。
客户端验证
![image.png](https://img-blog.csdnimg.cn/img_convert/b46b2f5388f500d6dded9c03788b1848.png#averageHue=#fcfbfb&clientId=uf37e8b5e-d1ff-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u03949b03&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=60730&status=done&style=none&taskId=u36081205-93d1-4ad0-b518-57683de50df&title=&width=1536.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/6590c3c72c8eb8639844090d5ef381e9.png#averageHue=#f9f8f7&clientId=uf37e8b5e-d1ff-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=uae9803be&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=77439&status=done&style=none&taskId=u5a914be1-1410-40c3-bac3-ad61cbdf65e&title=&width=1536.8)
地址重写总结
- rewrite 旧地址 新地址 [选项];
- last 不再读其他rewrite
- break 不再读其他语句,结束请求
- redirect 临时重定向
- permanent 永久重定向
七、nginx反向代理(nginx做代理服务器)
- 在做服务器集群的时候,可以使用nginx做代理服务器。或者说称之为调度器。
- nginx做代理服务器,部署不同的服务器集群,需要安装不同的模块,这些模块需要在nginx源码编译的时候,进行添加。
1、实验拓扑图
2、web1和web2构建web服务
yum -y install httpd
echo "192.168.2.100" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
firewall-cmd --set-default-zone=trusted
setenforce 0
注:web1和web2部署相同的服务。只有部署相同的服务才能做服务器集群,但是实验过程中为了方便验证,可以让index.html不同。这样方便客户端验证。
3、修改nginx配置文件
upstream webserver {
server 192.168.2.100:80; //这些是服务器集群的机器的IP地址
server 192.168.2.200:80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://webserver; //表示将请求转发到webserver这个服务器集群处理
root html;
index index.html index.htm;
}
}
注意:upstream是写在server外面,http里面的。因为nginx是做http服务调度器。所以,要写在http里面。
4、nginx重载并在客户端验证
nginx重载不在赘述,前面实验修改完配置文件都需要重载。已经练习很多次了。
客户端验证:
![image.png](https://img-blog.csdnimg.cn/img_convert/f94d8118197fb7c43f52b35cef2cb795.png#averageHue=#2a3b48&clientId=ud550ed7f-079a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=ua6133797&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1527741&status=done&style=none&taskId=u4fe6350f-887c-481d-9637-23af4fe36e6&title=&width=1536.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/8506203a4a21ce6861d545bf66f522dd.png#averageHue=#f5f4f4&clientId=ud550ed7f-079a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=832&id=u78438e75&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1921&originalType=binary&ratio=1&rotation=0&showTitle=false&size=392706&status=done&style=none&taskId=ub656116d-796d-472e-afc9-6f0c80780ca&title=&width=1536.8)
可以看到nginx将请求转发给了webserver那个服务器集群处理了,为了实验方便我们在/var/www/html下放置了不同的index.html文件。
注:nginx默认使用的是轮询算法,即请求交替转发到集群中的每一台机器上。
八、nginx做TCP/UDP调度器
- nginx做TCP/UDP调度器,编译时需要使用–with-stream,开启nginx_stream_core_module模块。
- nginx如果开启了该模块,就可以调度一切服务,比如:做ssh服务器集群,做MySQL集群等等。
本次实验是做ssh服务器集群,而ssh功能是默认开启的。所以,web1和web2可以先不用管。主要是对proxy做配置即可。
1、修改nginx配置文件
12 events {
13 worker_connections 1024;
14 }
15 stream {
16 upstream backend {
17 server 192.168.2.100:22;
18 server 192.168.2.200:22;
19 }
20 server {
21 listen 12345; //proxy服务器在12345端口提供ssh集群服务。本来应该是在22端口的,但是proxy本身又有ssh服务,该服务在22端口提供。所以,需要定义一个非22端口来提供ssh集群。
22 proxy_pass backend; //将请求转发到backend这个ssh服务集群
23 }
24 }
25
26 http {
27 include mime.types;
28 default_type application/octet-stream;
因为ssh服务不属于http服务,所以写在http外面。
2、客户端验证
![image.png](https://img-blog.csdnimg.cn/img_convert/91baa5d4388c681b9c02c75aa472b35b.png#averageHue=#242121&clientId=ud550ed7f-079a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=518&id=u32a68a8c&margin=[object Object]&name=image.png&originHeight=648&originWidth=1240&originalType=binary&ratio=1&rotation=0&showTitle=false&size=61197&status=done&style=none&taskId=u1c5c2644-4a2c-49d3-ab44-2b561c75977&title=&width=992)
![image.png](https://img-blog.csdnimg.cn/img_convert/62749ec1a391e56d1c9a79a90ee3819a.png#averageHue=#242121&clientId=ud550ed7f-079a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=518&id=u58c58814&margin=[object Object]&name=image.png&originHeight=648&originWidth=1240&originalType=binary&ratio=1&rotation=0&showTitle=false&size=72492&status=done&style=none&taskId=u84d36459-5c7f-4ebb-ba0a-bd4bea88c27&title=&width=992)