Table of Contents
What you will read?
Nginx is a powerful web server that can also function as a reverse proxy server. Setting up Nginx as a proxy server can help distribute load, enhance security, and cache content. In this guide, we’ll walk you through the steps to configure Nginx as a proxy server on a Linux system.
Step 1: Install Nginx
First, ensure that Nginx is installed on your server. You can install Nginx using the package manager of your choice. For Ubuntu or Debian-based systems, use the following commands:
sudo apt update
sudo apt install nginx
For CentOS or RHEL-based systems, use:
sudo yum install epel-release
sudo yum install nginx
After installation, start and enable Nginx to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Configure Nginx as a Proxy Server
Next, you need to configure Nginx to act as a proxy server. You will do this by editing the default configuration file or creating a new configuration file.
Editing the Default Configuration
-
Open the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/defaultFor CentOS, the configuration file is typically located at
/etc/nginx/nginx.conf. - Modify the server block to include the proxy settings. Here’s an example configuration for a simple reverse proxy that forwards requests to an upstream server (e.g., a web application running on port 3000):
server { listen 80; # Listen on port 80 server_name your_domain.com; # Replace with your domain or IP location / { proxy_pass http://localhost:3000; # Forward requests to the upstream server proxy_http_version 1.1; # Use HTTP 1.1 proxy_set_header Upgrade $http_upgrade; # Handle WebSocket connections proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; # Preserve the original Host header proxy_cache_bypass $http_upgrade; # Bypass cache for WebSocket connections } }
Creating a New Configuration File (Optional)
If you prefer to create a new configuration file, you can do so:
-
Create a new file in the
sites-availabledirectory: -
Add the same configuration as above, modifying it as needed.
- Create a symlink to enable the configuration:
sudo ln -s /etc/nginx/sites-available/myproxy.conf /etc/nginx/sites-enabled/
Step 3: Test the Configuration
Before reloading Nginx, it’s important to test the configuration for any syntax errors:
sudo nginx -t
If the configuration is valid, you should see a message saying that the test was successful.
Step 4: Reload Nginx
To apply the changes, reload the Nginx service:
sudo systemctl reload nginx
Step 5: Configure Firewall (if necessary)
If you have a firewall running, ensure that it allows traffic on port 80 (or 443 if you are using SSL):
For UFW:
sudo ufw allow 'Nginx Full'
For Firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 6: Test Your Proxy Server
To verify that your Nginx proxy server is working, open a web browser and navigate to http://your_domain.com. You should see the content served by the upstream server running on port 3000.
You have successfully set up Nginx as a proxy server! This setup can be further enhanced with SSL termination, load balancing, caching, and more, depending on your needs. For more guides and tutorials, visit dropvps.com. Enjoy your new proxy server!
