nginx 没有 server_name 并且只使用静态 IP 地址?

bas*_*sh- 44 ubuntu nginx gunicorn

这是我的第一个 Web 应用程序部署,并且遇到了各种问题。

我目前正在为 Django 应用程序寻找 nginx + gunicorn 实现,但这个问题主要与 nginx 配置有关。对于某些情况 - nginx 将接收到 gunicorn 本地服务器的连接和代理。

在 nginx 配置中,它说server_name我必须提供一个吗?我不打算使用任何类型的域名,只是通过我网络的外部 ip(它是静态的)和要监听的端口号。

我的愿望是当我访问类似的东西时,http://xxx.xxx.xxx.xxx:9050我将能够获得该网站。

以下是我将基于配置的示例代码以供参考。

   server {
        listen   80;
        server_name WHAT TO PUT HERE?;

    root /path/to/test/hello;

    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /admin/media/ {
        # this changes depending on your python version
        root /path/to/test/lib/python2.6/site-packages/django/contrib;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
        # what to serve if upstream is not available or crashes
        error_page 500 502 503 504 /media/50x.html;
     }
Run Code Online (Sandbox Code Playgroud)

Sha*_*den 38

server_name默认为空字符串,这很好;你可以完全排除它。

“我不想为此命名”需要的另一种常见方法是使用 server_name _;

但是,您的http://xxx.xxx.xxx.xxx:9050URL 不适用于此配置;你只监听端口 80。你还需要添加一个listen 9050;


Joe*_*Joe 7

服务器名称 _; 不是通配符,请参见此处:

http://blog.gahooa.com/2013/08/21/nginx-how-to-specify-a-default-server

只需为 ip-only 访问指定 default_server 指令(请参阅http://nginx.org/en/docs/http/request_processing.html

server {
    listen 1.2.3.4:80 default_server;
    ... 
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您“没有时间”在相关信息中添加额外的行,老实说,我不知道您为什么要费心用已接受的答案来回答 3 年前的帖子! (3认同)
  • 不鼓励简单地链接到有用的信息而不将其包含在您的答案中 - 在这种情况下,`server_name _;` 是不匹配的。 (2认同)
  • 两者并不相等;不匹配是当没有其他匹配时提供的服务,而通配符匹配所有内容。然而,这里的要点是,有用的信息应该在答案中,而不是链接到其他网站(无论在该网站上展示得多么好),详细信息 [此处](http://serverfault.com/help/how-to-回答) (2认同)