How can I serve multiple apps with Nginx?

Avatar asauber 4 months ago

I have a Flask app that runs with a unix domain socket at /tmp/appname.sock with gunicorn, a WordPress instance that runs on port 8081, and a static site that I would like to have a virtual root at /home/appname/www.

How can I serve these 3 apps on one Linode, each with a different domain name?

1 Reply

I'm not a master at Nginx, but perhaps I can help out a bit! If you, or anyone else reading this, doesn't have much experience setting up Nginx, here's a great Nginx primer guide from our documentation.

Nginx can handle many different websites/apps on a single Linode through configuring multiple server blocks, one for each site. Each server block is typically stored as a separate file within the directory /etc/nginx/sites-available. For example, the server block for your static site with the domain name "example.com" might be located in /etc/nginx/sites-available/example.com and may look like:

server {
    listen         80;
    listen         [::]:80;
    server_name    example.com www.example.com;
    root           /home/appname/www;
    index          index.html;
    try_files $uri /index.html;
}

Your wordpress server block should be very similiar to the one above, only changing the port from 80 to 8081 and of course modifying the domain and directory.

For the flask app, you'll need to change the listen directive to listen unix:/tmp/appname.sock

More details can be found in the Nginx documentation.

If you decide to store the app files outside the default web directory (typically /var/www/html for Ubuntu ), you should make sure the webserver user (www-data for Ubuntu) has permission to access those files. When in your home directory, you can run a command like chmod 701 appname to give other users access to the folder appname. Of course, you should review the permissions already in place before changing anything.

After creating and configuring the additional server blocks, you'll need to enable them and restart the Nginx service. This will depend on your OS and how you have nginx configured. For restarting Nginx on Ubuntu 16.04, run sudo systemctl restart nginx.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct