nginx on vagrant:请求未到达虚拟主机/服务器块

时间:2017-01-14 21:49:17

标签: nginx vagrant virtualhost reverse-proxy portforwarding

我目前在流浪汉设置上运行nginx实例。我试图通过为他们设置虚拟主机/服务器块来让我的nginx运行两个不同的URL dev.v1.example.com和dev.v2.example.com

流浪汉端口转发工作正常,我可以从我的主机到达nginx

config.vm.network "forwarded_port", guest: 80, host: 8980

当然,这意味着我可以在主机上的浏览器上访问它们,我必须输入dev.v1.example.com:8980,例如

guest虚拟机上的nginx.conf是您的标准默认nginx.conf

    # For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

我在conf.d / vhost.conf中有以下文件

server {
    listen 80;
    server_name dev.v1.example.com;
    root   /vagrant/dev_v1;
}

server {
    listen 80;
    server_name dev.v2.example.com;
    root   /vagrant/dev_v2;
}

每当我从主机请求dev.v1.example.com:8980或dev.v2.example.com:8980时,我从不点击服务器块并始终获取默认的nginx启动页面是默认服务器块指向的内容。它永远不能在各自的根文件夹中显示每个特定服务器名称的内容,但我认为服务器块会受到给定配置的命令的影响。

我已经尝试将vhost.conf中的监听端口更改为8980,并且它不能与更改服务器名称以附加8980端口一起使用,该端口始终不会更改任何内容,我可以看到我正在制作的请求中的“主机”标题是dev.v1.example.com:8980,这也是流浪客人的nginx日志中出现的内容

有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

在主机上创建一个具有两个上游的反向代理:

upstream dev1__upstream {
		 server 192.168.33.2:80;
		 keepalive 64;
		 }
upstream dev2__upstream {
		 server 192.168.33.3:80;
		 keepalive 64;
		 }

只需使用代理传递创建vhost:

server {
  listen 80;
  server_name  dev1.local;
  access_log /var/log/nginx/dev1.access.log;
  error_log /var/log/nginx/dev2.error.log;

  location / {
      proxy_pass http://dev1__upstream;
    }
}