docker - 我怀疑nginx是否在不知道还要做什么的情况下放 “Welcome to Nginx!”?

标签 docker nginx proxy

我正在调试一个简单的(Docker)代理服务器,据我所知,该服务器没有“默认网站”或类似的网站。我认为它正在从上游获取302响应,但我还不知道为什么。但是有趣的是,我得到了“欢迎使用Nginx!”即使我认为没有网站文件可以实际生成它,也没有任何理由去这样的地方。

那么……nginx有时会“独立地”产生该响应吗?如果是这样,如果有人可以告诉我“在什么情况下”,这将极大地帮助我进行故障排除。如果这是一个线索,我想了解那个线索...

最佳答案

实际上,这是Nginx Web服务器的default index.html。如果您旋转一个普通的Nginx容器并在其中进行连接,您甚至可以在文件系统上看到这些文件。例如:

$> docker run --name nginx -d nginx
98da5173df23ea4690b9ce8bda87d844775c77609905f76b542115e4babcdcfa

$> docker exec -it nginx sh

$> ls /usr/share/nginx/
html

$> ls /usr/share/nginx/html
50x.html  index.html

$> cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

那么Nginx如何解释该文件?好吧,除非您将自己的/etc/nginx/nginx.conf文件批量安装到容器中,否则Nginx进程将使用原始容器开发人员添加的默认文件(引用上述index.html文件)。我们也可以跟踪以下内容:
$> ls /etc/nginx/
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  uwsgi_params  win-utf

$> cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

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

因此,如果我们看这里,就会发现一件事。文件的最后一行说:include /etc/nginx/conf.d/*.conf。这意味着Nginx在完成读取此配置文件后,应继续递归处理在*.conf目录中存在的任何/etc/nginx/conf.d文件中找到的所有配置信息。

因此,让我们看一下该目录:
$> ls /etc/nginx/conf.d/
default.conf

$> cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

我们在这里看到什么?好吧,大多数情况下,几乎整个配置文件都会被注释掉。让我们删除它们以使其更易于阅读和理解。
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

我们可以在这里看到,容器开发人员默认将Nginx定义为将端口80接收到的所有流量路由到/usr/share/nginx/html(如果它在/上输入)或/usr/share/nginx/html(如果在任何其他路径上输入)。正如我在前面的文章STDOUT生成的cat中所展示的那样,这些文件的内容就是您在浏览器中看到的。

希望对您有所帮助,让我知道您是否还有其他疑问!

关于docker - 我怀疑nginx是否在不知道还要做什么的情况下放 “Welcome to Nginx!”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903363/

相关文章:

performance - 如何正确地 docker 化和连续集成20GB原始数据?

docker - docker 镜像的默认命令

nginx - 高负载下502网关错误(nginx/php-fpm)

caching - 如果 Cache-Control 有 `no-cache` 和 `max-age=900` 会发生什么?

java - java.lang.reflect.Proxy 实例是否经过特殊处理以进行终结?

Docker Compose 不会启动 Selenium Grid

docker - 显示UJ的Dockers Cassandra节点

nginx - 如果我的 nginx 配置错误,如何更正?

select - 使用 select、epoll 或 kqueue 服务大文件

android - 更改 android studio 的 https 代理设置