How to Use Nginx as a Reverse Proxy

How to Use Nginx as a Reverse Proxy

Step 1: Install Nginx

If you haven't already installed Nginx, you can do so on Ubuntu or Debian-based systems using the following commands:

sudo apt update sudo apt install nginx

For CentOS or RedHat-based systems, use:

sudo yum install nginx

Step 2: Start and Enable Nginx

After installation, start the Nginx service and enable it to start on boot:

sudo systemctl start nginx sudo systemctl enable nginx

Step 3: Configure Nginx as a Reverse Proxy

To configure Nginx to act as a reverse proxy, you need to modify the Nginx configuration file.

  1. Open the Nginx configuration file:

sudo nano /etc/nginx/sites-available/default
  1. Modify the server block to include a reverse proxy configuration. For example:

server { listen 80; server_name yourdomain.com; # Replace with your domain or public IP location / { proxy_pass http://localhost:3000; # Replace with the backend server address proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }

In this configuration:

  • proxy_pass points to the backend server (in this case, it's http://localhost:3000).

  • proxy_set_header Ensures that the correct headers are passed to the backend server.

  • proxy_http_version Ensures the use of HTTP/1.1 for WebSocket support (if needed).

  1. Save the file and exit the editor.

Step 4: Test Nginx Configuration

Before restarting Nginx, it's a good practice to test the configuration for syntax errors:

sudo nginx -t

If the configuration is correct, you should see a message that says "syntax is okay" and "test is successful."

Step 5: Restart Nginx

Now, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Adjust Firewall Rules (if applicable)

If you have a firewall enabled, ensure that HTTP traffic is allowed through the firewall.

For UFW (Ubuntu firewall), run:

sudo ufw allow 'Nginx Full'

For Firewalld (CentOS/RHEL), run:

sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --reload

Step 7: Verify the Reverse Proxy

Now, visit your server's public IP or domain in a browser. If everything is configured correctly, Nginx should forward the requests to the backend server (e.g., a Node.js or any other application running on port 3000) and return the content.

For example, if your Nginx server is acting as a reverse proxy to a web server running on localhost:3000, accessing http://yourdomain.com will load content from the server running on localhost:3000.

Step 8: Configure SSL (Optional)

If you want to set up SSL (HTTPS) with Nginx, you can use Let's Encrypt for a free SSL certificate. Here are the steps:

  1. Install Certbot:

sudo apt install certbot python3-certbot-nginx
  1. Obtain the SSL certificate:

sudo certbot --nginx -d yourdomain.com
  1. Follow the instructions to complete the SSL setup. Certbot will automatically configure SSL for you in Nginx.

Step 9: Test the Reverse Proxy

Finally, test everything:

  • Check your Nginx logs for any issues: sudo tail -f /var/log/nginx/error.log.

  • Ensure that your backend service is running and accessible.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close