nginx如何映射子域名?


我现在一台主机上运行了几个web服务,目前通过端口进行区分,想通过子域名对外提供服务地址。

我在nginx的nginx.conf中做了如下配置:


 server {
        listen       80;
        server_name  abc.xxx.com;

        location / {
                  proxy_pass http://127.0.0.1:84;
                  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_redirect off;
        }

想把本地的服务"http://127.0.0.1:84"通过子域名"abc.xxx.com"的80端口进行访问?
但是通过访问"abc.xxx.com"貌似只访问到当前IP地址和默认的80端口。

求教:如何使用nginx进行子域名以及端口映射?

nginx 域名

刚出锅的包子 9 years, 7 months ago

和server平级的多配置几个server即可,server_name域名和listen的端口你按需设置


 server {
    listen       80;
    server_name  abc.xxx.com;

    location / {
          proxy_pass http://127.0.0.1:84;
          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_redirect off;
    }
}

server {
    listen       84;
    server_name  xyz.xxx.com;

    location / {
        # another config
    }
}

绫波丽X明日香 answered 9 years, 7 months ago

Your Answer