在复杂的 Nginx 设置中启用静态目录列表

在复杂的 Nginx 设置中启用静态目录列表

我们的网站配置如下:

  • 前端由 Next.js 创建的静态 HTML 文件提供支持。当向前端发出请求时,用户正在查看这些 HTML 文件
  • Next.js 从 Wordpress API 中提取数据
  • 当静态 HTML 文件无法满足请求时,它将被传递给 wordpress
  • 由于路由的配置方式,我们必须从所有 URL 中删除正斜杠(解决这一问题可能是以下问题的解决方案)
  • 我们还需要从静态目录列表中提供一些 MP3 文件,这些文件的名称每周都会更改
  • 静态 HTML 文件来自 /var/www/next/source/serve
  • Wordpress 文件位于 /var/www/wordpress

以下是主要配置:

# Expires map
map $sent_http_content_type $expires {
  default                                off;
  #text/html                              epoch;
  text/css                               max;
  application/javascript                 max;
  ~image/                                max;
  font/ttf                               max;
  font/opentype                          max;
  application/font-woff                  max;
  font/woff2                             max;
  application/vnd.ms-fontobject          max;
}

server {
  # Initial setup
  listen 443 http2 ssl;
  server_name www.website.com;

  expires $expires;

  # Include SSL configutation
  include snippets/ssl-website.com.conf;
  include snippets/ssl-params.conf;

  # Set root directory
  root /var/www/next/source/serve;

  # Access and error logs
  access_log /var/log/nginx/next_access.log;
  error_log /var/log/nginx/next_error.log;

  # GZIP
  include conf.d/gzip.conf;

  # Cache locatons
  include locations/cache-locations.conf;

  # Non-critical / one-off locations
  include locations/special-locations.conf;

  # Get rid of all trailing slashes. This makes the root (/) location below function properly
  if (!-d $request_filename) {
    rewrite ^/(.*)/$ /$1 redirect;
  }

  location ~* \.(?:eot|ttf|svg|woff|woff2)$ {
    expires max;
    add_header Vary Accept-Encoding;
    add_header Cache-Control public;
    access_log off;
  }

  # First try for the static HTML file. If not found, go to Wordpress
  location / {
    try_files $uri $uri.html $uri.html/ $uri/ /index.php?q=$uri&$args;
  }

  # Wordpress
  location ~ \.php$ {
    root /var/www/wordpress;
    index index.php index.html index.htm;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    error_log /var/log/nginx/wordpress_error.log;
  }

  # Include redirects
  include redirects/primary-redirects.conf;

}

special-locations.conf文件包含一个名为的静态目录列表audiofiles

location ^~ /audiofiles {
  root /var/www/wordpress;
  autoindex on;
  break;
}

目前一切都按预期工作(前端、WP-Admin、Wordpress 静态资源(CSS、JS)、API 请求、图像文件等),除了 处的静态目录列表/audiofiles

问题是由rewrite根服务器块中的规则引起的。我们无法将其移入location /块中(无论如何我都不这么认为),因为每种路由都基于根 URL。

我尝试了无数种配置才解决这个问题。我该怎么做才能解决这个问题?

******** 更新后解决方案 ********

感谢@Richard Smith 提供的简单而优雅的解决方案。最终配置如下所示:

# Expires map
map $sent_http_content_type $expires {
  default                                off;
  text/css                               max;
  application/javascript                 max;
  ~image/                                max;
  font/ttf                               max;
  font/opentype                          max;
  application/font-woff                  max;
  font/woff2                             max;
  application/vnd.ms-fontobject          max;
}

server {
  # Initial setup
  listen 443 http2 ssl;
  server_name www.website.com;

  expires $expires;

  # Include SSL configutation
  include snippets/ssl-website.com.conf;
  include snippets/ssl-params.conf;

  # Set root directory
  root /var/www/next/source/serve;

  # Access and error logs
  access_log /var/log/nginx/next_access.log;
  error_log /var/log/nginx/next_error.log;

  # GZIP
  include conf.d/gzip.conf;

  # Cache locatons
  include locations/cache-locations.conf;

  # Non-critical / one-off locations
  include locations/special-locations.conf; # This file contains the `audiofiles` static file directory

  location ~* \.(?:eot|ttf|svg|woff|woff2)$ {
    expires max;
    add_header Vary Accept-Encoding;
    add_header Cache-Control public;
    access_log off;
  }

  # First try for the static HTML file. If not found, pass along to @redirects below
  location / {
    try_files $uri $uri.html $uri.html/ $uri/ @redirect;
  }

  # Some clever redirect management. Try either static files or send to Wordpress
  location @redirect {
    rewrite ^/(.*)/$ /$1 redirect;
    rewrite ^ /index.php?q=$uri&$args last;
  }

  # Wordpress
  location ~ \.php$ {
    root /var/www/wordpress;
    index index.php index.html index.htm;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    error_log /var/log/nginx/wordpress_error.log;
  }

  # Include redirects
  include redirects/migrate-redirects.conf;
}

答案1

对于复杂的 Nginx 设置,很难给出明确的答案。但语句$uri/的组件try_files应该执行!-d $request_filename测试,然后您可以使用命名location来执行两次重定向。

例如:

location / {
    try_files $uri $uri.html $uri.html/ $uri/ @redirect;
}
location @redirect {
    rewrite ^/(.*)/$ /$1 redirect;
    rewrite ^ /index.php?q=$uri&$args last;
}

相关内容