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

Why isn't my Nginx alias directive working?

· 52CE 我爱测

I'm using Nginx on CentOS 7 with Ruby on Rails / Puma. I would like for certain file paths to be skipped by RoR and be served directly by Nginx. I created this in my "server" block for my site location /assets/lib/ { alias /home/…

问题原文

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

I'm using Nginx on CentOS 7 with Ruby on Rails / Puma. I would like for certain file paths to be skipped by RoR and be served directly by Nginx. I created this in my "server" block for my site

location /assets/lib/ {
    alias /home/rails/myproject_production/app/assets/javascripts/lib/;
    autoindex off;
}

However, when I call an asset like http://www.mydomein.com/assets/lib/myfile.js, I get a 404. This is what appears in my logs

2018/05/11 15:21:47 [error] 242#0: *1 open() "/home/rails/myproject_production/public/assets/lib/myfile.js" failed (2: No such file or directory), client: 50.240.135.5, server: www.mydomein.com, request: "GET /assets/lib/myfile.js HTTP/1.1", host: "www.mydomein.com"

From the logs, it appears that the alias isn't being invoked at all. What am I missing in the above? My complete configuration is below

upstream myproject {
  server unix:///home/rails/myproject_production/shared/sockets/puma.sock;
}

# Listener for Apex Domain
server {
  listen 80;
  server_name www.mydomein.com;
  root /home/rails/myproject_production/public; # I assume your app is located at this location

  location / {
    proxy_pass http://myproject; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    if ($request_uri ~* "(\/image\/.*)|(.*\.(ico|gif|jpe?g|png)$)") {
      expires 60d;
      access_log off;
      add_header Pragma public;
      add_header Cache-Control "public";
      break;
    }
  }

  location /assets/lib/ {
        alias /home/rails/myproject_production/app/assets/javascripts/lib/;
        autoindex off;
  }

  location /cable {
    proxy_pass http://myproject;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

}

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

You have two location blocks:

location /assets/lib/ {
  …
}
location ~* ^/assets/ {
  …
}

Now, as described in Nginx documentation, Nginx checks first prefix locations, and remembers longest match. Then it checks regular expression locations, and uses a matching block if found. If none is found, then it uses the prefix match.

So, in your case, nginx first finds the first location block, remembers it. However, the regular expression block also matches, and nginx selects that block.

In order to change nginx selection process, you need to use:

location ^~ /assets/lib {
  …
}

for your first location. It also means that the directives in the regular expression block are not applied to these requests.

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

资料来源与适用范围

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

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

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