尽管已从配置中删除,但 nginx 仍继续使用“默认 nginx 站点”进行应答

尽管已从配置中删除,但 nginx 仍继续使用“默认 nginx 站点”进行应答

我希望有人能帮我解决这个奇怪的问题。在“干净”的服务器安装中,我有以下“website.com”和 nginx 配置,但如果您尝试转到 版本httpwww.website.com它会呈现默认nginx 页面,而不是像配置的那样转发到https版本。该站点使用 AWS Linux ami,并且位于 elb 后面(因此有 elb-check 指令)。

在我的/sites-available(并且ln -s已经/sites-enabled)中,我所拥有的(即使在执行时# ls -lah)是: default_server website.com elb-check

配置(以及nginx.conf下面的)。

提前谢谢您!如果您需要其他信息/配置,请告诉我。

website.com:

# Send http www. to https www.
    server {
    listen 80;
    server_name www.website.com;
    return 301 $scheme://www.website.com$request_uri;
    server_tokens off;

    }

# Send http non www. to https www.
    server {
    listen 80;
    server_name website.com;
    return 301 $scheme://www.website.com$request_uri;
    server_tokens off;

    }

# Send https non www. to https www. 
    server {
    listen 443 ssl;
    server_name website.com;
    return 301 $scheme://www.website.com$request_uri;
    server_tokens off;

        ssl_certificate "/path.to.crt";
        ssl_certificate_key "/path.to.key";
        ssl_dhparam "/etc/pki/nginx/dhparams.pem";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_protocols TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:!RC4:HIGH:!MD5:!aNULL:!EDH;
        ssl_prefer_server_ciphers on;

    }

# Answer https and www. requests

    server {
        listen  443 ssl;
        server_name www.website.com;
        index   index.html index.php;
        root    /home/website/html;
        access_log  /var/log/website/access.log;
        error_log   /var/log/website/error.log;
    server_tokens off;

        ssl_certificate "/path.to.crt";
        ssl_certificate_key "/path.to.key";
        ssl_dhparam "/etc/pki/nginx/dhparams.pem";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_protocols TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:!RC4:HIGH:!MD5:!aNULL:!EDH;
        ssl_prefer_server_ciphers on;

    location / {
        root    /home/website/html;
        try_files $uri $uri/ /index.php?$uri&$args;
        }

    location ~ /private\.php$ {
        auth_basic "Restricted Area";
        auth_basic_user_file /home/website/.htpasswd;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
        }

    }

nginx.conf:

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

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;


    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # Load Virtual Sites
    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/sites-available/*;

    index   index.php index.html index.htm;
    server_tokens off;
}    

默认服务器:

# To black-hole all other subdomain requests
server {
    listen 80;
    server_name _;
    return 444;
}

elb 检查:

# So the ELB sees the instance as still being alive
server {
    location /home/elb-check { 
    access_log off;
    return 200;
    add_header Content-Type text/plain;
    }
}

答案1

如果没有明确定义默认服务器,Nginx 将选择配置中的第一个服务器。安装一个虚拟默认服务器。

# This just prevents Nginx picking a random default server if it doesn't know which
# server block to send a request to
server {
  listen      80 default_server;
  server_name _;
  return 444;
  access_log off; log_not_found off;
}

您可能还想定义一个默认的 https 服务器,尽管如果它不在特定域上,它可能会生成证书警告。我不介意。

答案2

我认为另一个问题与 nginx.conf 有关,在底部您包含了 sites-enabled/*。您同时包含了可用配置和已启用配置。您加载了两次配置,因此 nginx 在找不到有效配置时将始终默认使用基本配置。

# Load Virtual Sites
    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/sites-available/*; <-- do not need this too.

相关内容