virtualbox 和 nginx 服务器名称

virtualbox 和 nginx 服务器名称

我正在尝试配置在 Windows7 主机上运行 Ubuntu 12.04 客户机的 gitlab。我可以使用端口转发通过 ssh 连接客户机,并使用端口重定向访问 nginx 服务器(主机中的 8888 是客户机中的 80,因此主机中的 localhost:8888 可以访问客户机中的 nginx 服务器),但 nginx 配置文件中的 server_name 给我带来了麻烦。nginx 会接受的正确 listen 和 server_name 是什么?

客户机的 NAT 接口为 10.0.2.15,Host-Only 接口为 192.168.56.101,静态。

谢谢!

编辑:由于主机使用静态 IP 地址,我无法为 Ubuntu 客户机使用桥接模式,因此我必须坚持使用仅主机和端口转发。这样,我可以访问客户机中的 nginx 服务器,但 server_name 仍然错误,因为我必须在 Windows 主机中使用 localhost:8888 并将其转发到客户机中的端口 80。server_name 应该是什么?

答案1

nginx 将查看 URL 中给出的名称以访问服务器并与服务器名称进行比较。您必须使用客户机主机名来访问它并将其用作服务器名称。另一种方法是使用本地 DNS 或hosts文件为您的客户机系统主机名(或您想要的任何 uri - 例如开发中的网站)提供正确的 IP,并使用此主机名或 uri 作为服务器名称。

要在客户机上使用服务器,我建议使用桥接模式而不是 NAT,这样您就不需要端口转发并避免很多小麻烦。(注意:我不是说您不能使用 NAT,但我发现使用桥接更容易)

答案2

谢谢你的帮助,laurent!

我最终在 nginx 中保留了这个配置:

upstream gitlab {
  server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}


server {
  listen 80 default;         # e.g., listen 192.168.1.1:80;
  #server_name thinkstation-ubuntu;     # e.g., server_name source.example.com;
  root /home/gitlab/gitlab/public;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/gitlab_access.log;
  error_log   /var/log/nginx/gitlab_error.log;

  location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @gitlab {
    proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://gitlab;
  }
}

并且成功了!最终配置如下:

Windows7主机:创建主机专用接口,ip 192.168.56.1,配置端口转发:主机8888到客户机80

Ubuntu 12.04 服务器客户机:nginx 正在运行,eth0 作为 VirtualBox NAT,eth1 为仅主机,静态 ip 为 192.168.56.101

Ubuntu 12.04 桌面:eth0 作为 Virtualbox NAT,eth1 为 Host-only,静态 ip 为 192.168.56.102,直接访问 192.168.56.101 nginx、ssh 及所有服务。

谢谢!

相关内容