Nginx下构建PC站点

Nginx下构建PC站点

总结整个实验

  • 先给 Nginx 配置 "规则文件夹"(conf.d),让它能加载我们写的网站规则;

  • 用root做 "路径拼接":网址路径拼上 root

  • 路径,找到对应文件(适合常规网站目录);

  • 用alias做 "路径替换":网址路径直接换成 alias 的路径(适合网址路径和实际文件路径不一样的场景);

  • 每改一次规则,都要重启 Nginx 让规则生效;

  • 用curl命令模拟访问,验证规则是否正确。

整个实验的目的就是让你明白:Nginx 的root和alias是怎么把 "用户输入的网址" 和 "服务器里的文件" 关联起来的,这是搭建网站最基础也最核心的一步

1.location中的root

复制代码
[root@Nginx conf]# cd /usr/local/nginx/conf/
[root@Nginx conf]# mkdir  conf.d
[root@Nginx conf]# vim nginx.conf
82     include "/usr/local/nginx/conf/conf.d/*.conf";

[root@Nginx conf]# nginx -s reload
[root@Nginx conf]# cd conf.d/

[root@Nginx ~]# mkdir  -p /webdata/nginx/timinglee.org/lee/html
[root@Nginx ~]# echo lee.timinglee.org > /webdata/nginx/timinglee.org/lee/html/index.html

[root@Nginx conf.d]# vim vhosts.conf
server {
    listen 80;
    server_name lee.timinglee.org;
    location / {
        root /webdata/nginx/timinglee.org/lee/html;
    }
}

root@Nginx conf.d]# systemctl restart nginx.service

#测试
[root@Nginx conf.d]# vim /etc/hosts
172.25.254.100     Nginx www.timinglee.org lee.timinglee.org

[root@Nginx conf.d]# curl  www.timinglee.org
timinglee
[root@Nginx conf.d]# curl  lee.timinglee.org
lee.timinglee.org



#local示例需要访问lee.timinglee.org/lee/目录
[root@Nginx conf.d]# vim vhosts.conf
server {
    listen 80;
    server_name lee.timinglee.org;
    location / {
        root /webdata/nginx/timinglee.org/lee/html;
    }
    location /lee {			#lee标识location中的root值+location 后面指定的值代表目录的路径
        root /webdata/nginx/timinglee.org/lee/html;
    }
    
}

[root@Nginx conf.d]# systemctl restart nginx.service
[root@Nginx conf.d]# mkdir  -p /webdata/nginx/timinglee.org/lee/html/lee
[root@Nginx conf.d]# echo lee > /webdata/nginx/timinglee.org/lee/html/lee/index.html
[root@Nginx conf.d]# curl  lee.timinglee.org/lee/
lee

2.location中的alias

复制代码
[root@Nginx conf.d]# vim vhosts.conf
server {
    listen 80;
    server_name lee.timinglee.org;

    location /passwd {				#标识文件		
        alias /etc/passwd;
    }


    location /passwd/ {				#表示目录
        alias /mnt/;
    }

}

[root@Nginx conf.d]# nginx -s reload
[root@Nginx conf.d]# echo passwd > /mnt/index.html

#测试
[root@Nginx conf.d]# curl  lee.timinglee.org/passwd/
passwd
[root@Nginx conf.d]# curl  lee.timinglee.org/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

问题:curl www.timinglee.org 为什么会被这个 server 处理?

  1. 你现在的配置里,没有 server_name www.timinglee.org; 对应的 server 块

    • 当请求的域名 www.timinglee.org 不匹配任何 server_name 时,Nginx 会使用默认 server
    • 你的配置里,只有一个 server 块,而且它是第一个,所以它就是默认 server
相关推荐
乐维_lwops14 分钟前
乐维社区“专家坐诊”第403期问答
运维·网络·监控系统·答疑·lerwee运维智能体
落苜蓿蓝17 分钟前
Java 循环中对象复用导致属性覆盖?从 JVM 内存模型讲解原因
java·jvm·python
爱喝水的鱼丶18 分钟前
SAP-ABAP:SAP 实战笔记:BAPI_OUTB_DELIVERY_CONFIRM_DEC 详解——外向交货单过账的利器
运维·性能优化·sap·abap·经验交流
weixin_4407841121 分钟前
Android基础知识汇总
android·java·android studio
布鲁飞丝22 分钟前
从零实现富文本编辑器#-浏览器选区与编辑器选区模型同步
java·前端·编辑器
国际云,接待24 分钟前
AWS RDS 备份与 PITR 恢复演练:从保留期、恢复命令到应用切换验证
运维·云计算·aws·灾难恢复·数据库备份·rds
回眸不遇1 小时前
类型的区别和应用场景 nested 每个对象独立存储为隐藏的子文档 适合存储数组 查询的时候有特定的语法 nested查询 且字段要 ...
运维·jenkins
jun_bai1 小时前
使用java安全的移动文件
java
caishenzhibiao1 小时前
市场同步系统 同花顺期货通指标
java·c语言·c#
Devin~Y1 小时前
从本地生活电商到 AI RAG:互联网大厂 Java 面试场景完整实战
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag