I have an app where I was using a database which was located in the same server (DigitalOcean Droplet) as an app. So in my Rails app it was:
config/database.yml:
production:
<<: *default
database: example_production
username: rails
password: <%= ENV['EXAMPLE_DATABASE_PASSWORD'] %>
And it was working. Now I want a dedicated server for database. DigitalOcean released Databases clusters for it. So I created it.
My config/database.yml I changed to:
production:
url: <%= ENV["DO_DATABASE"] %>
I've made rails db:migrate and db:seed. And it worked. When I check database content then everything is inside as it should. So I have a connection to db.
But, after my app restart, when I go to my web page I receive:
502 Bad Gateway nginx/1.14.0 (Ubuntu)
After a while it changes to:
503 Service Unavailable No server is available to handle this request.
My server error log looks like:
2019/02/18 23:33:06 [error] 32636#32636: *1267 connect() failed (111: Connection refused) while connecting to upstream, client: some_IP_which_doesnt_matter, server: _, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:3000/", host: "cloud.digitalocean.com"
So: even if I could connect to remote db to migrate and seed it, I cannot access it by webpage. When I come back (for a while) to internal database then page works.
I don't know if it is about server configuration or it's database configuration (in DigitalOcean database cluster I cannot reach configuration files, I have only an direct access to postgresql).
At database DigitalOcean level settings are regular, so: There is my droplet added to "ALLOWED INBOUND SOURCES". Basically there is few options to set-up in DigitalOcean Database panel. It looks like that it should simply work "like a charm" when you connect to it.
I was trying a lot of with NGINX 'server block' for my app of my droplet. Now the file /etc/nginx/sites-available/rails looks like that:
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /home/rails/model_app/public;
server_name _;
index index.htm index.html;
location ~ /.well-known {
allow all;
}
# From https://object.io/site/2015/rails-nginx-easy-assets
#
# Cache forever publicly: files for generated assets
# /assets/application-2565b50fc38a0b3a44882faa3e936262.css
#
# This setup means a CDN may cache these files
location ~ "^/assets/.+-[0-9a-f]{32}.*" {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
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 X-Forwarded-Port 443;
proxy_set_header X-Client-Verify SUCCESS;
proxy_set_header X-Client-DN $ssl_client_s_dn;
proxy_set_header X-SSL-Subject $ssl_client_s_dn;
proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
}
}
My firewall setup:
Status: active
To Action From
-- ------ ----
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
22/tcp LIMIT Anywhere
3000 ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH ALLOW Anywhere
25060 ALLOW Anywhere
25061 ALLOW Anywhere
25060/tcp ALLOW Anywhere
25061/tcp ALLOW Anywhere
53 ALLOW Anywhere
53/tcp ALLOW Anywhere
53/udp ALLOW Anywhere
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
22/tcp (v6) LIMIT Anywhere (v6)
3000 (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
25060 (v6) ALLOW Anywhere (v6)
25061 (v6) ALLOW Anywhere (v6)
25060/tcp (v6) ALLOW Anywhere (v6)
25061/tcp (v6) ALLOW Anywhere (v6)
53 (v6) ALLOW Anywhere (v6)
53/tcp (v6) ALLOW Anywhere (v6)
53/udp (v6) ALLOW Anywhere (v6)
So it seems to me that it is a matter of NGINX configuration, but right now I have no idea where to look for. Any ideas?
[EDIT]
It's about app server - puma. With "local" database it starts automatically:
rails@app:~/model_app$ ps aux | grep [p]uma
rails 31314 5.7 1.6 1054540 133500 ? Ssl 06:05 0:02 puma 3.12.0 (tcp://0.0.0.0:3000) [model_app]
With "remote" database it doesn;t. When i run it by:
bundle exec puma
Then the result is:
rails@app:~$ ps aux | grep [p]uma
rails 535 1.6 1.7 1056636 142552 pts/3 Sl+ 06:13 0:06 puma 3.12.0 (tcp://0.0.0.0:3000) [model_app]
My file: /etc/systemd/system/rails.service
[Unit]
Description=ExampleApp
Requires=network.target
[Service]
Type=simple
User=rails
Group=rails
WorkingDirectory=/home/rails/model_app/
ExecStart=/bin/bash -lc 'bundle exec puma'
TimeoutSec=30s
RestartSec=30s
Restart=always
[Install]
WantedBy=multi-user.target
Why it doesn't start automatically when I link a remote database?
Have you checked if your app server is running? Nginx gives 502 when the proxy forwarding fails. Try running ps aux | grep [p]uma
ps aux | grep [p]uma
(replace puma with the app server you are using) and see if it is running.
If it's not running, then run it, it should work. If it still doesn't work, then check for error in nginx logs (/var/log/nginx/error.log). You will find the problem there. If you still need help, post the last few lines from the log file here so that we can try understanding the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.