[RUBY] [Rails] I was addicted to the nginx settings when using Action Cable.

Introduction

This is a story when I used Action Cable to add a real-time chat function to an application made with Ruby on Rails.

It worked fine in the local environment, but the real-time chat part didn't work in the production environment.

After investigating the cause, it seems that it is necessary to set nginx corresponding to it in order to communicate with websocket used in Action Cable.

(For reference) Original nginx settings

This is the original nginx setting. This is also included so that the difference between before and after correction can be easily understood. Before implementing ActionCable, this worked fine.


 # https://github.com/puma/puma/blob/master/docs/nginx.md
upstream app {
  server unix:///app/tmp/sockets/puma.sock;
}

server {
  listen 80;
 server_name ***. ***. ***. ***; # IP address of the app

  keepalive_timeout 5;

  # static files
  root /app/public;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    # static files
    if (-f $request_filename) {
      break;
    }
    if (-f $request_filename.html) {
      rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename.html) {
      rewrite (.*) $1.html break;
    }

    if (!-f $request_filename) {
      proxy_pass http://app;
      break;
    }
  }

   location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
    expires max;
    break;
  }
}

Nginx settings corresponding to Action Cable

To be able to handle websocket communication Added location / cable and below.


 https://github.com/puma/puma/blob/master/docs/nginx.md
upstream app {
  server unix:///app/tmp/sockets/puma.sock;
}

server {
  listen 80;
 server_name ***. ***. ***. ***; # IP address of the app
  keepalive_timeout 5;

  # static files
  root /app/public;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    # static files
    if (-f $request_filename) {
      break;
    }
    if (-f $request_filename.html) {
      rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename.html) {
      rewrite (.*) $1.html break;
    }

    if (!-f $request_filename) {
      proxy_pass http://app;
      break;
    }
  }

 # Add the following

  location /cable {
    proxy_http_version 1.1;
    proxy_set_header Upgrade websocket;
    proxy_set_header Connection Upgrade;
    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://app/cable;
  }

 #Additional part up to here

   location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
    expires max;
    break;
  }
}

Recommended Posts

[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
What I was addicted to while using rspec on rails
A story I was addicted to in Rails validation settings
The story I was addicted to when setting up STS
I was addicted to the roll method
I was addicted to the Spring-Batch test
What I was addicted to when implementing google authentication with rails
I was addicted to using RXTX on Sierra
Memorandum: What I was addicted to when I hit the accounting freee API
Problems I was addicted to when building the digdag environment with docker
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the NoSuchMethodError in Cloud Endpoints
I was addicted to the record of the associated model
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I was addicted to setting default_url_options with Rails devise introduction
I was addicted to looping the Update statement on MyBatis
I was addicted to the setting of laradock + VSCode + xdebug
What I was addicted to with the Redmine REST API
I was addicted to using Java's Stream API in Scala
I was addicted to starting sbt
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
About the matter that I was addicted to how to use hashmap
I was addicted to the API version min23 setting of registerTorchCallback
[Rails 5] How to display the password change screen when using devise
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
[Rails] How to solve ActiveSupport :: MessageVerifier :: InvalidSignature that I was addicted to when introducing twitter login [ActiveStorage]
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
I summarized the points to note when using resources and resources in combination
My.cnf configuration problem that I was addicted to when I was touching MySQL 8.0 like 5.7
Recorded because I was addicted to the standard input of the Scanner class
I was a little addicted to running old Ruby environment and old Rails
I was addicted to scrollview because I couldn't tap the variable size UIView
I was addicted to unit testing with the buffer operator in RxJava
I want to test Action Cable with RSpec test
[Rails] When I use form_with, the screen freezes! ??
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I was addicted to installing Ruby/Tk on MacOS
The code I used to connect Rails 3 to PostgreSQL 10
I was addicted to doing onActivityResult () with DialogFragment
I was addicted to not being able to connect to AWS-S3 from the Docker container
I was a little addicted to the S3 Checksum comparison, so I made a note.
[Beginner] What I learned when trying to introduce bootstrap to the rails6 app without using a CDN [Asset pipeline]
What I was addicted to when developing a Spring Boot application with VS Code
A memo that I was addicted to when making batch processing with Spring Boot
When making a personal app, I was wondering whether to make it using haml
When importing CSV with Rails, it was really easy to use the nkf command
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
A story I was addicted to when getting a key that was automatically tried on MyBatis
I want to judge the range using the monthly degree
[Rails] I don't know how to use the model ...
docker + rails + nginx + webpacker + ssl + action cable + active storage
[Ruby] Misunderstanding that I was using the module [Beginner]
I want to call the main method using reflection
Addicted to the webpacker that comes standard with Rails 6
Rails: When the image is no longer displayed using refile (the story was just fill → size)