Jenkins

How To Setup Nginx On Ubuntu Server

Jan 16, 20257 min read2 views

Hey there, In this post we will be setting up nginx on our ubuntu server. Nginx is a modern web server that is fast, efficient and easy to work with.


This is the second part of the series. In the previous post we installed jenkins on ubuntu server. I strongly recommend that post to you if you want to follow along.


Agenda

1. Install and setup nginx

2. Understand the config file and its syntax

3. Create a nginx configuration to proxy our jenkins server.


Install Nginx

To install nginx, make sure you have all the prerequisites. Simply run this command to install the dependencies if not already installed.

$ sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-
  keyring

Fetch the signing key so that system can verify nginx's authenticity

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

To verify the authenticity of keys run

$ gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

It's output should contain the proper key

pub  rsa4096 2024-05-29 [SC]
     8540A6F18833A80E9C1653A42FD21310B49F6B46
uid                     nginx signing key <[email protected]>

pub  rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
     573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                     nginx signing key <[email protected]>

pub  rsa4096 2024-05-29 [SC]
     9E9BE90EACBCDE69FE9B204CBCDCD8A38D88A2B3
uid                     nginx signing key <[email protected]>

Now setup apt repository for nginx stable packages

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

After that refresh the package list and install nginx

$ sudo apt update && sudo apt install nginx -y

Verify the nginx installation

$ sudo systemctl status nginx

It should output something like this

$ ○ nginx.service - nginx - high performance web server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: inactive (dead)
      Docs: https://nginx.org/en/docs/

The server is installed but not running. Now enable the service so that it can automatically starts after system boot.

$ sudo systemctl enable nginx

Now finally run the service.

$ sudo systemctl start nginx

It will start the nginx server. You can now verify it using systemctl status command or by opening the ip address of your server in the browser tab.

If you see this page, it means nginx is up and running.


Nginx Configuration

Navigate to the /etc/nginx/ directory to see the config file.

$ cd /etc/nginx && ls

you will see the content of nginx directory

├── conf.d
│  └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

This is the structure of this directory. Now see the content of nginx.conf

$ cat nginx.conf

This is the main configuration file for nginx. We create server blocks to serve the website as per the incoming request through port :80. By default any incoming request will use port 80. Our nginx then matches server_name with the headers of the request. Then matched block serves the content specified in that particular server block.



See the diagram above to get the better idea of what I just explained. There are three different requests are made to our server. Nginx kind of matches the request header against server_name statement of each server block server1, server2, and server3. Each server block is configured to point at different processes/directories. Once a match is found the request is either transferred to the other service or the content is served from the specified directory.


This was a high level idea of how nginx works. If you want to know more in depth, consider reading official beginners guide. I'll be explaining each line I write in the configuration. If you check the content of /etc/nginx/nginx.conf, you will find a line like following --

include /etc/nginx/conf.d/*.conf;

It means include all the configurations defined in /etc/nginx/conf.d/ directory and saved in a file with the extension .conf. So this directory is the place where we should create our server blocks in files with extension .conf.


Okay enough. Now let's create our first server block. We will create a server block to proxy incoming requests to the jenkins service.


In the terminal, create a file named jenkins.mydomain.com.conf. ( of course I have a real domain for this. )

$ vim /etc/nginx/conf.d/jenkins.mydomain.com.conf

It will open the vim editor. Press i to enter the insert mode. And then copy following content and paste that in the editor by pressing ctrl + shift + v.

upstream jenkins {
   server 127.0.0.1:8080;
}
server {
   server_name jenkins.mydomain.com;

   location / {
       proxy_pass http://jenkins;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto $scheme;
   }
}


In the upstream, we specify on which service we are trying to proxy requests. the value 127.0.0.1:8080 means the service is running on the same machine where this nginx server is installed and the service is running on port 8080. This is the default port of jenkins.


In the server block we are specifying what domain nginx need to match when a request hits the server. We provide our domain name. So when some one will hit https://jenkins.mydomain.com/ the request will be proxied to the upstream backend. All the requests made to this domain will be served by this server configurations.


location / { this line specifies the root block for this server. And this server we are passing the request to our existing jenkins server. From now on it is the upstream jenkins's resposibility to handle the request.


proxy_set_header Host $host; this line sets the "Host" header to the forwarded request to match the Host header of original request.


proxy_set_header X-Real-IP $remote_addr; this line sets the "X-Real-IP" header to the client's IP address ("$remote_addr"). So that our upstream service can know who is the actual client requesting the resource.


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

This sets the "X-Forwarded-For" header, which is a standard way to track the chain of client IP addresses through proxy servers. The $proxy_add_x_forwarded_for variable appends the client’s IP address to the existing X-Forwarded-For header.


proxy_set_header X-Forwarded-Proto $scheme;

This sets the "X-Forwarded-Proto" header to the protocol used by the client (http or https). This is useful for backend servers to know whether the original request was secure (over HTTPS) or not.


Phew!! that was a whole lot of information. But these are the basic terms that you should know before creating a proxy. To see how it serves the static files you should see /etc/nginx/conf.d/default.conf it serves a basic index.html file. Which we have seen earlier. (remmeber that welcome screen of nginx).


Apply the changes to the server

Now it is time to see the changes we have made in the configuration. The first step is to check whether our configuration is valid or not. For this we need to hit following command.

$ sudo nginx -t

If everything is fine you should see something like below:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

It means our configuration is perfect we can reload the nginx service to see the changes...

$ sudo nginx -s reload

or

$ sudo systemctl reload nginx


Okay well none of the above command is working right now. I am using nginx version 1.27.3. So I will have to restart the service...


This is not recommended to restart the service each time you make changes to configuration or added a new conf. We should always perform a graceful reload for the nginx server so that the running services don't see a downtime...



Now open the domain jenkins.mydomain.com in a new tab. You should see the jenkins startup page.

Conclusion

Congratulations !! we successfully setup nginx on our ubuntu server. We discussed the high level working process of nginx, and learned how we can configure nginx reverse proxy via configuration file. Now we have two things running and up first jenkins automation server, and second nginx server. In the next post, we will setup jenkins jobs to create pipelines. Stay tuned.

cicdautomationubuntuserverclouddevopspipline

let's catch up

Discover more about what I'm up to! Follow me on social media for updates on my projects, tech thoughts, and a sprinkle of everyday life. Let's connect in this digital space!