How to configure nginx for reverse proxy on Windows 10?

Sample nginx.conf settings to perform reverse proxy functionality to.

Read Me

In order to access a server hosted within a vm (guest), for development purposes from the host OS, which is restricted to same origin / localhost only requests, I set up a siple nginx reverse proxy to forward my requests.

Steps

  1. To install in a Windows VM, download and install nginx from the current, stable release; I installed to C:\nginx\
  2. Edit the <install path>/conf/nginx.conf file with the marked changes in the file of the same name in this gist.
  3. Start the nginx executable, located in your install path. There are service wrappers for Windows, or you can just kill the process to stop the nginx instance.

Commands for Windows

More information on the implementation of nginx in Windows can be found on the corresponding docs page. Here's the basic breakdown of commands, form within the nginx install directory:

Command
start nginx starts the process
nginx -s stop fast shutdown
nginx -s quit graceful shutdown
nginx -s reload config change, graceful shutdown of existing worker proc, starts new
nginx -s reopen re-open log files

Description

The config file contains a server block, inside which is a location / block. Inside that location block, we need to replace the root and index assignment with a proxy_pass //127.0.0.1:8080; line and a proxy_http_version 1.1; line.

Profit

Nov 30, 2022

Edward S.

3min Read

In the Linux operating system, a Reverse Proxy acts as a link between the host (client) and the server. It takes up client requests and passes them on to other servers, and finally delivers the server’s response to the client, appearing as if they originated from the proxy server itself. In this tutorial, we’ll show you what an Nginx reverse proxy is and how to set it up on your VPS!

The client and server exchange information continuously to work efficiently. Typically, reverse proxies are used by a web server. A reverse proxy or gateway appears to the client just like an ordinary web server, where no special configurations are necessary. The client makes ordinary requests while the reverse proxy decides where to pass on the information delivering the final output to the client as if it were the origin.

The Nginx reverse proxy has a wide range of benefits. It is amongst the most actively used open-source servers on the internet.

  • The Advantages of Using Nginx Reverse Proxy
  • How to Set Up an Nginx Reverse Proxy?
    • 1. Install Nginx
    • 2. Disable the Default Virtual Host
    • 3. Create the Nginx Reverse Proxy
    • 4. Test Nginx and the Nginx Reverse Proxy

The Advantages of Using Nginx Reverse Proxy

Let’s delve into the reasons explaining why this tool is so popular:

  • It is simple to implement and provides the user with high-end security against Web server attacks such as DDoS and DoS
  • Nginx Reverse proxy helps create a balanced load among several back-end servers and provides caching for a slower back-end server
  • Nginx does not require setting up a new process for each web request from the client. Rather, the default configuration is to comprise one work process per CPU
  • It can act as a reverse proxy server for various protocols such as HTTP, HTTPS, TCP, UDP, SMTP, IMAP, and POP3
  • It can handle over 10000 connections with a low memory footprint. Nginx can operate multiple web servers via a single IP address and deliver each request to the right server within a LAN
  • Nginx is one of the best web servers for improving the performance of static content. Additionally, it can also be helpful to serve cache content and perform SSL encryption to lower the load from the web server
  • It can also be helpful when optimizing content by compressing it to boost the loading time
  • Nginx can perform randomized experiments or A/B tests without placing JavaScript codes into pages.

And all of these benefits are just the tip of the iceberg! The more you use Nginx Reverse Proxy, the more features you will discover!

How to Set Up an Nginx Reverse Proxy?

Now, we will configure Nginx in front of an Apache web server. We chose the Apache server because it’s better at handling dynamic content.

So, all the static content will go to Nginx, while the dynamic content will go to Apache. This will improve performance by optimizing the delivery of content based on the handling criteria.

Next, we will define the IP address of the Nginx Proxy Server as 192.x.x.1 and the back-end Apache server as 192.x.x.2. After setting up Apache, we can move on to these steps:

1. Install Nginx

We’ll be using the apt command on Ubuntu 18.04:

sudo apt-get updatesudo apt-get install nginx

2. Disable the Default Virtual Host

Once you have installed Nginx, follow the below command to disable the virtual host:

sudo unlink /etc/nginx/sites-enabled/default

3. Create the Nginx Reverse Proxy

After disabling the virtual host, we need to create a file called reverse-proxy.conf within the etc/nginx/sites-available directory to keep reverse proxy information.

For this, we should first access the directory using the cd command:

cd etc/nginx/sites-available/

Then we can create the file using the vi editor:

vi reverse-proxy.conf

In the file, we need to paste in these strings:

server {     listen 80;     location / {         proxy_pass //192.x.x.2;     } }

In the above command, the considerable point is the proxy pass is allowing the requests coming through the Nginx reverse proxy to pass along to 192.x.x.2:80, which is Apache remote socket. Thus, both the web servers – Nginx and Apache, shares the content.

Once completed, simply save the file and exit the vi editor. You can do this by keying in :wq.

To pass information to other servers, you can use the ngx_http_proxy_module in the terminal.

Now, activate the directives by linking to /sites-enabled/ using the following command:

sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

4. Test Nginx and the Nginx Reverse Proxy

Lastly, we need to run an Nginx configuration test and restart Nginx to check its performance. Type the below command to verify the Nginx functioning on the Linux terminal:

service nginx configtestservice nginx restart

Remember, if you receive a failed test, that most likely indicates that Apache was not properly set up.

Conclusion

There are many benefits of setting up an Nginx reverse proxy in the Linux operating system. It can effectively boost performance and enhance security against malware. The Nginx reverse proxy configuration is a simple process in a Linux terminal. Although there are a plethora of ways to install and configure it, which completely depend upon your requirement, the above tutorial is hassle-free and straightforward to help you get started with a reverse proxy setup.

Edward is a Content Editor with years of experience in IT as a writer, marketer, and Linux enthusiast. Edward's goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

How to setup Nginx reverse proxy on Windows?

Nginx reverse proxy configuration steps.
Install Nginx on your Windows or Linux server (prerequisite)..
Add the Nginx proxy_pass setting in a virtual host or the default config file..
Map a context root to the URL of a backend server..
Optionally set headers for the Nginx reverse proxy to use with the backend..

How to setup Nginx as reverse proxy?

How to Set Up an Nginx Reverse Proxy?.
Install Nginx. We'll be using the apt command on Ubuntu 18.04: sudo apt-get update sudo apt-get install nginx..
Disable the Default Virtual Host. ... .
Create the Nginx Reverse Proxy. ... .
Test Nginx and the Nginx Reverse Proxy..

Can we use Nginx as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

Can Nginx run on Windows?

It can be installed on any operating system and it comes as an open source application as well. As much as Nginx is setup and supported for Windows, it does come with quite a few issues which limit it's performance. We strongly recommend that you setup Nginx on a Linux server.

Chủ đề