LAMP+ NGINX reverse proxy on CentOS

September 26, 2014 7k views

Hey guys. I am having lots of difficulty getting this to work.

My hope is to install Joomla, but with NginX driving the static content, and apache driving the php, on Cent OS.

I tried doing a LAMP install and then using Reverse proxy on Cent OS, but I cant seem to get it right.

Id love it if you guys had a walkthrough for this exact topic. Apache is still a very popular webserver, and having NginX infront of it would help a lot. I know it works, I just cant seem to do it myself because whenever I read online, depending which flavor of linux is used in a tutorial, some stuff ends up moving around, and wrapping my mind around this topic is a bit frustrating.

1 Answer

There are a few tutorials that touch on this on the site, but they are mostly aimed at Ubuntu. Though it will be very similar. The main difference is the package names and the file location.

The first step is to set up Nginx to serve content as normal, but proxy all requests for php files to port 8080. Your Nginx config should look like:

server {
        listen   80; 

        root /var/www/; 
        index index.php index.html index.htm;

        server_name example.com; 

        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;
        }
}

Now you'll need to set up Apache to listen on 127.0.0.1:8080 in /etc/httpd/conf/httpd.conf Find and change the any lines referencing port 80 and change them to 8080. In particular, this line:

Listen 80
  • HI asb, on following your steps, and then starting nxing, it gives the following error -

    Starting nginx: nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1
    [FAILED]

    • Hi! Sounds like you put this directly in the top level Nginx config. "Server blocks" need to be inside of the http directive. Generally that's done by using an included file. You'll probably want to put this in /etc/nginx/conf.d/default.conf if you're on CentOS 7.

Have another answer? Share your knowledge.