Ivanori

Ivanori

I want Phoenix to not handle the assets at all in production. How to set it up in such a way?

Showing Posts 1 to 7

Qqwy

Qqwy

TypeCheck Core Team

What we usually do is to serve Phoenix behind Nginx (so inside your Nginx configuration you check for the domain name and forward certain calls to phoenix), and make this call conditional depending on if the call corresponds to a path in the priv/static folder.

So we do not tell Phoenix to not handle those (and as a matter of fact, in the development environment it still will) but those requests will simply be handled by Nginx directly and Phoenix will never see them.

I don’t have an example config snippet for you now unfortunately since I am on my phone, but I hope the gist is clear even without it :slight_smile: .

yurko

yurko

Look for plug Plug.Static calls in your endpoint, you can adjust or disable them.

Ivanori

Ivanori OP

2 different answers, which one is correct?

First - you don’t have to do anything other than setup nginx properly
Second - you first have to setup Phoenix correctly

minhajuddin

minhajuddin

If you don’t want phoenix to handle those requests just don’t send them to the phoenix server. I usually have an nginx config which looks like below:

server{
  server_name awesome.com;
  root /opt/www/awesome/path/to/static/assets;

  location / {
    try_files $uri @proxy;
  }

  location @proxy{
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  Host             $http_host;
    proxy_set_header X_FORWARDED_PROTO $scheme;

    proxy_redirect off;
    proxy_pass http://localhost:4000;

    gzip_proxied any;

  }
}

With this nginx will respond if it finds the asset and fallback to cowboy if it doesn’t find the static file.

LostKobrakai

LostKobrakai

Both. If you don’t send requests of static assets to phoenix in the first place it does not serve them. But if you want to make sure phoenix does not serve any assets no matter what (e.g. nginx being misconfigured) you’d want to disable Plug.Static.

NobbZ

NobbZ

As @LostKobrakai already pointed out, both are “correct”.

They are different strategies with the same goal. But to be honest, you need to make sure that your upstream reverse or HA proxy or even CDN is set up properly anyway, or you won’t have your assets at all.

Personally I’d just put the reverse proxy in front and leave phoenix untouched. This way there is a fallback available when the reverse proxy is failing parts of its job.

If though you still want to completely disable the delivery of statics in phoenix you can do it roughly like this:

unless Mix.env() in [:prod] do
  plug Plug.Static, …
end

You can find the original Plug.Static line in your projects endpoint.

Be aware though, that in case of a misbehaving reverse proxy the requests will now reach your applications router and be resolved by that. This might or might not be what you want in case of cases.

poops

poops

If you’re using releases, you’ll have to symlink to the release dir, since that will end up changing on every version bump.

Here’s how to do it with edeliver

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews