2013-03-13 103 views
2

我刚刚安装了nginx,并且有多个域名指向同一个IP。当调用每个域时,我必须重定向到运行在同一台机器上的不同应用程序,每个应用程序都运行在不同的端口上。多个域名的nginx配置

对于离,我有app1.domain.comapp2.domain.com & app3.domain.com

所以,对于app1.domain.com我要重定向到localhost:<port1> 同样,app2.domain.com我要重定向到localhost:<port2>app3.domain.com我要重定向到localhost:<port3>

我该怎么办?

在此先感谢

回答

3

那么如果你的应用程序在不同端口上运行,则您的nginx的conf文件应该是这样的。

upstream app1 { 
     server 127.0.0.1:port1; #App1 
} 

upstream app2 { 
     server 127.0.0.1:port2; #app2 
} 

server { 
    listen  xxx.xxx.xxx.xxx:80; 
    server_name app1.domain.com; 

    access_log /var/log/nginx/log/app1.domain.com.access.log main; 
    error_log /var/log/nginx/log/app1.domain.com.error.log; 
    root /usr/share/nginx/html; 
    index index.html index.htm; 

    ## send request back to apache1 ## 
    location/{ 
    proxy_pass http://app1; 
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; 
    proxy_redirect off; 
    proxy_buffering off; 
    proxy_set_header  Host   $host; 
    proxy_set_header  X-Real-IP  $remote_addr; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 

server { 
    listen  xxx.xxx.xxx.xxx:80; 
    server_name app2.domain.com; 
    access_log /var/log/nginx/log/app2.domain.com.access.log main; 
    error_log /var/log/nginx/log/app2.domain.com.error.log; 
    root  /usr/local/nginx/html; 
    index  index.html; 

    location/{ 
     proxy_pass http://app2; 
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503   http_504; 
     proxy_redirect off; 
     proxy_buffering off; 
     proxy_set_header  Host   app2.domain.com; 
     proxy_set_header  X-Real-IP  $remote_addr; 
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 

如果您有任何疑问,请让我知道。 谢谢

+0

感谢您的即时回复,将作出相应的更改和保持更新。 – 2013-03-13 19:28:06

+0

感谢@tapasmishra它的工作像魅力 – 2013-03-13 20:15:05

+0

此外,配置后,它打开各个应用程序的不同领域的预期,但对于应用程序中的任何动作,如让我们说'登录'请求再次击中nginx,它发出404找到答案。请帮忙吗? – 2013-03-14 10:39:09