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:
For CentOS or RedHat-based systems, use:
Step 2: Start and Enable Nginx
After installation, start the Nginx service and enable it to start on boot:
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.
-
Open the Nginx configuration file:
-
Modify the
server
block to include a reverse proxy configuration. For example:
In this configuration:
-
proxy_pass
points to the backend server (in this case, it'shttp://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).
-
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:
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:
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:
For Firewalld (CentOS/RHEL), run:
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:
-
Install Certbot:
-
Obtain the SSL certificate:
-
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.