How to configure Nginx

    Posted in Linux Servers on Nov 02, 2020

    How to configure Nginx as a reverse proxy for Apache on CentOS

    Nginx and Apache are powerful web servers. Apache’s power and Nginx’s speed are well known, However, both of them do have drawbacks. Apache is hard on server’s memory while Nginx can’t process PHP on its own and needs the help of the PHP-FPM or similar modules for dynamic content. In this tutorial, we are going to combine the two web servers 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.

    Install and configure Nginx via below mentioned command

    Step: - 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
    

    Configure 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 ~ \.pup$ {
            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;
            }
    }
    

    Install and Configure Apache

    If you have Apache already installed, you can skip this section. install Apache web server with the command below:

    yum install httpd

    Start and enable the httpd service:

    systemctl enable httpd
    systemctl start httpd
    

    Configure 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.

    Services Cloudtechtiq offers: