Nginx Proxy Server

Nginx Proxy Server

What is a proxy server?

A forward proxy, often called a proxy, proxy server, or web proxy, is a server that sits in front of a group of client machines. When those computers make requests to sites and services on the Internet, the proxy server intercepts those requests and then communicates with web servers on behalf of those clients, like a middleman.

In a standard Internet communication, computer A would reach out directly to computer C, with the client sending requests to the origin server and the origin server responding to the client. When a forward proxy is in place, A will instead send requests to B, which will then forward the request to C. C will then send a response to B, which will forward the response back to A.

Why would anyone add this extra middleman to their Internet activity? There are a few reasons one might want to use a forward proxy:

  • To avoid state or institutional browsing restrictions - Some governments, schools, and other organizations use firewalls to give their users access to a limited version of the Internet. A forward proxy can be used to get around these restrictions, as they let the user connect to the proxy rather than directly to the sites they are visiting.

  • To block access to certain content - Conversely, proxies can also be set up to block a group of users from accessing certain sites. For example, a school network might be configured to connect to the web through a proxy which enables content filtering rules, refusing to forward responses from Facebook and other social media sites.

  • To protect their identity online - In some cases, regular Internet users simply desire increased anonymity online, but in other cases, Internet users live in places where the government can impose serious consequences to political dissidents. Criticizing the government in a web forum or on social media can lead to fines or imprisonment for these users. If one of these dissidents uses a forward proxy to connect to a website where they post politically sensitive comments, the IP address used to post the comments will be harder to trace back to the dissident. Only the IP address of the proxy server will be visible.

How is a reverse proxy different?

A reverse proxy is a server that is placed in front of one or more web servers, intercepting requests from clients. When the clients try to connect to the origin server of a website, those requests are intercepted by the reverse proxy server. The proxy server forwards these requests to the proxied server and receives responses from it to send them to the clients.

Benefits of Reverse Proxy:

  • Enhances Security

  • Load Balancing

  • Caching

  • SSL Encryption and more

Typically all requests from D would go directly to F, and F would send responses directly to D. With a reverse proxy, all requests from D will go directly to E, and E will send its requests to and receive responses from F. E will then pass along the appropriate responses to D.

Installation Guide (Reverse proxy)

Pre-Requisites

  • Nginx web server installed on Ubuntu Server 20.04 LTS

  • Website configured on Ubuntu Server 20.04 LTS

  • SSH connection to remote machines (Nginx and Website)

  • Nginx Web Server’s Virtual Machine’s IP address: 184.73.141.217

  • Apache2 Deployed Website’s Virtual Machine’s IP address: 54.145.32.86

SSH to Nginx machine

The first step is to connect to the remote machine where Nginx is installed. We will configure Nginx as a reverse proxy on this machine.

Install Nginx

sudo apt-get update
sudo apt install nginx
sudo systemctl status nginx

Access the Nginx

We also installed apache2 on another instance

Now we will make nginx as a reverse proxy for the Apache2

Add an Nginx proxy_pass setting

The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx’s sites-available folder.

sudo nano /etc/nginx/sites-available/default

Nginx proxy_pass example

For this example, we setup the location mapping of the Nginx reverse proxy to forward any request of an Apache2 server that also runs on port 80, but I have changed it to 8010 here

# example nginx reverse proxy mapping
# /etc/nginx/sites-available/default
location /examples {
  proxy_pass http://54.145.32.86:8010/;         #PublicIP of Apache2
  proxy_buffering off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Port $server_port;
}

Add these Nginx proxy_pass, proxy_buffering and proxy_set_header updates to your default configuration file and save you changes.


Restart Nginx as a reverse proxy

sudo systemctl restart nginx

When the server comes online, try to access the backend server through the Nginx reverse proxy. In this example, we can access the apache2 running on port 8010 through Nginx.

http://nginxlocalhost/80 -- proxies for --> http://apache2localhost:8010


Nginx proxy for reactjs

reactjs installation steps

Connect to the EC2 instance and execute the below commands

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt install -y nodejs
npx create-react-app react-deploy
cd react-deploy
npm start

Access the reactjs app

Now we will config the nginx as a proxy for reactjs

Connect to the nginx EC2 instance and where I have already installed nginx

Add an Nginx proxy_pass setting

sudo nano /etc/nginx/sites-available/default

Nginx proxy_pass example

For this example, we setup the location mapping of the Nginx reverse proxy to forward any request of an reactjs application which runs on port no 3000

# example nginx reverse proxy mapping
# /etc/nginx/sites-available/default
location /examples {
  proxy_pass http://18.207.116.120:3000/;         #PublicIP of reactjs:port
  proxy_buffering off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Port $server_port;
}

rresatrt the nginx

sudo systemctl restart nginx