英文:
Why aren't my sub pages loading when they are in location blocks in the nginx configuration?
问题
以下是您提供的内容的中文翻译:
我有一个特定的子域名:/var/www/test.domain.com。现在我想要有多个页面,比如/about页面和/contact页面。主页运行正常。出于某种原因,我无法加载这些子页面。
目前,我在/var/www/test.domain.com文件夹中有3个文件
- index.html
 - about.html
 - contact.html
 
我原以为我不必为所有子页面创建不同的位置块,但我不确定。我的“默认”位置块如下所示:
  location / {
    try_files $uri $uri/ =404;
  }
然而,由于这没有起作用,我也尝试了这个:
server {
  root /var/www/test.domain.com;
  server_name test.domain.com;
  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  if ($scheme != "https") {
    return 301 https://$host$request_uri;
  } # 由Certbot管理
  location / {
    try_files $uri $uri/ =404;
  }
  location /about {
    root /var/www/test.domain.com;
    index about.html;
  }
  location /contact {
    root /var/www/test.domain.com;
    index contact.html;
  }
}
我重启了nginx,但/about页面和/contact页面返回404错误。问题似乎是什么?
英文:
I have a certain subdomain: /var/www/test.domain.com. Now I would like to have multiple pages, so an /about page and a /contact page for example. The homepage works just fine. For some reason I cannot get these subpages to load.
Currently I have 3 files in my /var/www/test.domain.com folder
- index.html
 - about.html
 - contact.html
 
I was under the impression that I did not have to create different location blocks for all my subpages, but I am not sure about this. My 'default' location block looked like this:
  location / {
    try_files $uri $uri/ =404;
  }
However, since that did not work, I also tried this:
server {
  root /var/www/test.domain.com;
  server_name test.domain.com;
  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  if ($scheme != "https") {
    return 301 https://$host$request_uri;
  } # managed by Certbot
  location / {
    try_files $uri $uri/ =404;
  }
  location /about {
    root /var/www/test.domain.com;
    index about.html;
  }
  location /contact {
    root /var/www/test.domain.com;
    index contact.html;
  }
}
I restart nginx but the /about page and /contact page returns a 404. What seems to be the problem?
答案1
得分: 1
返回 about.html 文件并使用 URL /about - 您可以在 try_files 语句 中使用 $uri.html 术语。
例如:
location / {
    try_files $uri $uri.html $uri/ =404;
}
英文:
To return the about.html file with the URL /about - you could use a $uri.html term in the try_files statement.
For example:
location / {
    try_files $uri $uri.html $uri/ =404;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论