How to run Capybara specs on Nginx+Node+Rails servers? - ruby-on-rails

An application is running on Rails (localhost:3000) + Node (localhost:8080) servers and the Nginx is used to redirect requests either to Rails server or Node.
In Nginx config file we have set constant IP (nginx.localhost) to send requests to Nginx.
Also the Rails and Node servers IP are constant for nginx.
And now there is a problem to run tests on this environment.
I need to run tests with this nginx.localhost as a root_path or Capybara.app_host so that I could test application via Nginx.
Do you have any ideas? All what came to my mind was to update nginx config file with port and server generated by rspec on suite start.
But this is a bit awkward.

Related

How do I serve Rails development environment assets over local network?

I have a Rails 5 application I am running in development mode (RAILS_ENV=development). The Rails server was started with bundle exec rails s -b 0.0.0.0 -p 3002. Computers on the same network (subnet) as me can access my Rails application by IP in a browser, e.g. http://10.123.10.5:3002/. However, links to assets such as images are broken.
If I inspect the image in a browser, the tag will reference localhost as the host part of the IP such as <img src="http://localhost:3001/assets/logo-70eb2453cbce2a1790196aeb4ff1db9cddd3789b951bed9a6815505a490318a6.png">.
If I change the src to use the correct subnet IP, it works as expected. E.g. <img src="http://10.123.10.5:3002/assets/logo-70eb2453cbce2a1790196aeb4ff1db9cddd3789b951bed9a6815505a490318a6.png">
Note, also, that even the port seems to be incorrect, not adhering to the port specified in the rails s command
How do I remedy this situation?
You need to configure the config.asset_host in you rails application with your IP address

Domain setting using ruby rails

I am planning to have a web application.
To do so, I studied about ruby and ruby on rails. I am using linux server from amazon clouding system.
I bought a domain from godday, and I put the IP address on DNS setting. When I run 'rails s' command, I can connect to the wep page through port 3000 in such a way that domain.com:3000. However, I cannot directly connect to domain.com. How can I my domain works without port 3000?
And Do I have to run 'rails s' every time to make the wep page work? Actually I tried to use 'rails s &' to make it run in background. But it fails. How can I make the server run even though I am not connected to the linux server?
Thank you!
usually you use rails s just in development. there are quite a few ruby web servers you can choose from for your production environment: puma, passenger or unicorn to name a few.
of course all of them have their own tutorials how to set them up. for starters, i'd go with with passenger because it's integrated with nginx and apache and easily set up.
You need to specify a port, if you don't see the port it can be either 80 (http) or 443 (https).
rails server -p 80
On linux you have to be root to bind to port less than 1000, so just append sudo in front.

Deploying Rails/Passenger via nginx

I'm having a hard time figuring out where I'm going wrong in trying to deploy a Rails app via nginx. Rails is accessible via site.com:3000 (after starting it with rails server), and site.com:80 displays the standard nginx "working, but further configuration required" page. I've spent a few hours trawling the documentation trying to figure our how to get my Rails app accessible at :80 rather than :3000, but to no avail.
I think it's most likely that I'm misunderstanding how nginx, Passenger, and Rails work together, and have therefore configured my nginx.conf incorrectly (one page I found implied that I shouldn't both be using nginx and running rails server). Any and all help is hugely appreciated.
Possibly relevant version numbers:
Rails 4.1.4
Ruby 2.1.2p95
CentOS 6.5
nginx 1.6.0
nginx.conf partial: http://pastebin.com/A3JD09pr
I'm new at this, so it turned out that a couple things were up:
I needed to put export rvmsudo_secure_path=1 in my .bashrc instead of just running it once, following up with source ~/.bashrc in the terminal. This allowed me to run "rvmsudo" commands to start on port 80 rather than the default 3000.
I had both nginx and Rails competing for port 80, so I had to stop nginx's static page server to allow that. Simple as nginx stop.

Start Unicorn using ssl in development

I'm migrating my rails app (still in development) from Thin to Unicorn. My app uses config.force_ssl = true.
When using Thin I could just write:
thin start --ssl
My question is: What is the equivalent way to start Unicorn with ssl in development?
If I correctly understood your question, you're trying to run unicorn on port 443.
This is a bad idea.
Instead, to achieve the same goal, I would suggest, run unicorn on an unprivileged port (above 1024), or better on a unix socket, and switch Nginx before, passing all static stuff directly trough nginx, and the rails stuff, trough unicorn.
I know this doesn't answer your question, but for the user, it will work exactly the same, with some benefits when your app server (unicorn) crashes, for example a nice rendered 502 error page served via nginx instead of a plain network error message seen in the browser of your users.
You can with this solution run X different applications on the same port, with different subdomains. a must have for a development machine with many projects.

Rails framework & Nginx web server

As we install rails it uses its own web server WEBrick. If i want to run ths application in Nginx server, then how to configure or set the Nginx web werver?
You should run your rails application in a production server such as mongrel_cluster or thin (I have used the former, and am currently switching to the latter). To put nginx in front of it, I would use the upstream and proxy_pass directives. I found a nice blog post comparing ways of running rails applications that shows their config for mongrel_cluster + nginx.
Passenger is also available for nginx, I've used that with Apache and it was very easy to set up.

Resources