Nginx Setting and Tips

Code Dexterous
3 min readOct 19, 2021

It is a web server that can also be used as a reverse proxy, load balancer, etc.

Today I am going to explain some basic steps/commands to setup nginx. I am using Ubuntu OS.

Installation

sudo apt install nginx

Check version of Nginx

nginx -v

Start Nginx

sudo service nginx start
or
sudo systemctl start nginx

Stop Nginx

sudo service nginx stop
or
sudo systemctl stop nginx

Restart Nginx

sudo service nginx restart
or
sudo systemctl restart nginx

Check if nginx configuration is OK

// Root User
nginx -t
// Normal User
sudo nginx -t

When we restart the server then nginx will not auto-restart. So to fix that use

sudo systemctl enable nginx

This command will create soft link of nginx service inside /usr/lib/systemd/system

Nginx configuration can be found inside

/etc/nginx

Default Web Server Configuration can be found inside

/etc/nginx/sites-available

Nginx have two folders sites-available and sites-enabled. The default configuration file is

000-default.conf

To create your own web server configuration, just make a file with conf extension. Generally to be safe we just make a copy of default configuration.

You can create as many confiuration you want. All configuration will not be enabled/used.

To enable any server configuration we have to make a symbolic link of that config file into sites-enabled.

Don’t forgot to add semicolon in the last of the line.

Our nginx file generally have below keywords/blocks.

  • server: It is main block of the file where we decide on which port our server will listen and from where it will serve the content for end-user
  • listen: here we provide the port number where our server will accept the request. Default is 80 for no SSL certification and 443 with SSL certification.
  • server_name: This is used to give our domain name. You can give name by space separated.
  • location: It is a block to identify the requested URI and decide from where we should get the content to be served for end user. We can have as many location we want.
  • error_page: It is used to specify the error content, in case any error happened like 404, 500, 502, 503 etc.
server.conf without SSL
server.conf with SSL

If we want to server content from an application which is running on PORT 5000 then we use Proxy Pass. If we only want to server static content from a file then we can use ROOT to specify that.

The above screenshot is the minimum required configuration. We can do more settings.

There are some more important setting which we should use whenever we are using Proxy-Pass and SSL.

We should override the header setting in proxy. Le understand why need to do this then we will see how to do this.

Let us suppose, we have an application which access the enduser Host, IP Address etc.

Since we have used nginx then what will happen whenever an user requested for an URL then it will come first to Nginx and then it will go on our application.

That’s why everytime the host in our application will be the host from where we getting the request that will be localhost. Because our nginx and application is running on same application

But we need end user details not our Nginx details, so we need to find a way to pass the header, which we received on Nginx to our application. To do this we need to add below settings outside the location block and inside the server block.

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1
proxy_cache_bypass $http_upgrade;

For SSL configuration I will create a separate blogs. Please stay tunned.

--

--

Code Dexterous

I am full stack developer. Works on different technology and lean to learn more.