我的子页面为什么在nginx配置的位置块中不加载?

huangapple go评论57阅读模式
英文:

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;
}

huangapple
  • 本文由 发表于 2023年5月24日 23:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定