繁体   English   中英

如何让 Puma 和 Nginx 在 Rails 生产中运行?

[英]How to get Puma and Nginx to run in Rails production?

与此搏斗数月,每周都绽放出另一条新的职业道路,看来,我低头了。

所以,就这么说了。 这是我最近的一次。 我让它工作了好几次,但它太脆弱了,因为我更像是一个开发人员而不是一个 devops (?) 人。

我正在运行 Ubuntu 20.04。

be puma -C config/puma.rb config.ru -e production \
  --pidfile /run/puma.pid \
  --control-url 'unix:///root/mysite/tmp/sockets/mysite-puma.sock' \
  --control-token 'app' \
  --state tmp/puma.state \
  -b 'tcp://mysite.com'

我可以这样运行pumactlbundle exec pumactl -T 'app' -C 'unix:///root/mysite/tmp/sockets/mysite-puma.sock' -S tmp/puma.state [pumactl switch]

我的 nginx 配置。

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;

pid /run/nginx.pid;

include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 1024;
    multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

        sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # 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 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip off;

    gzip_vary off;
    #gzip_proxied any;
    #gzip_comp_level 6;
    #gzip_buffers 16 8k;
    #gzip_http_version 1.1;
    #gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/mysite.com;
}

/etc/nginx/sites-enabled/mysite.com

# This configuration uses Puma. If using another rack server, substitute appropriate values throughout.
upstream puma {
  server unix:///root/mysite/tmp/sockets/mysite.sock;
}

# We need to be listing for port 80 (HTTP traffic). 
# The force_ssl option will redirect to port 443 (HTTPS)
server {

  # Update this
  server_name mysite.com www.mysite.com;

  # Don't forget to update these, too.
  # For help with setting this part up, see:
  # http://localhost:4000/2018/09/18/deploying-ruby-on-rails-for-ubuntu-1804.html
  root        /root/mysite/public;
  access_log  /root/mysite/log/nginx.access.log;
  error_log   /root/mysite/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;

  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

}

  # This is the configuration for port 443 (HTTPS)
  server {

    listen [::]:443 ssl ipv6only=on; # managed by Certbot

    server_name mysite.com www.mysite.com;
    
    ssl_certificate       /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key   /etc/letsencrypt/live/mysite.com/privkey.pem;   # managed by Certbot
    include               /etc/letsencrypt/options-ssl-nginx.conf;          # managed by Certbot
    ssl_dhparam           /etc/letsencrypt/ssl-dhparams.pem;                # managed by Certbot

    # Don't forget to update these, too.
    # I like to update my log files to include 'ssl' in the name.
    # If there's ever any need to consult the logs, it's handy to have HTTP and HTTPS traffic separated.
    root        /root/mysite/public;
    access_log  /root/mysite/log/nginx.ssl.access.log;      # Updated file name
    error_log   /root/mysite/log/nginx.ssl.error.log info;  # Updated file name

    error_page 500 502 503 504 /500.html;
    client_max_body_size 10M;   
    
    location ^~ /assets/ {
      gzip_static off;
      expires max;
      add_header Cache-Control public;
    }

    try_files $uri/index.html $uri @puma;
    location @puma {
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       # This is an important line to help fix some redirect issues.
       proxy_set_header X-Forwarded-Proto https; 
    
       proxy_set_header Host $http_host;
       proxy_redirect off;

       proxy_pass http://puma;
     }
  }

  # If you chose Certbot to redirect all traffic to HTTPS, this will be in your current config. 
  # Remove it or you'll run into redirection errors:
  server {
   if ($host = example.com) {
      return 301 https://www.example.com$request_uri;
   } # managed by Certbot
 
 
    listen [::]:80 default_server deferred;
    server_name example.com;
    return 404; # managed by Certbot
 
}

首先,在您的/etc/nginx/sites-enabled/mysite.com

第一步

改变

upstream puma {
  server unix:///root/mysite/tmp/sockets/mysite.sock;
}

upstream puma {
  server 0.0.0.0:9838; # port number in which your puma server starts
}

更改/etc/nginx/sites-enabled/mysite.com后应该如下所示。

upstream puma {
  server 0.0.0.0:9838;
}

server {
    server_name  mysite.com www.mysite.com;
    client_max_body_size 200m;
    gzip             on;
    gzip_comp_level  4;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain application/javascript application/json application/x-javascript text/xml text/css application/xml text/javascript;

    root /root/mysite/public;

    location / {
      try_files $uri/index.html $uri @app;
    }

    location  ~* ^/assets {
      root /root/mysite/public;
      expires 1y;
      add_header Cache-Control public;
      add_header Last-Modified "";
      add_header ETag "";
      break;
    }

    error_page 500 502 503 504 /500.html;

    location @app {
      proxy_pass http://puma;

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header Host $http_host;
      proxy_redirect off;
    }

    location ~ /.well-known {
      allow all;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


server {
    if ($host = mysite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = www.mysite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name  mysite.com www.mysite.com;
    listen 80;
    return 404; # managed by Certbot
}

第二步

然后运行gem install foreman安装工头库。 要了解有关工头的更多信息,请单击此处

第三步

在您的项目根目录中创建Procfile并粘贴以下内容

web: RAILS_ENV=production bundle exec puma -e  production -p 9838 -d -S  ~/puma -C config/puma.rb

最后一步

运行foreman start启动 puma 服务器,出现 go,您将能够看到您的应用程序正在运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM