如何使用 Vagrant 在一个 NGINX 上设置多个网站?

如何使用 Vagrant 在一个 NGINX 上设置多个网站?

我有由 Vagrant 管理的虚拟 ubuntu 服务器。我安装了 NGINX 并创建了 3 个虚拟服务器。它们都有名称:test1.localhost、test2.localhost、test3.localhost。当我尝试浏览我的测试网站时,一切都很好,但只有当我在虚拟机中打开它们时才会这样(lynx test1.localhost、lynx ......等)。

为什么我无法在主机上查看我的网站?为什么当我输入test1.localhost:8080test2.localhost:8080或 时test3.localhost:8080,会出现错误“未找到服务器?”。

  • 当我输入时,127.0.0.1:8080我看到的是 test1.localhost 网站。

编辑

  • 我刚刚发现我的配置在 Chromium 浏览器和 Google Chrome 上运行正常!为什么 Firefox 无法显示test1.localhost:8080test2.localhost:8080test3.localhost:8080Chromium 浏览器可以?
  • 好的,我找到了,这是 Firefox 的问题:https://bugzilla.mozilla.org/show_bug.cgi?id=1433933

测试1.conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # include snippets/snakeoil.conf;

        root /var/www/test1;
        index index.html index.htm index.nginx-debian.html;
        server_name test1.localhost;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

测试2.conf

server {
        listen 80;
        listen [::]:80;
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # include snippets/snakeoil.conf;
        root /var/www/test2;
        index index.html index.htm index.nginx-debian.html;
        server_name test2.localhost;
        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

测试3.conf

server {
        listen 80;
        listen [::]:80;

        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # include snippets/snakeoil.conf;

        root /var/www/test3;
        index index.html index.htm index.nginx-debian.html;
        server_name test3.localhost;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

/etc/hosts

127.0.0.1       localhost test1.localhost test2.localhost test3.localhost
127.0.1.1       vagrant

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Vagrant文​​件

...
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # nginx
...

答案1

http 的工作方式如下:http://example.com:8080/abc?def进入浏览器栏,后台的url被切割成四个部分:

  1. http:
  2. example.com
  3. 8080
  4. /abc?def

粗略地说,1 2 3 4 部分的用法如下:

$ telnet  2  3
(no TLS negotiation here, because 1)
GET 4    HTTP/1.1
Host: 2:3      <---- look here
User-agent: blah-blah
Accept: blah blah

----------------------

因此 nginx 知道您想要的是test1.localhost:8080而不是test1.localhost:80。不要在 TCP 层上更改 http 端口。您需要使用proxy_pass来执行此操作。

相关内容