nginx 将非 www 重定向到我的 index.html 页面时出现问题

nginx 将非 www 重定向到我的 index.html 页面时出现问题

我遇到了 Nginx 的问题,需要你的帮助。

我创建了 2 个 A DNS 记录:

www.domain_name.docker.hub.registry.domain_nameetc.com域名.docker.hub.registry.domain_nameetc.com

我需要它们都返回 /var/www/domain_name.docker.hub.registry/html 文件夹下存在的 index.html 页面。

到目前为止,当我浏览到www.domain_name.docker.hub.registry.domain_nameetc.com,出现index.html页面。

当我浏览到域名.docker.hub.registry.domain_nameetc.com,出现“欢迎使用 nginx”页面。

我该如何配置域名.docker.hub.registry.domain_nameetc.com返回index.html页面而不是标准的“欢迎来到nginx”页面?

下面提供的是我的服务器块代码。先感谢您 !!

server {

    root /var/www/domain_name.docker.hub.registry/html;
    index index.html;
   
    listen 80;
    listen 443 ssl;
    server_name domain_name.docker.hub.registry domain_name.docker.hub.registry.domain_nameetc.com;

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

    #listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/domain_name.docker.hub.registry.domain_nameetc.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain_name.docker.hub.registry.domain_nameetc.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

    return 301 https://www.domain_name.docker.hub.registry.domain_nameetc.com$request_uri;
}
server {

    root /var/www/domain_name.docker.hub.registry/html;
    index index.html index.htm;

    # listen to the standard http port 80
    #listen 80;
    listen 443 ssl;

    server_name domain_name.docker.hub.registry www.domain_name.docker.hub.registry.domain_nameetc.com;

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

    ssl_certificate /etc/letsencrypt/live/www.domain_name.docker.hub.registry.domain_nameetc.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.domain_name.docker.hub.registry.domain_nameetc.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
}

答案1

我想到了。下面提供的是我采用的解决方案。请提供有关最终解决方案的任何反馈。

server {
  root /var/www/<domain>.docker.hub.registry.<domain>.com/html;
  index index.html;

  server_name <domain>.docker.hub.registry.<domain>.com;
  listen 80;
  listen [::]:80;

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

server {
  root /var/www/<domain>.docker.hub.registry.<domain>.com/html;
  index index.html;

  server_name www.<domain>.docker.hub.registry.<domain>com;
  listen 80;
  listen [::]:80;

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

}

server {

  root /var/www/<domain>.docker.hub.registry.<domain>.com/html;
  index index.html;

  server_name <domain>.docker.hub.registry.<domain>.com;

  listen 443 ssl;
  listen [::]:443 ssl;

  ssl_certificate /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.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

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

}

server {

  root /var/www/<domain>.docker.hub.registry.<domain>.com/html;
  index index.html;

  server_name www.<domain>.docker.hub.registry.<domain>.com;

  listen 443 ssl;
  listen [::]:443 ssl;

  ssl_certificate /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/<domain>.docker.hub.registry.<domain>.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

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

相关内容