You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Vagrant在同一主机部署两个不同域名的站点

Hey there! Great question—let's break this down step by step so you can get both sites up and running with clean, custom domains, even when they’re on separate Vagrant VMs and both using port 80.

1. Configuring Independent Custom Domains

For local development, you don’t need a public domain registrar—just edit your system’s hosts file to map your custom domains to the private IPs of your Vagrant VMs. Here’s how:

  • On Windows: Edit C:\Windows\System32\drivers\etc\hosts (run your text editor as admin)
  • On macOS/Linux: Edit /etc/hosts (use sudo to gain write access)

Add these lines (replace the IPs with whatever private IPs you assign to your VMs):

192.168.33.10  laravel.local
192.168.33.11  dotnet.local

This tells your computer to route requests for laravel.local to your Laravel VM, and dotnet.local to your .NET Core VM.

2. Can Both Domains Point to the Same IP?

Yes—if you want both domains to resolve to your host machine’s IP (instead of separate VM IPs), but you’ll need a reverse proxy to route traffic correctly. This is useful if you want to avoid typing port numbers when accessing the sites.

Without a proxy, pointing both domains to the same IP would mean only one VM’s port 80 could be mapped to your host’s port 80. The reverse proxy fixes this by acting as a traffic cop on your host.

3. Port Forwarding for Both Apps on Port 80

Each VM can run its app on port 80 natively—you just need to map them to different ports on your host machine first, then use a reverse proxy to hide those port numbers.

Step 1: Configure Vagrant Port Forwarding

Update each VM’s Vagrantfile to forward unique host ports to the VM’s port 80:

Laravel VM Vagrantfile snippet:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64" # Use your preferred box
  config.vm.network "private_network", ip: "192.168.33.10"
  # Map host port 8080 to VM port 80
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

.NET Core VM Vagrantfile snippet:

Vagrant.configure("2") do |config|
  config.vm.box = "mcr.microsoft.com/dotnet/core/sdk:6.0" # Use your preferred box
  config.vm.network "private_network", ip: "192.168.33.11"
  # Map host port 8081 to VM port 80
  config.vm.network "forwarded_port", guest: 80, host: 8081
end

Step 2: Add a Reverse Proxy (No More Port Numbers!)

To access both sites on port 80 without appending port numbers, install Nginx on your host machine (or use a third Vagrant VM dedicated to proxying). Here’s how to set it up on your host:

Laravel Site Nginx Config (save as laravel.local in your Nginx sites directory):

server {
    listen 80;
    server_name laravel.local;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

.NET Core Site Nginx Config (save as dotnet.local):

server {
    listen 80;
    server_name dotnet.local;

    location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        # .NET Core-specific settings to prevent connection issues
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable the Configs and Reload Nginx:

# Linux/macOS
sudo ln -s /etc/nginx/sites-available/laravel.local /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/dotnet.local /etc/nginx/sites-enabled/
sudo nginx -t # Test config validity
sudo systemctl reload nginx

Now you can visit laravel.local and dotnet.local in your browser without any port numbers—Nginx routes the traffic to the correct VM automatically.

4. Do You Need Extra Tools?
  • Basic setup (with port numbers): No extra tools needed—Vagrant’s built-in port forwarding and your system’s hosts file are enough.
  • Clean, portless access: Yes, you’ll need a reverse proxy like Nginx (most common), Caddy (simpler, auto-HTTPS ready), or Traefik (great for containerized setups). Nginx is the most straightforward choice for this use case.
5. Alternative: Proxy VM for Complete Isolation

If you don’t want to install software on your host machine, spin up a third Vagrant VM to act as the reverse proxy. Assign it a private IP, map its port 80 to your host’s port 80, and configure its Nginx to forward requests to the other two VMs’ private IPs (instead of host ports). This keeps all infrastructure isolated in VMs.

内容的提问来源于stack exchange,提问作者Jaka Kordež

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:39:57