Nginx 核心配置-根目录root指令与别名alias指令实战案例

时间:2023-02-10 19:47:38

          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/",可以访问到数据,如下图所示。

Nginx 核心配置-根目录root指令与别名alias指令实战案例

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

Nginx 核心配置-根目录root指令与别名alias指令实战案例

 

三.编辑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/",可以访问到数据,如下图所示。

Nginx 核心配置-根目录root指令与别名alias指令实战案例

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

Nginx 核心配置-根目录root指令与别名alias指令实战案例