Nginx 核心配置-根目录root指令与别名alias指令实战案例
Nginx核心配置-根目录root指令与别名alias指令实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.试验环境说明
1>.虚拟机环境说明
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release
CentOS Linux release 7.6. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# grep 172.30.1.101 /etc/hosts
172.30.1.101 node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]#
2>.主配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes ;
worker_cpu_affinity ; events {
worker_connections ;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-;
keepalive_timeout ;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
} #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
3>.Nginx源码方式安装步骤
博主推荐阅读:
https://www.cnblogs.com/yinzhengjie/p/12031651.html
二.编辑root案例
1>.编辑子配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/image.conf
server {
listen ;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/pc;
index index.html;
} location /image {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
2>.创建测试数据
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1>" > /yinzhengjie/data/web/nginx/html/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/html/index.html
<h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# file /yinzhengjie/data/web/nginx/html/index.html
/yinzhengjie/data/web/nginx/html/index.html: ASCII text
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/html/image
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/image’
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : 01人柱力.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# mv 01人柱力.jpg /yinzhengjie/data/web/nginx/html/image/.jpg #你可以自己从网上下载一些图片,我这直接从本地拷贝的。
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,0,255)'>In the Image</h1>" > /yinzhengjie/data/web/nginx/html/image/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/html/image/
total
-rw-r--r-- root root Dec : .jpg
-rw-r--r-- root root Dec : index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cd /yinzhengjie/data/web/nginx/html/image/
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html/image]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html/image]# file .jpg
.jpg: JPEG image data, JFIF standard 1.01
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html/image]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html/image]# file index.html
index.html: ASCII text
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html/image]#
3>.重新加载配置文件,是子配置文件生效
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep #master进程的ID并没有发生改变,但worker进程的PID变化了,说明reload执行成功了。
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 、
4>.浏览器访问"http://node101.yinzhengjie.org.cn/image/",可以访问到数据,如下图所示。

5>.浏览器访问"http://node101.yinzhengjie.org.cn/image/01.jpg",可以访问到数据,如下图所示。

三.编辑alias案例
1>.修改子配置文件
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/image.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/image.conf
server {
listen ;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/pc;
index index.html;
} location /image {
#注意,root指令表示指定默认的web根目录所在位置,当请求到当前location时,其实访问的是/yinzhengjie/data/web/nginx/html/image/index.html
#root /yinzhengjie/data/web/nginx/html;
#alias和root是有所区别的,alias指令表示指定路径别名,当请求到当前location时,其实访问的是/yinzhengjie/data/web/nginx/html/index.html
alias /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
2>.重新加载nginx的配置文件
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
3>.浏览器访问"http://node101.yinzhengjie.org.cn/image/",可以访问到数据,如下图所示。

4>.浏览器访问"http://node101.yinzhengjie.org.cn/image/01.jpg",不可以访问到数据,如下图所示。

Nginx 核心配置-根目录root指令与别名alias指令实战案例的更多相关文章
- Nginx 核心配置-可优化配置参数
Nginx 核心配置-可优化配置参数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx的官网查看指令帮助信息方法 1>.打开nginx的官网(https://ng ...
- Nginx 核心配置-作为上传服务器配置
Nginx 核心配置-作为上传服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关键参数说明 client_max_body_size 1m: 设置允许客户端上传单 ...
- Nginx 核心配置-新建一个web站点
Nginx 核心配置-新建一个web站点 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx基础配置常用参数说明 [root@node101.yinzhengjie.or ...
- Nginx 核心配置-作为下载服务器配置
Nginx 核心配置-作为下载服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.无限速版本的下载服务器 1>.查看主配置文件 [root@node101.yinz ...
- Nginx 核心配置-长连接配置
Nginx 核心配置-长连接配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.长连接配置参数说明 keepalive_timeout number; 设定保持连接超时时长,0 ...
- Nginx 核心配置-检测文件是否存在
Nginx 核心配置-检测文件是否存在 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件 ...
- Nginx 核心配置-自定义日志路径及清空日志注意事项
Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...
- Nginx 核心配置-自定义错误页面
Nginx 核心配置-自定义错误页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个h ...
- Nginx 核心配置-location的登录账户认证实战篇
Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...
随机推荐
- SQL Server 创建数据库
创建数据库有两种方式: 方式1-图形化界面创建: 1. 鼠标右击“数据库”,然后点击新建数据库 2.设置常规选项卡 2.1 给数据库命名,一般多个单词要用下划线连接,不建议用空格,如Test_DB. ...
- [BJOI2019]勘破神机(第一类斯特林数,斐波那契数列)
真的是好题,只不过强行多合一有点过分了…… 题目大意: $T$ 组数据.每个测试点中 $m$ 相同. 对于每组数据,给定 $l,r,k$,请求出 $\dfrac{1}{r-l+1}\sum\limit ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 509道Java面试题解析:2020年最新Java面试题
<Java面试全解析>是我在 GitChat 发布的一门电子书,全书总共有 15 万字和 505 道 Java 面试题解析,目前来说应该是最实用和最全的 Java 面试题解析了. 我本人是 ...
- linux查看每秒的网络流量
import os import time cmd = 'ifconfig | grep "RX bytes" | tail -1 | awk -F":" \' ...
- vs2019 更新之后无法用ctrl+d再设置回来..
工具-选项-环境-键盘
- There is no getter for property named 'id' in 'class java.lang.Integer
There is no getter for property named 'id' in 'class java.lang.Integer 问题描述: 使用mybatis传入参数, 当参数类型是St ...
- Linux内核中的并发与竞态概述
1.前言 众所周知,Linux系统是一个多任务的操作系统,当多个任务同时访问同一片内存区域的时候,这些任务可能会相互覆盖内存中数据,从而造成内存中的数据混乱,问题严重的话,还可能会导致系统崩溃. 2. ...
- c++小学期大作业攻略(三)用户系统
Update at 2019/07/22 14:16 发现一个大坑,我们后期是打算用QSS统一堆样式进行美化的,于是我把之前对QLabel进行的setAlignment全部去掉了,打算统一丢进Qss里 ...
- JVM 的垃圾回收器详解
Parallel Scavenge(Paraller):Parallel Scavenge和ParNew关注的点不一样:ParNew关注的是尽可能缩短暂停的时间,Parallel Scavenge关注 ...