一台服务器通过nginx配置多个域名(80端口)

1. 问题描述

多个域名对应一个服务器,为了避免域名后增加端口号,两个域名都需要占用80端口号,使用nginx来进行配置。

2. 解决方案

目前项目中,线上正在使用(100%可用)多域名对应一个服务器情况(线上ip及域名替换了下)

#user  nobody; worker_processes  1;  #error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info;  #pid        logs/nginx.pid;   events {     worker_connections  1024; }  http {     include       mime.types;     default_type  application/octet-stream;     sendfile        on;     keepalive_timeout  65;       server {         listen       80;        server_name  test1.hbusy.com;           location / {             root   html;             index  index_test1.html index_test1.htm;         }         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }     }            upstream visitip{         server 192.168.0.11:3107;      }         server {         listen       80;         server_name  test2.hbusy.com www.test2.hbusy.com;          location / {             proxy_pass   http://visitip;             proxy_set_header Host $host:$server_port;             proxy_set_header X-Real-IP $remote_addr;             proxy_set_header REMOTE-HOST $remote_addr;             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             client_max_body_size    2000m;              proxy_connect_timeout 3600;             proxy_send_timeout 3600;             proxy_read_timeout 3600;         }     } }  

3. 方案说明

其实主要分两步:

第一步就是配置域名对应ip地址;

第二就是在nginx中配置两个server(端口都为:80)。

您可能还会对下面的文章感兴趣: