nginx 学习:不同的域名指向服务器的不同的端口

nginx proxy subdomains to other addresses and ports [closed]
更新于: 2022-02-21 10:04:19

先看一下需求

  1. 有几个不同的域名
  2. 需要对应宿主机(本机)的 127.0.0.1 的不同端口上
My domain.com has IP y.y.y.y and accepts requests on port 80
My subdomains are:
- admin.domain.com -> I need to proxy to x.x.x.x:3434
- user.domain.com -> I need to proxy tox.x.x.x:3435
- vendor.domain.com -> I need to proxy to x.x.x.x:3436

nginx 配置如下

server {
    listen 80;
    server_name admin.domain.com;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3434;
        proxy_redirect off;
    }
}
server {
    listen 80;
    server_name user.domain.com;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3435;
        proxy_redirect off;
    }
}
server {
    listen 80;
    server_name vendor.domain.com;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3436;
        proxy_redirect off;
    }
}

参考