多个域名的nginx配置

标签 nginx

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

例如,我有 app1.domain.com , app2.domain.com & app3.domain.com

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

我该怎么办?

提前致谢

最佳答案

如果您的应用程序在不同的端口上运行,那么您的 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;
    }
}

如果您有任何疑问,请告诉我。 谢谢

关于多个域名的nginx配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15392926/

相关文章:

docker - Traefik:使用Nginx为Django应用程序提供静态内容

Nginx:正确设置压缩样式/脚本的 MIME 类型

ubuntu - Cloudflare nginx 服务器 nodejs 应用 SSL 错误

PHP Flush 有效……即使在 Nginx 中

javascript - key 斗篷 : Redirection loop using javascript adapter behind nginx

nginx - Docker 登录仅适用于守护进程,但不适用于 Docker 事件服务

nginx 和星号 server_name?

php - 全新安装时出现 "Error in exception handler"

ssl - 服务器和客户端证书必须由 SSL 中的同一 CA 签名

c - 修改nginx Web服务器的源代码