This tutorial will teach you how to optimize a website that is being hosted with Nginx. We'll be doing the following:
Steps in this tutorial have been tested on both CentOS 6 and 7. In addition, both 32-bit and 64-bit architectures were tested working.
Let's get started with the removal of Nginx. Also, we'll need to have OpenSSL and a few dependencies installed before we compile Nginx again.
cd ~ mkdir nginx cp -r /etc/nginx ~/nginx
yum remove nginx
yum install zlib-devel pcre-devel openssl libssl-devel make gcc gcc-c++ -y
Now that we're done with the removal of Nginx, we can compile it from source.
wgetto retrieve the Nginx source code
cd ~ wget https://nginx.org/download/nginx-1.8.0.tar.gz
tar -xvf nginx-1.8.0.tar.gz
cd ~/nginx-1.8.0 ./configure --with-http_spdy_module --with-http_ssl_module --with-http_gzip_static_module make install
We will use a third-party script from GitHub for the
cd /etc/init.d wget https://gist.githubusercontent.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a/nginx chmod +x nginx
At this point, you can move your old configuration back to the
service nginx start
Begin editing the Nginx configuration.
Within the "http" block, add the following:
Exit and save the file by hitting the escape key, proceeded with typing
:wq, then hitting enter.
Reload Nginx. This is known as a soft restart.
service nginx reload
Congratulations! You have now compiled and optimized Nginx. Going forward, you should see a performance gain in page loads on your website.
Note that adding more modules to Nginx is not possible after it has been compiled. See the bonus section below for information about adding modules.
To add modules to Nginx, you will have to repeat the compile process from this tutorial. Start by removing Nginx again. When you get to the step starting with
./configure ..., you can add modules with this syntax:
This argument can be repeated if you have more than one module.