使用EC2上的nginx将域和子域指向不同的端口(app)

时间:2015-02-03 18:42:00

标签: nginx amazon-ec2 proxy cloudflare

设定:

带有弹性IP的EC2盒。

我有一个域example.com。我希望它映射到在端口3000上运行的nodejs应用程序。我有一个子域email.example.com,应该映射到端口3001

区域记录:

相关配置文件:

/etc/hosts 127.0.0.1 localhost 127.0.0.1 example.com 127.0.0.1 email.example.com

/etc/nginx/nginx.conf

worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;

    server {
        listen 80;

        server_name example.com www.example.com;

        location / {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
    }

    server {
        listen 80;

        server_name email.example.com;

        location / {
                proxy_pass http://localhost:3001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
    }

}

这不起作用。 example.com转到3000端口但email.example.com也转到端口3001

然后我尝试将server块移动到/etc/nginx/conf.d/example.com.conf/etc/nginx/conf.d/email.example.com.conf但结果相同。然后我尝试了/etc/nginx/sites-enables/example.com/etc/nginx/sites-enabled/email.example.com但同样的事情。

我不知道如何完成这项工作。在网上进行了很多研究并尝试了一些东西,但似乎没有任何工作。

0 个答案:

没有答案