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

1.试验环境说明

1.1.虚拟机环境说明

[root@nginx-test-10e0e0e22 ~]# uname -r 
3.10.0-957.el7.x86_64
[root@nginx-test-10e0e0e22 ~]# uname -m
x86_64
[root@nginx-test-10e0e0e22 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

1.2.主配置文件

[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf/nginx.conf
user  nginx;
worker_processes  1;
#worker_cpu_affinity 00000001 ; 


error_log  /yanhuihuang/softwares/nginx/logs/error.log warn;
pid        /yanhuihuang/softwares/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
    accept_mutex on;
    multi_accept on; 
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     
   #导入其他路径的配置文件
     include /yanhuihuang/softwares/nginx/conf.d/*.conf;
 }

1.3.Nginx源码方式安装步骤

博主推荐阅读:
         https://www.cnblogs.com/huihuangyan/p/14501280.html

2.编辑root案例

2.1.编辑子配置文件

[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf.d/image.conf
server {
    listen 80;
    server_name 10.0.0.22;

    location / {
        root /yanhuihuang/data/web/nginx/html/pc;
        index index.html;
    }

    location /image {
        root /yanhuihuang/data/web/nginx/html;
        index index.html;
    }
}
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx -t 
nginx: [warn] conflicting server name "10.0.0.22" on 0.0.0.0:80, ignored
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful

2.2.创建测试数据

[root@nginx-test-10e0e0e22 ~]#  echo "<h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yanhuihuang/'</h1>" > /yanhuihuang/data/web/nginx/html/index.html
[root@nginx-test-10e0e0e22 ~]# cat  /yanhuihuang/data/web/nginx/html/index.html
<h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yanhuihuang/'</h1>
[root@nginx-test-10e0e0e22 ~]# file  /yanhuihuang/data/web/nginx/html/index.html
/yanhuihuang/data/web/nginx/html/index.html: ASCII text
[root@nginx-test-10e0e0e22 ~]# mkdir -pv /yanhuihuang/data/web/nginx/html/image
mkdir: created directory ‘/yanhuihuang/data/web/nginx/html/image’                                                                                                                                                              [root@nginx-test-10e0e0e22 ~]# cd /yanhuihuang/data/web/ngin/html/image/
[root@nginx-test-10e0e0e22 ~]# rz
[root@nginx-test-10e0e0e22 image]# ls
1.jpeg
[root@nginx-test-10e0e0e22 image]# echo "<h1 style='color:rgb(0,0,255)'>In the Image</h1>" > /yanhuihuang/data/web/nginx/html/image/index.html
[root@nginx-test-10e0e0e22 image]# ll /yanhuihuang/data/web/nginx/html/image/
total 32
-rw-r--r--. 1 root root 27446 Mar 10  2021 1.jpeg
-rw-r--r--. 1 root root    49 Mar  9 14:41 index.html
[root@nginx-test-10e0e0e22 image]# file /yanhuihuang/data/web/nginx/html/image/1.jpeg 
/yanhuihuang/data/web/nginx/html/image/1.jpeg: JPEG image data, JFIF standard 1.01
[root@nginx-test-10e0e0e22 image]# file /yanhuihuang/data/web/nginx/html/image/index.html 
/yanhuihuang/data/web/nginx/html/image/index.html: ASCII text

2.3.重新加载配置文件,是子配置文件生效

[root@nginx-test-10e0e0e22 ~]# systemctl restart nginx.service

 3.编辑alias案例

3.1.修改子配置文件

[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf.d/image.conf
server {
    listen 80;
    server_name 10.0.0.22;

    location / {
        root /yanhuihuang/data/web/nginx/html/pc;
        index index.html;
    }

    location /image {
        #注意,root指令表示指定默认的web根目录所在位置,当请求到当前location时,其实访问的是/yanhuihuang/data/web/nginx/html/image/index.html
        #root /yanhuihuang/data/web/nginx/html;
        #alias和root是有所区别的,alias指令表示指定路径别名,当请求到当前location时,其实访问的是/yanhuihuang/data/web/nginx/html/index.html
        alias /yanhuihuang/data/web/nginx/html;
        index index.html;
    }
}
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx  -t 
nginx: [warn] conflicting server name "10.0.0.22" on 0.0.0.0:80, ignored
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful
[root@nginx-test-10e0e0e22 ~]# systemctl restart nginx.service

3.2.浏览器访问

 

 

 

posted @ 2021-03-10 14:24  辉煌-love  阅读(433)  评论(0编辑  收藏  举报