Django, Gunicorn, Nginx redirect to local host

Viewed 401

I am trying to configure a production server using gunicorn and nginx for my Django app.

Currently I have gunicorn running but when I do a curl to my local host, I receive no output, so I think the issue is in my Django urls/views but I am not sure what to change as I am not referencing localhost anywhere. I also have nginx running. I am not using the nginx.conf file directly as I created a symlink and am using site-enabled. Here is my sites-available config:

#  /etc/nginx/sites-availible/msc_project.conf

# HTTP to HTTPS redirect: 
server {
    
    listen 0.0.0.0:80;
    server_name mscentral.mysite.com;
    
    # Certbot Letsencrypt verification
    location /.well-known/acme-challenge {
                alias /home/mscentral/mscentral_project/static/.well-known/acme-challenge/; #      
     }  
    
    return 301 https://mscentral.mysite.com$request_uri;
    }


#  Virtual host for mscentral
server {
    listen 0.0.0.0:443 ssl;
    server_name mscentral.mysite.com;
    ssl_certificate /etc/letsencrypt/live/mscentral.mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mscentral.mysite.com/privkey.pem;
    
    #static file handling
    location /static/ {
        alias /home/mscentral/mscentral_project/static/;
    }
    
    location /media/ {
        alias /home/mscentral/mscentral_project/media/;
    }
    
    #WSGI server
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8000/;
    }
    
    access_log /var/log/nginx/mscentral_project-access.log;
    error_log /var/log/nginx/mscentral_project-error.log;
    
    
    } 

here is my gunicorn service file:

[Unit]
Description=My Central Service for Gunicron
After=network.target

[Service]
Type=simple
# Another Type: forking
User=mscentral_internal
WorkingDirectory=/home/mscentral/mscentral_project
ExecStart=/home/mscentral/venv/bin/gunicorn -b localhost:8000 mscentral_project.wsgi
Restart=on-failure
# Other restart options: always, on-abort, etc

# The install section is needed to use
# `systemctl enable` to start on boot
# For a user service that you want to enable
# and start automatically, use `default.target`
# For system level services, use `multi-user.target`
[Install]
WantedBy=multi-user.target

This is my urls file:

from django.conf.urls import url, include
from django.urls import path
from ms_app import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # path('oauth2/', include('django_auth_adfs.urls')),
    path('',views.index,name='index'),
    path('ms_app/',include('ms_app.urls', namespace='ms_app')),
    path('admin/', admin.site.urls),
    path('logout/',views.user_logout,name='logout'),
    path('special/',views.special,name='special'),
    # path('accounts/profile/', views.index),
    
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# if settings.DEBUG:
#     urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and I have a basic index page at the moment:

def index(request):     
return render(request, 'ms_app/index.html')

Here is my ALLOWED_HOSTS in my settings file:

ALLOWED_HOSTS = ["mscentral.mysite.com","localhost"]

When I remove localhost from my ALLOWED_HOSTS then I get the following Django error: Disallowed Hosts Django error

And when I keep localhost in my ALLOWED_HOSTS then mysite's fqdn then I get redirected to localhost.

This is my first time trying this kind of deployment. Please, any and all advise/tips/recourses will be appreciated.

My Django app is in a virtual environment, Using Django: 3.2.5, Gunicorn: 20.1.0, Python: 3.8.10, Virtualization: vmware, Operating System: Ubuntu 20.04.3 LTS, Kernel: Linux 5.4.0-91-generic, Architecture: x86-64, nginx version: nginx/1.18.0

Thank you.

0 Answers
Related