大网数据广告活动
服务器运维问答

How to get nginx to return 404 unless a specific domain name is requested?

· 52CE 我爱测

I have a nginx server and I'd like to return 404 when anything.mydomain.com is requested except when anything is www , mypythonapp1 or mypythonapp2 I created a 00-noredirect file in /etc/nginx/sites-enabled : server { listen 80; …

问题原文

原帖链接 · nicoco · CC BY-SA 3.0

I have a nginx server and I'd like to return 404 when anything.mydomain.com is requested except when anything is www, mypythonapp1 or mypythonapp2

I created a 00-noredirect file in /etc/nginx/sites-enabled:

server {
    listen 80;
    listen 443 ssl;

    server_name _;

    return 404;
}

Unfortunately, everything gets redirect to mypythonapp1.

The other server blocks are as follow:

server {
    listen mypythonapp1.mydomain.com:80;
    server_name mypythonapp1.mydomain.com;
    (...) 
    listen mypythonapp1.mydomain.com:443 ssl; # managed by Certbot
    (...)
}

I think this might be related to certbot additions in my /etc/nginx/sites-enabled/mypythonapp1, even though I'm not sure:

(...)
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
(...)

Is there something I have to add to 00-noredirect to avoid this unwanted behaviour?

EDIT: as requested by @Tim, here's my -almost complete- nginx configuration:

server {
    listen 80;

    server_name mypythonapp1.mydomain.com;

    disable_symlinks off;

    location /static {
        alias /var/www/mypythonapp/mypythonapp/mypythonapp/static;
    }

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/var/run/uwsgi/app/mypythonapp1/socket;
    }


    listen 443 ssl; # managed by Certbot
    # some ssl magic here
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot



}
server {
    listen 80;

    server_name mypythonapp2.mydomain.com;

    disable_symlinks off;

    location /static {
        alias /var/www/mypythonapp/mypythonapp/mypythonapp/static;
    }

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/var/run/uwsgi/app/mypythonapp2/socket;
    }


    listen 443 ssl; # managed by Certbot
    # some ssl magic here
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot


}
server {
    listen 80;

    server_name www.mydomain.com mydomain.com;
    root /home/me/mystaticwebsite/;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    listen 443 ssl; # managed by Certbot
    # some ssl magic here
    if ($scheme != "https") {
        return 301 https://$host$uri;
    } # managed by Certbot




}
server {
    listen 80 default_server;

    server_name _;

    return 444;
}

# this last bit I commented out because it broke everything...

#server {
#    listen 443 default_server;
#
#    server_name _;
#
#    return 444;
#}

EDIT2: Examining error.log (why didn't I do that before...) I realized that the last part "broke everything" because no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking. I ended up copying the ssl_certificate and ssl_certificate_key lines that certbot added to the other domains. Now I still have a certificate warning when requesting anything.mydomain.com but at least if I bypass it, I don't access mypythonapp1 which is all that I really wanted in the first place.

已采纳解答

原帖链接 · Tim · CC BY-SA 3.0

Create server blocks that specify the server_name by domain names you want to service. Next create a default server to handle all others. You shouldn't return 404, return a more appropriate code.

server {
  # These can be in individual servers if you like
  server_name anything.mydomain.com www.mydomain.com etc.mydomain.com

  # add configuration
}


# 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; # "Connection closed without response"
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。