How to Set up Configuration Settings for Nginx in Linux



Linux


In this article, we show how to set up configuration settings for nginx in linux.

Nginx works with gunicorn in order to serve django websites in a production environment.

Nginx serves traffic received via web traffics to gunicorn, which is a django web server that processes Django websites.

So in order to set up nginx for your website, there are a few things to be done.

One thing is, we must install nginx.

We install nginx by the following line shown below.



So now we have nginx installed.

Now we need to configure the settings for nginx.

Within this nginx file, we specify the port the server listens to, the location of the static directory, and the socket.

We configure these nginx configuration settings by specifying the following line belwo.



So we now open up the nano editor. Any time we type in sudo nano, we are opening up the nano text editor. We now open up the editor to edit the nginx configuration settings. Nginx routes traffic to gunicorn so that our website can be served to the web.

For these nginx configurations, we write in the following code below.



In these nginx configurations, we listen on port 80, which is the default port of the web to listen to web requests.

We then must specify the IP address and/or domain name of our website. In the above example, the website has an IP address of 22.33.44.129.

In this example, our static file is located in the the mywebsite directory (in the same directory as all the apps for our website). Therefore, we specify the location of the static directory as, root /home/david/mywebsite

david the user

We then must specify the socket file (.sock file). In the gunicorn.service file, we specify a .sock file. In this nginx file in the sites-available directory, we must specify this same exact socket file. This socket is how nginx and gunicorn communicate with each other.

Next save and close the file by typing in Ctrl+X, 'Y', and then 'Enter'

Next, we enable the file by linking it to the sites-enabled directory.

So we simply made this site available in the sites-available directory. Now we must make enable this site by linking it to the sites-enabled directory.

This is done through the following line shown below.



So now our website, our django project mywebsite, is enabled on nginx.

nginx and gunicorn now serve up the website permanently unless we disable it.

We can now test our nginx configuation for syntax errors by typing in the following line shown below.



If there are no errors, then restart nginx by typing in the following line shown below.



So now nginx is restarted.

Lastly, we specify the following line.



So we are all up and running.

Now type in your IP address in a web browser and your website should appear.

Everything should work except for the display of static files. To make the static files appear, type in, python3 manage.py collectstatic. Now static files should be displayed.

And this is how to set up configuration settings for nginx in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...