36

I'm deploying a Rails app to production. It seems that Puma is fast and handles many of the things I want in a web server.

I'm wondering if I even need to bother with Nginx, and what I'd be missing out on if just used Puma?

gylaz
  • 13,221
  • 8
  • 52
  • 58
  • 2
    Possible duplicate of [Why do we need nginx with thin on production setup?](https://stackoverflow.com/questions/17482440/why-do-we-need-nginx-with-thin-on-production-setup) – Greg May 24 '18 at 19:54

2 Answers2

50

Nginx is a web server and puma is an application server. Both have their advantages, and you need both.

Some examples:

  • Static redirects- you could setup your nginx to redirect all http traffic to the same url with https. This way such trivial requests will never hit your app server.

  • Multipart upload- Nginx is better suited to handle multipart uploads. Nginx will combine all the requests and send it as a single file to puma.

  • Serving static assets- It is recommended to serve static assets (those in /public/ endpoint in rails) via a webserver without loading your app server.

  • There are some basic DDoS protections built-in in nginx.

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
  • I want to use nginx with puma,how to do? – wokerman Sep 11 '22 at 08:20
  • 1
    @wokerman you may check this [Setting up Nginx in Ubuntu 18 for a Ruby on Rails app using Puma](https://damuz91.medium.com/setting-up-nginx-in-ubuntu-18-for-a-ruby-on-rails-app-using-puma-ce6b1bafbf88) – Masroor Oct 22 '22 at 11:27
  • If you set your website with Cloudflare, you can skip Nginx. Because CF can redirect HTTP to HTTPS, and the statics assets are cached. Assets will load only once when they are changed. – Kartikey Tanna Mar 28 '23 at 11:30
13

There's a significant difference between a web server and an application server. Nginx (Web Server) and Puma (App Server) will handle requests in your application simultaneously.

Whenever there's a request coming from a client, it will be received by the nginx and then it will be forwarded to the application server which is Puma over here.

Having nginx as a web server will help you in handling multiple requests much more efficiently. Being a multi threaded server it will distribute requests into multiple threads making your application more faster.

As mentioned by vendant you can serve static pages using a web server as it will be a better approach.

If you're going to include a certification to your web application then you can provide redirects from http to https over here which will hit the app server only after redirecting to https.

If you're going to use Puma then you've to make sure that server is using resources efficiently but if you'll use nginx then it's going to take care of it by itself.

you can get more info here.

Gagan Gupta
  • 1,189
  • 7
  • 19
  • 3
    The link in the answer refers to stale data, the descriptions of the servers was written in 2010 and updated (not fully) for 2013. Things changed quite a lot since that time. – Myst May 31 '18 at 15:47