【问题标题】:Is it bad practice to have multiple Unicorn App Servers with one Nginx reverse proxy server?拥有多个 Unicorn App Server 和一个 Nginx 反向代理服务器是不好的做法吗?
【发布时间】:2016-12-20 03:42:43
【问题描述】:

我有一个运行 Ubuntu 14.04 的 Linux 机器,内存约为 50GB。

我有 5 或 6 个 Ruby-on-Rails Web 应用程序,每个应用程序都有一个 Unicorn 应用服务器,全部由 Nginx 反向代理服务器提供服务。

每个应用都托管在一个子目录中。 例如:

  • www.webserver.com/app1
  • www.webserver.com/app2

每个应用每天可能收到 50-100 个请求。它们都是促进我公司业务流程的小应用程序。

我的 Nginx 配置文件如下所示:

upstream app1 {
    #path to Unicorn SOCK file;
}

upstream app2 {
    #path to Unicorn SOCK file;
}

upstream app3 {
    #path to Unicorn SOCK file;
}

# ...several more apps

server {
    listen 443 ssl;
    access_log #path;
    error_log #path;

    ssl_certificate #path;
    ssl_certificate_key #path;

    add_header X-UA-Compatible "IE=Edge,chrome=1";

    root /srv/apps/app1/public;

    location /app1 {
            proxy_pass http://app1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

    location /app2 {
            proxy_pass http://app2;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

    location /app3 {
            proxy_pass http://app3;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }

     # ...several more apps

}

这个设置已经运行了一年左右没有问题,但我有一种唠叨的感觉,我做这一切都错了......

如果我继续添加应用程序,我会遇到问题吗?有没有更好的方法来做到这一点?

更新:

“问题”是指:

  • 静态资源路径冲突?
  • 内存问题?即,使用超过我需要完成相同的行为?

我的意思是“一种更好的方法”:

  • 除了通过解析出 URL 中的子目录名称向相关独角兽服务器发送请求
  • 我应该使用单个 Nginx 反向代理来服务多个应用程序吗?

【问题讨论】:

    标签: nginx unicorn


    【解决方案1】:

    对于不同应用的相同配置,可以使用include指令。

    例如,使用以下内容创建名为 /etc/nginx/global_proxy.conf 的文件:

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    

    nginx.conf 你的 /appX 部分:

    location /appX {
            proxy_pass http://appX;
            include /etc/nginx/global_proxy.conf;
    }
    

    为了提高您的安全性,我建议您添加 dhparam,并将其添加到 SSL 配置中:

    # SSL :
    # drop SSLv3 (POODLE vulnerability)
        ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
    # Recommanded ciphers
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
    # Diffie–Hellman key exchange (D–H)
        ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    # config to enable HSTS(HTTP Strict Transport Security)
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    # force timeouts if one of backend is died
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    

    生成 dhparam.pem 文件:

    openssl dhparam -out dhparam.pem 4096

    【讨论】:

    • 感谢您提供有关如何通过将代理配置包含在单独的文件中来稍微压缩文件的提示。但是,我刚刚更新了我的问题,所以它更清楚了......我真正想要的是使用单个 Nginx 反向代理的多个应用程序的一般做法是否是一个好方法,如果不是,为什么?
    猜你喜欢
    • 2020-09-22
    • 2016-09-14
    • 1970-01-01
    • 2020-09-29
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2014-03-17
    相关资源
    最近更新 更多