nginx 静态目录配置规则,路径匹配与本地资源

简介: 经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使 用以下规则 假如nginx是在本机,静态目录也是在本机, 1、子目录匹配 如下配置  Java代码   location / {       root /data/www;   }   访问http://127.

经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使 
用以下规则 

假如nginx是在本机,静态目录也是在本机, 

1、子目录匹配 
如下配置 

Java代码   收藏代码
  1. location / {  
  2.     root /data/www;  
  3. }  



访问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/下的路径 

而对于配置 

Java代码   收藏代码
  1. location /images/ {  
  2.     root /data/www;  
  3. }  


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

2、重复路径匹配规则 

对于如下配置: 

Java代码   收藏代码
  1. server {  
  2.     location / {  
  3.         root /data/www;  
  4.     }  
  5.   
  6.     location /images/ {  
  7.         root /data;  
  8.     }  
  9. }  



访问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. 
目录
相关文章
|
7月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
627 1
|
7月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
417 18
|
7月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
680 17
|
8月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
9月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1982 10
|
8月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
397 0
|
10月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
722 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
|
应用服务中间件 nginx
Nginx进程配置指令详解
Nginx进程配置指令主要包括:`worker_processes`设置工作进程数;`worker_cpu_affinity`绑定CPU核心;`worker_rlimit_nofile`设置最大文件描述符数量;`worker_priority`设置进程优先级;`worker_connections`设置最大连接数;`daemon`控制守护进程模式;`master_process`启用主进程模式;`pid`设置PID文件路径;`user`指定用户和组;`error_log`配置错误日志。这些指令在`nginx.conf`中配置,用于优化和控制Nginx的运行行为。
553 10
|
缓存 运维 应用服务中间件
运维系列.Nginx配置中的高级指令和流程控制
运维系列.Nginx配置中的高级指令和流程控制
1262 1
|
网络协议 应用服务中间件 nginx
Nginx的http块sendfile,keepalive_timeout的配置指令说明
Nginx的http块sendfile,keepalive_timeout的配置指令说明