设为首页 收藏本站
查看: 567|回复: 0

[经验分享] nginx 静态目录配置规则

[复制链接]

尚未签到

发表于 2016-12-24 06:43:17 | 显示全部楼层 |阅读模式
经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使
用以下规则
假如nginx是在本机,静态目录也是在本机,
1、子目录匹配
如下配置

location / {
root /data/www;
}


访问http://127.0.0.1/时,配匹配/data/www
访问http://127.0.0.1/images时,配匹配/data/www/images
访问http://127.0.0.1/images/1.jpg时,配匹配/data/www/images/1.jpg
也就是说,地址栏里"/"后的路径是直接匹配目录data/www/下的路径
而对于配置

location /images/ {
root /data/www;
}

访问http://127.0.0.1/images时,配匹配/data/www/images
也就是说,地址栏里/images,直接匹配了/data/www的子目录.
经常出问题的是,location里的url随意配了一个名字,如/xxx,但是对应的/data/www目录
下并没有该/data/www/xxx子目录,一访问就404

2、重复路径匹配规则
对于如下配置:

server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}


访问URL http://localhost/images/example.png,将会匹配第二个/images/规则,
虽然也可以匹配location /规则,但nginx默认会选择最长前缀去匹配当前URL,也就是
第二个配置会生效,访问/data/images/目录,而不是/data/www/images/目录

Serving Static Content

引用

Serving Static Content
An important web server task is serving out files (such as images or static HTML pages). You will implement an example where, depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (containing images). This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.
First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.
Next, open the configuration file. The default configuration file already includes several examples of the server block, mostly commented out. For now comment out all such blocks and start a new server block:
    http {
        server {
        }
    }
Generally, the configuration file may include several server blocks distinguished by ports on which they listen to and by server names. Once nginx decides which server processes a request, it tests the URI specified in the request’s header against the parameters of the location directives defined inside the server block.
Add the following location block to the server block:
    location / {
        root /data/www;
    }
This location block specifies the “/” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system. If there are several matching location blocks nginx selects the one with the longest prefix. The location block above provides the shortest prefix, of length one, and so only if all other location blocks fail to provide a match, this block will be used.
Next, add the second location block:
    location /images/ {
        root /data;
    }
It will be a match for requests starting with /images/ (location / also matches such requests, but has shorter prefix).
The resulting configuration of the server block should look like this:
    server {
        location / {
            root /data/www;
        }
        location /images/ {
            root /data;
        }
    }
This is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at http://localhost/. In response to requests with URIs starting with /images/, the server will send files from the /data/images directory. For example, in response to the http://localhost/images/example.png request nginx will send the /data/images/example.png file. If such file does not exist, nginx will send a response indicating the 404 error. Requests with URIs not starting with /images/ will be mapped onto the /data/www directory. For example, in response to the http://localhost/some/example.html request nginx will send the /data/www/some/example.html file.
To apply the new configuration, start nginx if it is not yet started or send the reload signal to the nginx’s master process, by executing:
    nginx -s reload
    In case something does not work as expected, you may try to find out the reason in access.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-318482-1-1.html 上篇帖子: Nginx配置文件说明 下篇帖子: nginx+mongrel(只属于自己的记录)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表