【问题标题】:nginx one ipaddress but 2 sites served from subfoldernginx 一个 IP 地址,但从子文件夹提供 2 个站点
【发布时间】:2013-08-22 22:24:48
【问题描述】:

我已经成功配置了 nginx。使用默认站点它可以正常工作。 现在我有 2 个站点,一个位于 /home/bugz,另一个位于 /home/git/github/public。并且只有一个 ip 10.10.10.10(我没有 dns 设置,因此无法使用域名) 我希望在地点提供网站服务

http://10.10.10.10/bugz and http://10.10.10.10/github respectively

下面是两个配置文件

server {
  listen  *:80;
  server_name 10.10.10.10;
  server_tokens off;
  root /home/bugz;

   # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/bugzilla_access.log;
  error_log   /var/log/nginx/bugzilla_error.log;

  location /bugz {
      index  index.html index.htm index.pl;
  }


  location ~ \.pl|cgi$ {
      try_files $uri =404;
      gzip off;
      fastcgi_pass  127.0.0.1:8999;
      fastcgi_index index.pl;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include fastcgi_params;
      }
}

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

server {
#  listen *:80 default_server;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  listen *:80;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  server_name 10.10.10.10;     # e.g., server_name source.example.com;
  server_tokens off;     # don't show the version number, a security best practice
  root /home/git/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;
  }

  }

我如何做到这一点?

【问题讨论】:

  • 您在配置目录中使用sites-available 吗?
  • 是的站点可用在我的配置中使用

标签: nginx subdomain subdirectory


【解决方案1】:

您的nginx.conf 应该在http 块内包含类似这样的内容:

 include /etc/nginx/sites-enabled/*;

然后您将在/etc/nginx/sites-available 文件夹中拥有2 个配置文件。 (其中有来自sites-enabled 文件夹的符号链接。

每个 conf 都需要让它们监听不同的端口;即一个在 80 端口,一个在 81 端口

server1.conf

server {
    listen       80;
    server_name  localhost;

server2.conf

server {
    listen       81;
    server_name  localhost;

-或-

在 conf 文件中为每个服务器设置不同的servername 并使用 hosts 文件。

【讨论】:

  • 是的,我确实启用了 /etc/nginx/sites-available 并在启用了站点的正确符号链接。
  • 是的,我确实启用了 /etc/nginx/sites-available 并且在启用了站点中具有正确的符号链接。我不想在 2 diff 端口上有两个 diff 站点。他们在同一个端口 80 上工作。你能解释一下第二个选项吗?我怎样才能有不同的服务器名称?我只有一个接口 10.10.10.10 (没有配置 dns,因此不能使用主机名)当你说“玩主机文件”时,你有什么建议,你能解释一下吗,我是 nginx 的新手。在 apache 中很容易,将两个站点放在 /var/www/ 的 2 个文件夹下,就是这样。我们在 nginx 中没有类似的东西吗?
  • 如果您没有 DNS 设置,我认为这不会起作用。你需要告诉 ngnix 监听不同的主机名/服务器名。仅使用一个 IP 地址不足以支持同一端口上的 2 个站点。
  • @sparkbird - 如果您的 URL 请求始终将文件夹附加到末尾,您可能可以使用重写规则。但是,我没有太多重写 URL 的经验。
【解决方案2】:

我不明白为什么这么大的配置,我只会配置 2 个位置的 1 个站点

server {
    server_name 10.10.10.10;
    location /bugz {
        root /root/to/bugz;
        access_log  /var/log/nginx/bugzilla_access.log;
        error_log   /var/log/nginx/bugzilla_error.log;
        index  index.html index.htm index.pl;
        # try_files statement
    }
    location /git {
        root /home/git/gitlab/public;
        #access and error log and rest of config
    }
    location ~ \.pl|cgi$ { }
    location @gitlab { }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2020-06-10
    • 2017-05-19
    • 2016-04-22
    • 2015-10-23
    • 2014-04-04
    相关资源
    最近更新 更多