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

5

我为此奋斗了好几个月,每周都有新的职业道路出现,似乎我一筹莫展。

话虽如此,我已经接近成功。我曾多次使其运行正常,但由于我更像一个开发者而非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'

我可以这样运行pumactl

bundle 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
 
}

1个回答

7

首先,在您的/etc/nginx/sites-enabled/mysite.com文件中:

第一步:

更改:

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

to

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来安装foreman库。要了解更多关于foreman的内容,请点击这里

第三步

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

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

最后一步

运行foreman start启动Puma服务器,然后你就可以看到你的应用程序正在运行了。


那个没起作用。"已经在那个端口运行。" - listenlight
1
此外,没有“-d”选项来进行守护进程化(但是Foreman会处理这个问题)。 - listenlight
1
你需要稍微修改一下你的 /etc/nginx/sites-enabled/mysite.com 文件。我会很快更新答案。 - Tashi Dendup
1
@tidelake 请检查更新后的答案。如果您仍然遇到问题,请告诉我。 - Tashi Dendup
1
为什么要从套接字更改为端口号? - Stephane Paquet
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接