使用具有不同命名的HTML文件的nginx位置

时间:2018-04-28 08:23:05

标签: nginx nginx-location

我希望我的www.example.com/select返回文件/srv/www/example/static/html/select.html 我已经将我的example.conf设置如下,但它没有做到这一点

server {
  server_name  mywebsite.com www.mywebsite.com;

  root /srv/www/mywebsite/static;

  #charset koi8-r;
  #access_log  /var/log/nginx/host.access.log  main;

  location = / {
      index  html/index.html;
  }

  location = /select {
      index html/select.html;
  }

  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

虽然返回了index.html,但尝试获取/选择将记录此错误

2018/04/28 08:16:59 [error] 17160#17160: *43106 open() "/srv/www/mywebsite/static/select" failed (2: No such file or directory)

我应该如何配置我的nginx以获得所需的结果?

1 个答案:

答案 0 :(得分:1)

index指令对以/结尾的URI进行操作,因此在这种情况下不起作用。有关详细信息,请参阅this document

如果您只有一个URI,则可以使用:

location = /select {
    rewrite ^ /html/select.html last;
}

是否有理由将/html/后缀从root值中删除?更通用的解决方案可能是:

root /srv/www/mywebsite/static/html;

location / {
    try_files $uri $uri/ $uri.html =404;
}

有关详细信息,请参阅this document