|NO.Z.00066|——————————|LinuxNetwork|——|Linux&Nginx&反向代理.V02|——|目录保护|创建虚拟机|
一、nginx相关实验
### --- nginx状态统计
~~~ #<注意事项>
~~~ 注意配置文件中的结尾有;作为结束~! (切记!)
~~~ 每次实验修改完配置文件后需要重启nginx才有生效
[root@server11 ~]# pkill -HUP nginx // 热重启,热部署,在不重启nginx服务的情况下,让配置文件生效。
### --- 实验1:nginx的状态统计
~~~ 安装nginx时将-with-http_stub_status_module模块开启
~~~ 修改nginx配置文件(写入要访问的server标签中)
[root@server11 ~]# vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
stub_status on;
access_log off;
}
[root@server11 ~]# ln /usr/local/nginx/sbin/* /usr/local/sbin/
[root@server11 ~]# 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
[root@server11 ~]# pkill -HUP nginx
~~~ Active connections:4 // 表示当前的活动连接数;
~~~ server accepts handled requests // 表示已经处理的连接信息
~~~ 三个数字一次表示已处理的连接数,成功的TCP握手次数,已处理的请求数
~~~ 客户端访问网址:http://10.10.10.11/nginx_status
Active connections: 2
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 1
二、目录保护
### --- 实验2:目录保护
~~~ 原理和apache的目录保护原理一样(利用上一个实验接着完成)
~~~ 在状态统计的location中添加:
[root@server11 ~]# vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
stub_status on;
access_log off;
auth_basic "Welcome to nginx_status!";
auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx;
}
~~~ 使用http的命令htpasswd进行用户密码文件的创建(生成在上面指定的位置)
[root@server11 ~]# yum install -y httpd
[root@server11 ~]# htpasswd -c /usr/local/nginx/html/htpasswd.nginx user
New password: 123456
Re-type new password: 123456
~~~ 重启nginx并再次访问统计页面
~~~ #客户端访问网址:http://10.10.10.11/nginx_status,需要客户身份认证账户及密码才可以登入
[root@server11 ~]# nginx -t
[root@server11 ~]# pkill -HUP nginx
三、基于IP并再次访问统计页面
### --- 实验3、基于IP并再次访问统计页面
~~~ 接着上一个实验完成操作
~~~ 在转态统计的location中添加
location /nginx_status {
stub_status on;
access_log off;
auth_basic "Welcome to nginx_status!";
auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx;
allow 10.10.10.240;
deny 10.10.10.0/24;
}
~~~ #仅允许10.10.10.240客户端访问服务器
http://10.10.10.11/nginx_status // 可以正常访问
[root@server11 ~]# elinks 10.10.10.11/nginx_status // 自己本身去访问都会拒绝
### --- 放行再验证
[root@server11 ~]# vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
stub_status on;
access_log off;
auth_basic "Welcome to nginx_status!";
auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx;
#allow 10.10.10.240;
#deny 10.10.10.0/24;
}
[root@server11 ~]# pkill -HUP nginx
[root@server11 ~]# elinks 10.10.10.11/nginx_status // 可以跳转到登录页面
四、创建虚拟机

### --- 实验4:创建虚拟主机
~~~ 提前准备好两个网站的域名,并且规划好两个网站网页存放目录
~~~ 在nginx主配置文件中并列编写两个server标签,并分别写好各自信息
[root@server11 ~]# mkdir /usr/local/nginx/html/blog
[root@server11 ~]# mkdir /usr/local/nginx/html/bbs
[root@server11 ~]# vim /usr/local/nginx/html/blog/index.html
blog.atyanqi.com
[root@server11 ~]# vim /usr/local/nginx/html/bbs/index.html
bbs.atyanqi.com
[root@server11 ~]# vim /etc/hosts
10.10.10.11 blog.atyanqi.com
10.10.10.11 bbs.atyanqi.com
[root@server11 ~]# vim /usr/local/nginx/conf/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #开启日志格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name blog.atyanqi.com;
index index.html index.htm index.php;
root html/blog;
# include enable-php.conf; // include标签,PHP页面解析的时候要匹配文件的标签名,
access_log logs/blog-access.log main; // access_log logs/access.log main;日志格式为main格式
}
server {
listen 80;
server_name bbs.atyanqi.com;
index index.html index.htm index.php;
root html/bbs;
# include enable-php.conf;
access_log logs/bbs-access.log main;
}
[root@server11 ~]# nginx -t
[root@server11 ~]# pkill -HUP nginx
~~~ 分别访问两个不同的域名验证结果
[root@server11 ~]# elinks http://blog.atyanqi.com/
[root@server11 ~]# elinks http://bbs.atyanqi.com/
### --- 日志格式main的问题
[root@server11 ~]# vim /usr/local/nginx/conf/nginx.conf
access_log logs/bbs-access.log main; // main其实是一个标签;可以随便起,abc都可以。。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #与这个遵循一致即可。
# include标签
# include enable-php.conf; // 进行PHP解析的时候要匹配PHP的后缀名,只不过在该文件中,把PHP解析的location的配置文件卸载location外面,要使用它的时候调用即可,不需要写在里面,下面实验把PHP解析写在里面测试ok
server {
listen 80;
server_name blog.atyanqi.com;
index index.html index.htm index.php;
root html/blog;
access_log logs/blog-access.log main;
location ~ \.php$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
}
server {
listen 80;
server_name bbs.atyanqi.com;
index index.html index.htm index.php;
root html/bbs;
access_log logs/bbs-access.log main;
location ~ \.php$ {
root html/bbs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
}
[root@server11 ~]# nginx -t
[root@server11 ~]# pkill -HUP nginx
### --- 添加PHP的解析页面
[root@server11 ~]# vim /usr/local/nginx/html/blog/index.php
<?php
echo "blog";
[root@server11 ~]# vim /usr/local/nginx/html/bbs/index.php
<?php
echo "bbs";
[root@server11 ~]# elinks http://blog.atyanqi.com/index.php
blog
[root@server11 ~]# elinks http://bbs.atyanqi.com/index.php
bbs
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
cdv007-network
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通