Назад | Перейти на главную страницу

Как настроить несколько веб-сайтов на одном NGINX с помощью Vagrant?

У меня есть виртуальный ubuntu-сервер под управлением Vagrant. Я установил NGINX и создал 3 виртуальных сервера. У всех них есть имена: test1.localhost, test2.localhost, test3.localhost. Когда я пытаюсь просмотреть свои тестовые веб-сайты, все в порядке, но только когда я открываю их на своей виртуальной машине (lynx test1.localhost, lynx ... и т. Д.).

Почему я не могу просматривать свои веб-сайты на хост-компьютере? Почему, когда я печатаю test1.localhost:8080 или test2.localhost:8080 или test3.localhost:8080, Я получаю сообщение об ошибке «Сервер не найден?».

редактировать

test1.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;
        }
}

test2.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;
        }
}

test3.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

Vagrantfile

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

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. Не меняйте порт http на уровне TCP. Вам нужно использовать proxy_pass сделать это.