【问题标题】:nginx on centos 7 is servicing pages using www.domain.tld but not domain.tld (all request are http on port 80)centos 7 上的 nginx 使用 www.domain.tld 而不是 domain.tld 为页面提供服务(所有请求都是端口 80 上的 http)
【发布时间】:2020-07-07 01:23:01
【问题描述】:

我遇到以下问题:
我的 centos 7 vps 配置了 nginx 和两个 server-blocks (vhosts)
对于这两个域,我可以使用 www.domain.tld 访问网页,但不能使用 domain.tld。
访问 domain.tld 时不会生成错误或访问日志
dns 记录被检查并指向具有空 A 记录的服务器 IP 地址(当服务器配置 ubuntu 18.04、iRedMail、nextcloud 和两个网站时,相同的 dns 记录正常工作)。

浏览器中的错误消息是:“无法访问此站点。domain.tld 拒绝连接。”

以下是配置文件(nginx 和一个域),我已包含路径以供参考:

nginx.conf

#cat /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;


  server {
    listen 80 default_server;
    server_name "";
    return 444;
  }
}

* default_server 用于在访问 http:\serverIP 时阻止访问 nginx 欢迎页面。将此配置返回到 nginx 默认值不会影响行为。

域配置

# cat /etc/nginx/sites-enabled/domain.tld.conf 

server {
   listen   80;
   server_name  domain.tld www.domain.tld;

   location / {
        root  /var/www/domain.tld;
        index  index.html index.htm;
   }

   error_page 404 /404.html;
       location = /40x.html {
   }

   error_page 500 502 503 504 /50x.html;
       location = /50x.html {
   }
# Setting log locations
   access_log /var/log/nginx/domain.tld-access.log;
   error_log /var/log/nginx/domain.tld-error.log debug;
}

不得不提的是,这个VPS的目的是学习基本的Linux sysadmin,因此没有实际的数据或限制,也是我第一次接触centos。

感谢您的时间和精力。

L.E. 将“127.0.0.1 www.domain.tld domain.tld”添加到 /etc/hosts 并不能解决问题。这两个域都是具有 dns 服务器的真实域,并且所有记录都被复制。此外,使用跟踪路由到域 VPS IP。

Nginx 配置成功验证

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

经过进一步调查,我注意到访问 domain.tld 时没有请求出现在访问或错误日志中(错误设置为调试),但他们确实为 www.domain.tld Witch 会直接将我发送到 DNS,但使用一些 dig 在线版本检查 DNS 似乎一切正常,domain.tld 和 www.domain.tld 都指向正确的 VPS ip

【问题讨论】:

    标签: nginx nginx-config


    【解决方案1】:

    在花了将近两天的时间试图找出导致此问题的原因之后,我无意中在另一台 PC 上远程访问了该网站,令我最惊讶的是一切正常。 现在我确定我已经多次清除了我的 DNS 缓存,但显然问题是由插件 httpsEverywhere 产生的。

    谢谢,我保证下次会更加努力:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2015-02-10
      • 2015-08-20
      • 2021-05-12
      • 2018-11-18
      相关资源
      最近更新 更多