小工具知识网

当前位置: 首页 > 行业知识 > Nginx多location实现多个静态资源的访问

行业知识

Nginx多location实现多个静态资源的访问

2026-11-11 20:05:52 来源:互联网转载

摘要:最近在自己的云服务器上部署了前端项目(原来已部署一个项目),这就需要通过Server块的配置,实现多个项目位于同一台服务器,记录下配置方法如下

        由于之前在服务器上放了个静态页面,占用了通用的匹配规则,再增加其他内容需处理多了location的匹配规则。

1. Nginx的配置文件结构

        先复习下Nginx的配置文件结构分如下三部分全局块、events块、http块。

user nginx;worker_processes auto;  ## 全局块events {    ## events块    worker_connections 1024;}http {      ## http块    include mime.types;  ## http全局块    default_type application/octet-stream;        server {             ## server块        listen 80;        server_name example.com;                location / {            root /usr/share/nginx/html;            index index.html;        }    }}
  • user nginx:指定 Nginx 运行的用户和用户组。
  • worker_processes auto:设定 Nginx 使用多少个 worker 进程来处理请求,使用auto表示根据 CPU 核心数动态设置。
  • events { ... }:配置事件处理模型,包括连接数等参数。
  • http { ... }:配置 HTTP 服务的全局设置。
    • include mime.types:引入 MIME 类型配置文件。
    • default_type application/octet-stream:默认的 MIME 类型。
    • server { ... }:定义一个虚拟主机。
      • listen 80:监听端口。
      • server_name example.com:指定域名。
      • location / { ... }:定义 URI 匹配规则和对应的处理逻辑。
        • root /usr/share/nginx/html:指定根目录。
        • index index.html:默认首页文件。

            此处需要配置的是server块中的location,它是Nginx中的块级指令(block directive),location指令的功能是用来匹配不同的url请求,进而对请求做不同的处理和响应。

    2. location的匹配规则

            从上可以看出实现多个静态资源的访问,需要修改server中location的配置,按照一定的规则匹配不同的资源。location的匹配规则如下:

    模式含义
    location = /url= 表示精准匹配,只有内容同表达式完全一致才会匹配成功。

    location = /abc/ { ...}

    #只匹配http://abc.com/abc

    #http://abc.com/abc [匹配成功]

    #http://abc.com/abc/index [匹配失败]

    location ^~ /url^~ 开头对URL路径进行前缀匹配,并且在正则之前

    location ^~ /index/ {.....}

    #以 /index/ 开头的请求,都会匹配上

    #http://abc.com/index/index.page [匹配成功]

    #http://abc.com/error/error.page [匹配失败]

    location ~ pattern表示区分大小写的正则匹配

    location ~ /Abc/ {.....}

    #http://abc.com/Abc/ [匹配成功]

    #http://abc.com/abc/ [匹配失败]

    location ~* pattern表示不区分大小写的正则匹配

    location ~* /Abc/ {.....}

    # 则会忽略 uri 部分的大小写

    #http://abc.com/Abc/ [匹配成功]

    #http://abc.com/abc/ [匹配成功]

    location /url不带任何修饰符,也表示前缀匹配,但是在正则匹配之后

    location /index/ {......}

    #http://abc.com/index [匹配成功]

    #http://abc.com/index/index.page [匹配成功]

    #http://abc.com/test/index [匹配失败]

    #http://abc.com/Index [匹配失败] 

    location /通用匹配,任何未匹配到其他location的请求都会匹配到,兜底的相当于switch中的default。

    nginx 基础配置:多个location转发任意请求或访问静态资源文件_nginx 前端转发指向不同的静态资源-CSDN博客文章浏览阅读3.4w次,点赞4次,收藏10次。server { #监听的端口 listen 80; #监听的域名 server_name localhost; #监听带后缀的url location ^~\.txt { #文件放到/html文件夹下 root /; }..._nginx 前端转发指向不同的静态资源https://blog.csdn.net/tutian2000/article/details/81531513https://blog.csdn.net/tutian2000/article/details/81531513

              最终,location添加多个,但是要相互之间不要冲突,否则会请求超时。nginx配置文件修改如下:

    user  nginx;worker_processes  auto;error_log  /var/log/nginx/error.log notice;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    **server {      listen 80; # 监听端口      server_name your_domain.com; # 域名或者服务器 IP      location /daohang {        alias /usr/share/nginx/html2; # 静态资源文件目录        index index.html; # 默认首页文件      }      location / {        root /data/wwwroot/mallv/; # 慕慕生鲜用户        index index.html index.htm; # 默认首页文件      }      location /admin {        alias /data/wwwroot/admin/; # 慕慕生鲜管理员        index index.html index.htm; # 默认首页文件      }    }**    include /etc/nginx/conf.d/*.conf;}

上一篇:揭秘扫雷背后的C语言源码与逻辑奥秘深度解析

下一篇:K最临近算法(KNN)