Cannot properly redirect Nginx to Dash Application

I have a dash app that I built and I am trying to host it on my server using Nginx and Gunicorn and while it works by going to www.site.com:5055 , it does not work going to www.site.com/dash/ for example. Basically the page stays stuck on Loading and in the developer tools I’m seeing failing to load resources such as dash_renderer.v1_2_2m1574885976.min.js and many more. So I’m not really sure what is going on here? If I’m missing something in my Nginx file. Thanks!

**Supervisor Setup**
[program: crm]
directory=/home/production/crm-dash-app
command = /home/production/crm-dash-app/venv/bin/gunicorn -w 3 -b :5055 app:server
user = production
autostart = true
autorestart = true
stopasgroups = true
killasgroup = true
stderr_logfile = /var/log/crm/crm.err.log
stdout_logfile=/var/log/crm/crm.out.log

**Nginx**

location /dash/ {
               proxy_pass   http://127.0.0.1:5055/;
               proxy_redirect off;

}

Have you set the url_base_pathname for your Dash app? I’ve copied an example below:

app = dash.Dash(__name__, assets_folder='assets', assets_url_path='', url_base_pathname='/dash/')
3 Likes

That will be something that I’ll try. Right now I am using an instance of Flask so I guess I can try create a redirect function such as

         @server.route("/crm") 
          def reroute_dash(): 
                 return flask.redirect("/dash/") # /dash/ will be the url_base_pathname 

See if this will reroute the original URL request to the correct location and the resources will load.

This is a shortened version of the application I got and it still isn’t working as its supposed to. IT works on www.site.com:5055/crm but not www.site.com/crm which I don’t get. It still saying Loading… and stays stuck on there. When I go into the developer tools it says failed to load a bunch of resources.

import dash
import dash_core_components as dcc
import dash_html_components as html
import os
import pymysql as sql
from dash.dependencies import Output, Input
import json
import pandas as pd
import flask

 
# Read in db credentials from confif.json
json_file = open("config.json", "r")
config = json.load(json_file)
 
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server, url_base_pathname="/dash/")
 
# Define Layout of the dashboard
app.layout = html.Div([
	html.Div(className="title", children=[
		html.Link(
			href="https://fonts.googleapis.com/css?family=Saira&display=swap", rel="stylesheet"),
		html.P(id="dash-title", children="CRM Dashboard")
	]),
])
 

@server.route("/crm")
def render_dash():
	return flask.redirect("/dash/")
 
if __name__=="__main__":
	app.run_server(port=5000, host="0.0.0.0", debug=True, use_reloader=True)

Are you loading any external resources, e.g. css or icons (it does not look so from your example code)? Have you tried running it without supervisor?

I am using nginx + gunicorn + docker, and i don’t remember facing a similar issue.

I tried running it without Supervisor and it still giving me the same issue when I go to www.mysite.com/crm and it just stays stuck on loading. It wants to load but something is preventing the rest of the resources from loading. This is what I ran below and port 5055 is opened in the firewall settings. As for resources I got a folder called assets and in there I just have css file.

 gunicorn -w 3 --bind 0.0.0.0:5055 app:server

What happens if you remove “server=server” from “app” and change…

server = flask.Flask(__name__)

to…

server = app.server

I tried that as well and its still failing to load. I might try this out on another VM site and see if I still get the same error. It could be my nginx configuration.

I looked back through my notes for configuring Gunicorn/NGINX and found that I had to run the following terminal command before NGINX would work. This will “Allow HTTPD scripts and modules to connect to the network”.

setsebool -P httpd_can_network_connect true

Reference:
https://wiki.centos.org/TipsAndTricks/SelinuxBooleans#line-40

1 Like

Thanks I’ll go ahead and try this out. Is there a version of this for Ubuntu 18.04 LTS?

Oh. Looks like the selinux-policy-default package isn’t included with Ubuntu by default. I’m running RHEL, so I had to run that command before NGINX would work properly. That said, I’m not sure what else to check. May need to up the logging levels for Gunicorn and NGINX for review.