Nginx核心配置-根目录root指令与别名alias指令实战案例
Nginx核心配置-根目录root指令与别名alias指令实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.试验环境说明
1>.虚拟机环境说明
[root@node101.yinzhengjie.org.cn ~]# uname -r 3.10.0-957.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.1810 (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 4; worker_cpu_affinity 00000001 00000010 00000100 00001000; events { worker_connections 100000; use epoll; accept_mutex on; multi_accept on; } http { include mime.types; default_type application/octet-stream; sendfile on; gzip on; charset utf-8; keepalive_timeout 65 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 /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 80; 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 244 -rw-r--r-- 1 root root 248743 Dec 15 22:38 01人柱力.jpg [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# mv 01人柱力.jpg /yinzhengjie/data/web/nginx/html/image/01.jpg #你可以自己从网上下载一些图片,我这直接从本地拷贝的。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 0 [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 248 -rw-r--r-- 1 root root 248743 Dec 15 22:38 01.jpg -rw-r--r-- 1 root root 49 Dec 15 23:07 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 01.jpg 01.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 2999 1 0 22:24 ? 00:00:00 nginx: master process nginx nginx 3141 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3142 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3143 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3144 2999 0 22:46 ? 00:00:00 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 2999 1 0 22:24 ? 00:00:00 nginx: master process nginx nginx 3157 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3158 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3159 2999 0 22:46 ? 00:00:00 nginx: worker process nginx 3160 2999 0 22:46 ? 00:00:00 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 80; 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 2999 1 0 22:24 ? 00:00:00 nginx: master process nginx nginx 3667 2999 0 23:19 ? 00:00:00 nginx: worker process nginx 3668 2999 0 23:19 ? 00:00:00 nginx: worker process nginx 3669 2999 0 23:19 ? 00:00:00 nginx: worker process nginx 3670 2999 0 23:19 ? 00:00:00 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 2999 1 0 22:24 ? 00:00:00 nginx: master process nginx nginx 3675 2999 4 23:19 ? 00:00:00 nginx: worker process nginx 3676 2999 5 23:19 ? 00:00:00 nginx: worker process nginx 3677 2999 5 23:19 ? 00:00:00 nginx: worker process nginx 3678 2999 5 23:19 ? 00:00:00 nginx: worker process [root@node101.yinzhengjie.org.cn ~]#
3>.浏览器访问"http://node101.yinzhengjie.org.cn/image/",可以访问到数据,如下图所示。
4>.浏览器访问"http://node101.yinzhengjie.org.cn/image/01.jpg",不可以访问到数据,如下图所示。
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12037100.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。