5 steps to running apps from a single port via nginx

Uncategorized

Written by:

Reading Time: 2 minutes

Hello,

NGINX_logo_rgb-01

I have already spoken about how to configure nginx from a previous post. However that was used for when you wan to run them on different ports. However what if you want them to run from the same port ?

Fortunately, that is possible too. This short tutorial is very specific to running multiple apps from a single port on nginx and uwsgi for Django.

The one error I made was to run it in emperor mode. Its a costly mistake that does not need to be made. So I will keep it short and just say there is no need to do it in emperor mode. Emperor mode works best if you are addling different nginx configuration files running on different ports Without much ado lets kick things off in this 5 step tutorial.

Step 1:

Keep it simple, and do a nginx configuration file like so;

server {

# the port your site will be served on

listen 7002;

server_name localhost;

charset utf-8;

# Finally, send all non-media requests to the Django server.

location /app1/ {

uwsgi_pass unix:///home/user/app1/app1.sock;

include /home/user/app1/uwsgi_params;

}

location /app2/ {

uwsgi_pass unix:///home/user/app2/app2.sock;

include /home/user/app2/uwsgi_params;

}      

}

Step 2:

Nothing changes in your .ini file. It remains the same just like how you would have configured your app to run on different ports. After you are done, you just call it when you do uwsgi app1.ini . The details from the .ini are loaded from it.

Step 3:

In app1, add the sub directory to the urls.py file. It should be something like so;

urlpatterns = patterns(”,

url(r’^app1/home/’, include(‘home.urls’, namespace=’home’)),

url(r’^app1/$’, ‘home.views.intro’, name=’index’),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Step 4:

Good job. Now you have configured your url, all that is left is for the assets to show. That is done in your settings file. Add the sub directory ( ie app1 ) in the settings.py file too.

For example;

STATIC_URL = ‘/app1/static/’

MEDIA_URL = ‘/app1/media/’

ADMIN_MEDIA_PREFIX = ‘/app1/admin-media/’

STATIC_PATH = os.path.join(BASE_DIR,’/app1/static’)

LOGIN_REDIRECT_URL = ‘/app1/home/intro’

Step 5

Finally, you will access link like so http://127.0.0.1:7002/app1/ . And that’s it you are done!

Remember from steps 2 to 5 are for configuring app1 to run from a port( in our case port 7002 ). For app2 just replicate steps 2 to 5.

Hopefully this short tutorial will have saved someone lots of lots time trying to implement it the wrong way only to fail. I am glad I could be of help. Catch you all later.

I’ve left all code snippets here on my personal site, and here on my Github page.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up