Setting Proxy Nginx Centos 7


Setting Proxy Nginx Centos 7

Introduction

Setting up a proxy server on a CentOS 7 server is a relatively straightforward task. Nginx is a powerful open source reverse proxy server built on top of an optimized HTTP server engine. Nginx is excellent for serving high-performance, stable web applications under heavy load. This tutorial explains how to install Nginx on a CentOS 7 server and how to configure it as a reverse proxy for Apache web server.

Installing Nginx

First, make sure you’re working as root. Installing Nginx on CentOS 7 requires root privileges. To switch to root, type the following command:


# sudo su

The next step is to install Nginx. You can do this by running the following command:


# yum install nginx

Once Nginx is installed, you can start it by running the following command:


# systemctl start nginx

You can then check the status to make sure everything is working correctly:


# systemctl status nginx

If everything is working correctly, you should see something like this:


Active: active (running) since Mon 2019-07-15 10:12:56 EDT; 20min ago

By default, Nginx runs on port 80. You can confirm this by running the following command:


# netstat -tulpn | grep :80

You should see something like this:


tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 44696/nginx

Configuring Nginx to Proxy Apache

We now need to configure Nginx to proxy requests to Apache. To do this, edit the Nginx configuration file located at /etc/nginx/nginx.conf. Scroll down to the “HTTP” section of the file and add the following line:


proxy_pass http://127.0.0.1:8080;

The line above tells Nginx to proxy requests to the Apache server listening on port 8080.Save the file and restart Nginx with the following command:


# systemctl restart nginx

Testing the Proxy

Now that Nginx is configured as a proxy for Apache, we can test it by making a request to Apache via Nginx. To do this, make a request to the following URL:


http://your_server_ip/

If everything is working correctly, you should see the default Apache page. You can also test the proxy by making a request to the following URL:


http://your_server_ip/proxy_test

If everything is working correctly, you should see the default Nginx page.

Installing Apache

If you don’t already have Apache installed, you can install it with the following command:


# yum install httpd

Once Apache is installed, you can start it by running the following command:


# systemctl start httpd

You can then check the status to make sure everything is working correctly:


# systemctl status httpd

If everything is working correctly, you should see something like this:


Active: active (running) since Mon 2019-07-15 10:12:56 EDT; 20min ago

Configuring Nginx to Proxy VIRTUAL HOSTS

Now that Nginx and Apache are installed, it’s time to configure Nginx to proxy requests to the appropriate virtual hosts. To do this, edit the Nginx configuration file located at /etc/nginx/nginx.conf and add the following lines:


http {
upstream myproxyserver {
server 127.0.0.1:8080
}

server {
listen 80;
server_name www.mydomain.com;

location / {
proxy_pass http://myproxyserver;
}
}
}

Save the file and restart Nginx with the following command:


# systemctl restart nginx

Conclusion

In this article, we installed Nginx and Apache, and configured a reverse proxy for Apache web server using Nginx on a CentOS 7 server. Setting up a proxy server is a relatively straightforward task but requires a good understanding of the configuration options available for both Nginx and Apache. We hope you have found this tutorial useful.

Thank You for Reading

Thank you for reading this article. If you have any questions or comments, please feel free to leave them in the comments section below. We hope this article has been helpful and if you found it useful, please share it with your friends and colleagues.

Leave a Reply

Your email address will not be published. Required fields are marked *