大网数据广告活动
服务器运维问答

Unable to serve NextJS static assets through Nginx

· 52CE 我爱测

I have a NextJS app running on port 3000 and I have a test.ico file located at the following location: /static/images/icons/ I want this file to be served off the root rather than the actual path. I mean instead of https://www.sc…

问题原文

原帖链接 · TheLearner · CC BY-SA 4.0

I have a NextJS app running on port 3000 and I have a test.ico file located at the following location:

/static/images/icons/

I want this file to be served off the root rather than the actual path. I mean instead of https://www.schandillia.com/static/images/icons/test.ico, I want it to be available as https://www.schandillia.com/test.ico. I can easily configure my Express server to serve this file but I want to let Nginx handle this. So I have the following configuration added to my Nginx conf server block:

location / {
    proxy_pass http://127.0.0.1:3000;
    charset UTF-8;
    proxy_http_version 1.1;
  }
  location /test.ico {
    proxy_pass http://127.0.0.1:3000/static/images/icons/test.ico;
    expires 365d;
    add_header Pragma public;
    add_header Cache-Control "public";
  }
  location ~* \.(?:ico|svg|woff|woff2|ttf|otf|css|js|gif|jpe?g|png)$ {
   proxy_pass http://127.0.0.1:3000;
   expires 365d;
   add_header Pragma public;
   add_header Cache-Control "public";
  }

However, https://www.schandillia.com/test.ico is now throwing a 404. What am I doing wrong?

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The regular expression location takes precedence over regular prefix location blocks. See this document for details.

You can change the precedence order by using any of these instead:

location = /test.ico
location ^= /test.ico
location ~ ^/test.ico

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。