Home > English > Tutorials > How to set NGINX as Proxy for Apache

How to set NGINX as Proxy for Apache

 

The combination of  nginx and apache

I.Preparation

Install all epel and remi repositories , get preference link here Click here

Vps installed with Apache 

Get reference here Click here

II.Proceding

Use netstat command to test open gateways

# netstat -tunpl

Returned result is that http is running through the gateway 80

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17360/mysqld
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1429/sshd
tcp        0      0 :::80                     :::*                        LISTEN      14324/httpd
tcp        0      0 :::22                       :::*                        LISTEN      1429/sshd

Step 1: Edit the configuration file of  http

Access the file httpd.conf 

# nano /etc/httpd/conf/httpd.conf

Search for the phrase Listen 80 

# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80

Listen 80

#
# Dynamic Shared Object (DSO) Support

Replace it with  Listen 8080 to disallow  http to run through the gateway

# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80

Listen 8080

#
# Dynamic Shared Object (DSO) Support

Then search for the phrase AllowOverride None

<Directory />
    Options FollowSymLinks
    AllowOverride
None

</Directory>

Change it into AllowOverride All

  <Directory />
    Options FollowSymLinks
    AllowOverride
All
</Directory>

Restart httpd service

# service httpd restart

Step 2: Install and configure nginx

First you need to add nginx package by adding the file nginx.repo

# nano /etc/yum.repos.d/nginx.repo

In the file nginx.repo, creat this content 

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

Use yum command to install nginx
# yum install nginx -y

Start the service

# service nginx start

Let the service start together with the system

# chkconfig nginx on

Edit the configuration file of nginx 

# nano /etc/nginx/conf.d/default.conf

Add blue lines and the sign # at the beginning of red lines

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
                proxy_pass  
http://ip-vps:8080;
    }

    #location / {
     #   root   /usr/share/nginx/html;
      #  index  index.html index.htm;
   # }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    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;
    }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;

Restart the nginx service

# service nginx restart

Test the result 

Type this command to test 

# curl -I http://ip-vps/info.php

If the content is as follows, you've succeeded 

HTTP/1.1 200 OK
Server: nginx/1.9.12
Date: Sat, 12 Mar 2016 02:07:06 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.19

Use netstat command to check the connection 

# netstat -tunpl

The result comes out that nginx runs through port 80 , http runs through port 8080

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17360/mysqld
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      14562/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1429/sshd
tcp        0      0 :::8080                     :::*                        LISTEN      14324/httpd
tcp        0      0 :::22                       :::*                        LISTEN      1429/sshd