title: How to add favicon to django app with React front end
body: <p>I'm building a Django app with a React front end, built using react-create-app. I can't get the favicon to show up in production.</p>
  <p>I'm using Django 2.10.0.</p>
  <p>When I build the React app, the build output files are put in the assets/static/ folder. This includes image files used as backgrounds via css, and all works fine. These are included in the build because they are referenced in the stylesheets. The favicon is referenced from index.html and therefore isn't picked up by the build process.</p>
  <p>However I don't know how to get the server to serve up the favicons. If I put them in the frontend/public folder, the Django server doesn't find them.</p>
  <p>If I manually copy them into the static/ folder, they show up but that folder is managed by Webpack and the contents are removed when the app is built.</p>
  <p>Copying the favicons into the project root doesn't work. They are in the same folder as index.html, which is served up, but Django server won't serve those co-located image files.</p>
  <p>The only documentation I can find deals with either Django or create-react-app; I can't find anything that explains how to tie them together for favicons.</p>
  <p>In settings.py:</p>
  <pre><code>STATIC_URL = '/static/'
  
  STATICFILES_DIRS = [
      os.path.join(BASE_DIR, 'assets/static'),
      os.path.join(BASE_DIR, 'assets'),
  ]
  </code></pre>
  <p>In index.html:</p>
  <pre><code>&lt;link rel=&quot;shortcut icon&quot; href=&quot;%PUBLIC_URL%/favicon.ico&quot; /&gt;
  </code></pre>
  <p>In the browser, this is converted to:</p>
  <pre><code>&lt;link rel=&quot;shortcut icon&quot; href=&quot;/favicon.ico&quot;&gt;
  </code></pre>
  <p>which doesn't work - the file is not served up. I'm not sure why this doesn't work given that I've added 'assets' to STATICFILES_DIRS in settings.py and have copied the favicons into the root of 'assets'.</p>
  <p>All the working css and image links include the static folder, e.g.:</p>
  <pre><code>http://localhost:8000/static/media/private.d7365d01.svg
  </code></pre>
  <p>It seems like there are two problems:</p>
  <ol>
  <li><p>how to get the build process to copy the favicon and related files into the assets folder so they can be served up</p>
  </li>
  <li><p>how to get index.html to look in the right place for the favicon</p>
  </li>
  </ol>
  <p>If I could customise PUBLIC_URL to point to static/ that might do it, but that might break other bits of code that reference it?</p>
  <p>I am sure I'm missing something simple. I'd be grateful for any help!</p>
  <p>Edit: I have now got the favicon to show up in a built version running on my local machine, but it isn't being served by the production server.</p>
  <p>Base.py</p>
  <pre><code>STATIC_URL = '/static/'
  
  STATICFILES_DIRS = [
      os.path.join(BASE_DIR, 'assets/static'),
      os.path.join(BASE_DIR, 'assets'),
  ]
  </code></pre>
  <p>index.html</p>
  <pre><code>&lt;link rel=&quot;shortcut icon&quot; href=&quot;%PUBLIC_URL%/static/favicon.ico&quot; /&gt;
  </code></pre>
  <p>And favicon.ico is in the 'assets' folder. After building my React app and running the development server locally, the url http://localhost:8000/static/favicon.ico displays my favicon. But after deploying this to the server, the url <a href="https://example.com/static/favicon.ico" rel="nofollow noreferrer">https://example.com/static/favicon.ico</a> finds nothing. I have checked and the favicon.ico file is present on the server, in the assets folder. Settings.py is the same. I can't figure out why the production server isn't serving up the favicon?</p>
  <p>Edit: I've tried adding this to my settings.py, but it still doesn't work:</p>
  <pre><code>STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
  </code></pre>
  <p>'assets' is the name of the folder on the server that contains my build files. The favicons are in assets, as is index.html, and the React files in assets/static.</p>
  <p>Edit: this is my Passenger config file for Nginx:</p>
  <pre><code>server {
      listen 80;
      server_name 178.62.85.245;
  
      passenger_python /var/www/myappname/venv36/bin/python3.6;
  
      # Tell Nginx where your app's 'public' directory is
      root /var/www/myappname/myappname/myappname;
  
      # Turn on Passenger
      passenger_enabled on;
  
  
     # Tell Nginx the location of the build output files
      location /static/ {
         autoindex on;
         alias /var/www/myappname/myappname/assets/;
      }
  }
  </code></pre>
  <p>Nginx finds the file index.html and all the files that React built. I don't know why it can't find the favicon in the same folder!</p>