How to configure Nginx as a reverse proxy for Apache on CentOS
Introduction
In this tutorial, we are going to combine the two web servers (Apache and Nginx) to get the best result of each other, We are going to set Nginx as our static content processor and Apache to processing the back end and dynamic content.
We are assuming that you have root permission, otherwise, you may start commands with “sudo”.
Step 1 — Installing Nginx
Run an update on your repositories list first:
$ yum update -y
Now install Epel repository easily with:
$ yum install epel-release -y
Then install nginx using the command below:
$ yum install nginx -y
Enable and start Nginx service:
$ systemctl enable nginx
$ systemctl start nginx
Step 2 — Configuring Nginx
Create a config file with the following command:
$ nano /etc/nginx/conf.d/default.conf
Paste the following configuration in your file then save and exit:
server {
listen 80;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
Step 4 — Configuring Apache
We need to configure the Apache to take the backend part of the job, for that cause we need to configure the Apache to listen on port 8080:
$ nano /etc/httpd/conf/httpd.conf
Find the line that starts with “listen” remove it and paste the following lines instead:
Listen 127.0.0.1:8080
Then find the line that starts with “DocumentRoot” and modify it like below:
DocumentRoot “/usr/share/nginx/html/”
The “DocumentRoot” should be the same on both Nginx and Apache, If you have VirtualHost(s) configured, it should be configured on both of them.
Save and Exit.
How useful was this post?
Click on a star to rate it!
Average rating 1 / 5. Vote count: 1
No votes so far! Be the first to rate this post.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?