【问题标题】:How to Optimize and Scale Ruby on Rails App with Nginx and Passenger如何使用 Nginx 和 Passenger 优化和扩展 Ruby on Rails 应用程序
【发布时间】:2017-12-09 00:12:18
【问题描述】:

我有一个正在生产的应用程序,它使用 Ruby on RailsNginxPassenger,开始时性能很好,但随着时间的推移(一个小时或更长时间后)它会变慢,我需要重新启动 Nginx 以再次提高其性能。我可以知道如何使其更加优化和可扩展,并避免不断重启。谢谢,伙计们非常需要你的帮助。

【问题讨论】:

    标签: ruby-on-rails nginx passenger


    【解决方案1】:

    您可以进行一些设置来优化您的Nginx 服务器。

    如果您的服务器有root access 并且正确添加了Nginx,您可以考虑进行一些更改。

    1) 运行sudo vim /etc/nginx/nginx.conf 并根据您的需要考虑这些设置(不要将整个代码替换到您的服务器中,因为这里没有一些默认设置。只需添加您需要的)

    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;
    
    events {
            worker_connections 768;
            multi_accept on;
            use                 epoll;
    }
    
    worker_rlimit_nofile 40000;
    
    http {
    
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            client_body_timeout 500s;
            client_header_timeout 500s;
            keepalive_timeout 500s;
            send_timeout 300s;
            types_hash_max_size 2048;
            # server_tokens off;
            client_max_body_size 100000M;
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ##
            # SSL Settings
            ##
    
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
    
            access_log off;
            #access_log /var/log/nginx/access.log;
            #error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings
            ##
    
            gzip on;
            gzip_disable "msie6";
                    gzip_vary on;
            # gzip_proxied any;
            gzip_comp_level 2;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            gzip_min_length  10240;
            gzip_proxied     expired no-cache no-store private auth;
            gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
            gzip_disable "MSIE [1-6]\.";
    
    }
    

    您可以根据自己的应用和需求更改数字。

    2) 运行sudo vim /etc/nginx/sites-enabled/default 并根据您的需要考虑这些设置(不要将整个代码替换到您的服务器中,因为这里没有一些默认设置。只需添加您需要的)

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
    
            passenger_enabled on;
    
            # redirect server error pages to the static page /50x.html
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        location ~*  \.(jpg|jpeg|png|gif|ico)$ {
            access_log        off;
            log_not_found     off;
            expires 30d;
        }
    
        location ~*  \.(js)$ {
            access_log        off;
            log_not_found     off;
            expires 150d;
        }
        location ~*  \.(css)$ {
            access_log        off;
            log_not_found     off;
            expires 150d;
        }
    }
    

    您可以根据自己的应用和需求更改数字。

    更改后记得restart ngnix

    再次注意添加或删除的内容,并再次不要复制并粘贴整个代码,因为这里没有一些默认设置。

    另一件事是查看您的logstmp 文件/文件夹,因为您可以消除日志以减少内存和空间。你也可以看看tmp文件夹和设置。

    您也可以考虑(推荐)在您的视图中进行缓存。如您所知,rails 有 Fragment and Russian doll caching。缓存总是有助于提高响应时间。

    如果您不熟悉缓存,还有一种叫做渐进式渲染的东西。您可以使用gem 'progressive_render'watch this toturial

    希望对你有帮助。

    【讨论】:

    • 没问题。如果这对您有帮助,如果您支持并接受作为您的答案,我将不胜感激
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2019-04-12
    • 2015-07-16
    • 2013-06-13
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多