[Rails 6.0] CSS suddenly stopped loading, [Mysterious phenomenon? ]

I'm currently making a Rails app by personal development, but the CSS of the Rails app that was working normally suddenly stopped loading (and the image file), which wasted time. After all, I don't know the exact cause, but I will write here the process until it is resolved.

Development environment

How the error occurred

Write a test with rspec system specs, run it, repeat the failure about 10 times and access localhost (root_path) CSS was no longer reflected. Error details of Console at that time ↓

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

The error itself was something I often see, and as the title says, the CSS file wasn't loaded.

Solution procedure (?)

To be honest, it is delicate whether this solution is correct, but I will write here how it was cured. From the conclusion, it seems that it was cured by rewriting the contents of nignx.conf appropriately, rebuilding the docker container, restarting it, causing an error, rebuilding it again, and starting the container. ** Rebuilding normally didn't help. ** It took a long time because of this, I happened to make an error and it was cured, but ... Only those who know this will know it, so I'll explain it in detail below.

1. 1. Rewrite the contents of nginx.conf

In my development environment, I start the nginx container as a WEB server in order to make it as close to the production environment as possible. The nginx.conf is the configuration file ↓


#Specify proxy destination
#Send the request received by Nginx to the backend puma
upstream webapp {
  #I want to communicate with sockets, so puma.Specify sock
  server unix:///webapp/tmp/sockets/puma.sock;
}

server {
  listen 80;
  #Specify domain or IP
  server_name webapp ;

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

  #Specifying the document root
  root /webapp/public;

  client_max_body_size 100m;
  error_page 404             /404.html;
  error_page 505 502 503 504 /500.html;
  try_files  $uri/index.html $uri @webapp;
  keepalive_timeout 5;

  #Reverse proxy related settings
  location @webapp {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://webapp;
  }
}

Rewrite proxy_pass in this configuration file appropriately and cause an error on purpose. (The place may be anywhere)

2. Rebuild and start docker.

$ docker-compose down
$ docker-compose up -d --build

The -d option runs in the background. If you add the --build option, it will do everything from build to startup at once.

Then

$ docker-compose ps
    Name                  Command               State           Ports
----------------------------------------------------------------------------
webapp_app_1   bundle exec puma -C config ...   Up
webapp_db_1    docker-entrypoint.sh mysqld      Up       3306/tcp, 33060/tcp
webapp_web_1   /bin/sh -c /usr/sbin/nginx ...   Exit 1

An error should occur and the webapp_web_1 container should exit. (The container name changes depending on the docker-compose.yml setting, so please replace it yourself.)

3. Restore the rewritten nginx.conf and rebuild

Here, rewrite it appropriately and restore the nginx.conf file that caused the error on purpose. Then run 2 again. Then it came to be reflected for some reason! happy! !!

Thank you for reading to the end!

Honestly, what kind of logic did this method cure? Is it correct in the first place? I don't know. If you have any questions, I would appreciate it if you could comment.

For the time being, it is a fact that if I did this, it would be cured. I still know if CSS loading doesn't work when precompiling in a production environment, but this time it was a sudden error so it took a long time to fix it. If you're running into the same error, I hope this helps!

Recommended Posts

[Rails 6.0] CSS suddenly stopped loading, [Mysterious phenomenon? ]