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

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

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

我原以为我不必为所有子页面创建不同的位置块,但我不确定。我的“默认”位置块如下所示:

  1. location / {
  2. try_files $uri $uri/ =404;
  3. }

然而,由于这没有起作用,我也尝试了这个:

  1. server {
  2. root /var/www/test.domain.com;
  3. server_name test.domain.com;
  4. listen 443 ssl;
  5. ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  6. ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  7. if ($scheme != "https") {
  8. return 301 https://$host$request_uri;
  9. } # 由Certbot管理
  10. location / {
  11. try_files $uri $uri/ =404;
  12. }
  13. location /about {
  14. root /var/www/test.domain.com;
  15. index about.html;
  16. }
  17. location /contact {
  18. root /var/www/test.domain.com;
  19. index contact.html;
  20. }
  21. }

我重启了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:

  1. location / {
  2. try_files $uri $uri/ =404;
  3. }

However, since that did not work, I also tried this:

  1. server {
  2. root /var/www/test.domain.com;
  3. server_name test.domain.com;
  4. listen 443 ssl;
  5. ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  6. ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  7. if ($scheme != "https") {
  8. return 301 https://$host$request_uri;
  9. } # managed by Certbot
  10. location / {
  11. try_files $uri $uri/ =404;
  12. }
  13. location /about {
  14. root /var/www/test.domain.com;
  15. index about.html;
  16. }
  17. location /contact {
  18. root /var/www/test.domain.com;
  19. index contact.html;
  20. }
  21. }

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 术语。

例如:

  1. location / {
  2. try_files $uri $uri.html $uri/ =404;
  3. }
英文:

To return the about.html file with the URL /about - you could use a $uri.html term in the try_files statement.

For example:

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

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:

确定