Nginx as a Reverse Proxy
Shubham K. Sawant
DevSecOps @PwC | 2x AWS | Terraform Certified | GitOps Certified | Scripting | Infrastructure As a Code | CICD | Docker | Kubernetes | Ansible | Azure | AIOps
Today i completed a task that involved setting up Apache and Nginx on the server, with Nginx acting as a reverse proxy for Apache.
Below is a detailed walkthrough of the task, the steps I followed, and the best practices I adhered to.
I have divided task into the following key steps:
1. Installing and Configuring Apache
2. Installing and Configuring Nginx
3. Setting Up Nginx as a Reverse Proxy for Apache
4. Copying Files to Apache’s Document Root
5. Starting and Testing the Services
Step-by-Step Execution
1. Installing and Configuring Apache 'yum' package manager
?sudo yum install httpd -y
- Configuring Apache to Listen on Port 6000 Edit the Apache configuration file Update the 'Listen' directive. The Listen directive tells Apache to listen on port 6000. This ensures that Apache is accessible via this port from any network interface.
sudo vi /etc/httpd/conf/httpd.conf
Listen 6000
2. Installing and Configuring Nginx 1st we need to install epel repo
sudo yum install epel-release
sudo yum install nginx -y
- Configuring Nginx to Listen on Port 8095 Edit the Nginx configuration file and Update the 'server' block 'listen 8095' tells Nginx to listen on port 8095. 'proxy_pass https://127.0.0.1:6000 ' directs requests received by Nginx to Apache running on port 6000. 'proxy_set_header' directives are used to pass client-related information to Apache.
sudo vi /etc/nginx/nginx.conf
server {
listen 8095;
server_name <your_server_name>;
location / {
proxy_pass https://127.0.0.1:6000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
run nginx -t to check the configuration
领英推荐
3. Copying the Sample Index File to Apache’s Document Root This command copies the 'index.html' file from the Jump Host to Apache’s document root at '/var/www/html/'.
scp index.html user@ip/hostname:/tmp/
sudo cp /home/index.html /var/www/html/
4. Starting Apache and Nginx Services 'systemctl start' starts the services, and 'systemctl enable' ensures that they start automatically on boot.
?sudo systemctl start httpd
sudo systemctl enable httpd
?sudo systemctl start nginx
sudo systemctl enable nginx
5. Testing the Configuration tests the reverse proxy configuration by making a request to Nginx on port 8095. The request should be forwarded to Apache on port 6000, and you should see the content of index.html
curl https://< server IP or Hostname>:8095
Best Practices and Documentation
- Configuration Documentation: Always document changes made to configuration files. Note the file paths, directives changed, and reasons for the changes. This makes troubleshooting easier in the future.
- Backup Configuration Files: Before making changes to configuration files, create a backup. For example:
?sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
- Security Considerations: Ensure that your server is secure by limiting access to configuration files and using firewalls to control traffic to your server.
- Testing: Always test your configuration in a controlled environment before deploying it to production. Use tools like 'curl' to verify that your services are functioning as expected.
This was a good learning experience in setting up and configuring Nginx as a reverse proxy, a critical component in many production environments. Proper configuration and documentation are key to maintaining a secure and reliable infrastructure.
Feel free to connect or reach out if you have any questions or want to discuss more about reverse proxies and web server configurations!
Reachout to me on linkedin :-https://www.dhirubhai.net/in/shubhamsawant/
Checkout my work on GitHub :-https://github.com/shubhamksawant/shubhamksawant
Checkout more Blogs on Medium - https://medium.com/@shubhamksawant
#DevOps #Nginx #Apache #ReverseProxy #Linux #ConfigurationManagement #LearningJourney #shubhamksawant #DevOps #DevSecOps #AIOps #LearnWithShubham #DevOpsWithShubham #DevSecOpsWithShubham #AIOpsWithShubham #everydaylearning #what_did_i_learn_today