Nginx 重定向 www,但不重定向非 www

Nginx 重定向 www,但不重定向非 www

192.123.100.251我已经在具有以下 IP 地址的服务器中使用 Bind9 配置了 DNS 服务器:

named.conf
==========
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
include "/etc/bind/named.conf.default-zones";

zone "example.com" {
    type master;
    file "/etc/bind/db.example.com";
    allow-transfer { none; };
    allow-query { any; };
};

zone "100.123.192.in-addr.arpa" {
    type master;
    file "/etc/bind/db.100.123.192";
    allow-transfer { none; };
    allow-query { any; };
};

db.example.com
================
$TTL 86400 ;
@   IN  SOA ns1.example.com. root.example.com. (
    1 ; 
    6H ;
    1H ;
    2W ;
    3H ;
)
@   IN  NS  ns1.example.com.
ns1 IN  A   192.123.100.251
www IN  A   192.123.100.251

db.100.123.192
=============
$TTL 86400  ;
@   IN  SOA ns1.example.com. root.example.com. (
    1   ;
    6H  ;
    1H  ;
    2W  ;
    3H  ;
)
    IN  NS  ns1.example.com.
ns1 IN  A   192.123.100.251
251 IN  PTR ns1.example.com.

然后我安装了 Nginx,将其作为 Tomcat 的反向代理(尚未实现)。我需要实现 SSL,因此我进行了以下配置:

/etc/nginx/sites-available/example.com
======================================
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

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

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    return 302 https://$server_name$request_uri;
}

/etc/nginx/snippets/self-signed.conf 
====================================
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

/etc/nginx/snippets/ssl-params.conf 
===================================
ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparam.pem;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
ssl_session_timeout  10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off; # Requires nginx >= 1.5.9
ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Disable strict transport security for now. You can uncomment the following
# line if you understand the implications.
# add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

我可以访问www.example.com,并且https协议似乎已成功实施,但是我无法访问example.com按照 Chrome 的提示进入我的网站无法访问此网站

我遗漏了什么吗?提前致谢。

我正在关注这个教程:

答案1

抛开这个问题的公共 IP 地址问题,你最明显的问题(对我来说)是db.example.com你错过了@ IN A 192.123.100.251

在调试此类问题时,首先要从第一原理开始

  1. dig @<nameserver> <host>在这种情况下我可以得到 DNS 响应吗dig @ns1.example.com example.com (我故意忽略了粘合要求,因为名称服务器的主机名位于您正在查询的域内)
  2. 你能 ping 一下服务器吗(这个方法现在越来越没用了,因为人们过滤 ICMP)但它也能确认 DNS 解析是否正常工作
  3. 您是否可以使用命令行工具访问该网站,例如curl -vv https://example.com某些响应对于某些浏览器来说太短或花费的时间太长

相关内容