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

如何在Ubuntu服务器将不同域名通过80端口转发至本地不同端口?

解决Ubuntu服务器多域名反向代理到不同本地端口的问题

Hey there! This is such a common use case, and Nginx is the go-to tool for setting up reverse proxying like this on Ubuntu. Let me walk you through the entire process step by step:

1. 安装Nginx

First, update your package list and install Nginx:

sudo apt update && sudo apt install nginx

Once installed, start Nginx and set it to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

2. 创建虚拟主机配置文件

You'll need a separate configuration file for each domain. Let's start with example.com:

配置example.com指向localhost:9000

Create a new config file in Nginx's sites-available directory:

sudo nano /etc/nginx/sites-available/example.com

Paste the following content into the file (replace the details as needed):

server {
    listen 80;
    server_name example.com www.example.com; # Include the www subdomain if needed

    location / {
        proxy_pass http://localhost:9000;
        # Pass important request headers to the backend service
        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;
    }
}

Save and exit the editor (for nano: Ctrl+O, press Enter, then Ctrl+X).

配置deneme.com指向localhost:6555

Repeat the same process for your second domain:

sudo nano /etc/nginx/sites-available/deneme.com

Use this config, adjusting the proxy port:

server {
    listen 80;
    server_name deneme.com www.deneme.com;

    location / {
        proxy_pass http://localhost:6555;
        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;
    }
}

3. 启用配置文件

Nginx reads configs from the sites-enabled directory, so we'll create symbolic links to our new files:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/deneme.com /etc/nginx/sites-enabled/

4. 验证配置并重启Nginx

Always check for syntax errors before restarting:

sudo nginx -t

If you see "test is successful", restart Nginx to apply the changes:

sudo systemctl restart nginx

5. 配置DNS记录

Don't forget to point both domains to your Ubuntu server's public IP address! You'll need to create an A record (or AAAA for IPv6) for each domain in your DNS provider's dashboard.

额外注意事项

  • Firewall: Make sure port 80 is open on your server's firewall. If you're using UFW, run:
    sudo ufw allow 'Nginx HTTP'
    
  • HTTPS (Optional): If you want to use HTTPS (port 443), you can get free SSL certificates using Certbot. It will even auto-configure Nginx for you!
  • Backend Services: Ensure that Soft1 and Soft2 are actually running on localhost:9000 and localhost:6555 respectively, and that they're accessible from the server itself.

内容的提问来源于stack exchange,提问作者Muhammed Ali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:47:34